Dealing with Attributes and Elements in XPath with XSLT using ASP.NET 2.0 - Retrieving all values of a particular attribute at any level of hierarchy (or path)
(Page 2 of 6 )
In the previous section, we tried to search for an attribute at a particular level of the hierarchy (or path). Now I would like to retrieve the values of a particular attribute regardless the hierarchy of its element.
The following is the first approach we can use to retrieve all the values of a particular attribute at any location (using a separate template):
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<b>
<xsl:apply-templates select ="//@Dname"/>
</b>
</xsl:template>
<xsl:template match="@Dname">
<xsl:value-of select ="."/>
<br/>
</xsl:template>
</xsl:stylesheet>
Within the above code, you can observe that I am using “//@Dname” which stands for “anywhere the attribute Dname exists.” The following is the second approach we can use to accomplish our goal (using the “xsl:for-each” construct).
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<b>
<xsl:for-each select="//@Dname">
<xsl:value-of select="."/>
<br/>
</xsl:for-each>
</b>
</xsl:template>
</xsl:stylesheet>
In any of the above cases, when the XSLT gets executed, the transformation would be the same as provided in previous section
Next: Retrieving all values of all attributes available at any level of hierarchy (or path) >>
More ASP.NET Articles
More By Jagadish Chaterjee