Count XML items with specific attribute value

To count the number of nodes that have specific attribute or that have attribute with specific value in your XSLT transformation, use the following code:
  • All elements having the specified attribute:
  • <xsl:select value="count(//*[@attr])" />
  • All elements having attribute with desired value:
  • <xsl:select value="count(//*[@attr = 'value'])" />
For example, to count all elements in an XHTML document having a class name, use:
<xsl:select value="count(//*[@class])" />
To count all elements with class name containing 'menu' use:
<xsl:select value="count(//*[contains(@class,'menu')])" />

No comments:

Post a Comment