Jump to content

Count with nested elements on different levels


Motxslt

Recommended Posts

Suppose you have the following: <System> <system type="Main"> <properties> <prop name="Color" value="Green" /> <prop name="Purpose" value="Relaxing" /> </properties> <system type="Secondary"> <properties> <prop name="Texture" value="Smooth" /> <prop name="Smell" value="Flowery" /> <prop name="Aesthetics" value="Fair" /> </properties> </system> </system> If there is a scenario where you need to count all that are Color="green" and Texture="Smooth". Note the elements are on different levels. What is the format? How do you obtain elements located in different levels? <xsl:variable name="GreenSmoothFood"select="count(//system[@type=Main]/properties/prop[@name=Color' and @value='Green]/system[@type=PortableConfiguration]/properties[prop[@name=Texture' and @value='Smooth]])"/>Thanks,

Link to comment
Share on other sites

  • 1 month later...

Hi, The following XSLT should work as long as you use a XSLT 2.0 processor:

<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions"><xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:template match="/">  <xsl:element name="data">   <xsl:element name="totalGreenColor">    <xsl:value-of select="count(//prop[@name = 'Color' and @value = 'Green'])" />   </xsl:element>   <xsl:element name="totalSmoothTexture">    <xsl:value-of select="count(//prop[@name = 'Texture' and @value = 'Smooth'])" />   </xsl:element>  </xsl:element></xsl:template></xsl:stylesheet>

Using "//prop" (without the ") means select all <prop> elements. With in the brackets you can select only those prop elements that are needed.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...