Dealing with Attributes and Elements in XPath with XSLT using ASP.NET 2.0 - Retrieving all values of all attributes available at any level of hierarchy (or path)
(Page 3 of 6 )
In the previous sections, we tried to search for a particular attribute. Now I would like to retrieve the values of all attributes present in the document, regardless of what element or hierarchy it belongs to!
The following is the first approach we can use to retrieve all values of any 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 ="//@*"/>
</b>
</xsl:template>
<xsl:template match="@*">
<xsl:value-of select ="."/>
<br/>
</xsl:template>
</xsl:stylesheet>
Within the above code, you can observe that I am using “//@*” which stands for “any attribute at any path.” 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="//@*">
<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 looks something like the following:
<b>
10<br />
Accounting<br />
New York<br />
20<br />
Sales<br />
Dallas<br />
30<br />
Research<br />
Washington<br />
</b>
Next: Retrieving values of attributes and elements simultaneously >>
More ASP.NET Articles
More By Jagadish Chaterjee