1

I have the following xml

<color>
   <title>white</title>
</color>
<color>
   <title>black</title>
</color>
<color>
   <title>white</title>
</color>
<color>
   <title>black</title>
</color>
<color>
   <title>white</title>
</color>

I need to get the count of color nodes where title equals 'white' using xslt

ie to get the result: 3

Thanks

2 Answers 2

1

Solution : count(color[./title='white'])

Please try the following code

 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="1.0">
 <xsl:template match="*">
    <Value>
        <xsl:value-of select="count(color[./title='white'])"/>
    </Value>
 </xsl:template>
 </xsl:stylesheet>

Output : <Value>3</Value>

Sign up to request clarification or add additional context in comments.

Comments

1

You'll need some XSLT to start from, and you'll need valid XML (that only has one root element).

And I would need you to provide both of those in order to give you a full answer, but essentially, you can use the count() function and a predicate:

<xsl:value-of select="count(//color[title = 'white'])" />

More complete example:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

    <xsl:template match="/">
      <n>
        <xsl:value-of select="count(//color[title = 'white'])"/>
      </n>
    </xsl:template>
</xsl:stylesheet>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.