0

## I have my xml as below and I want get the count of title node I have tried below but I always get count as 1 since it is under for-each loop

    <xsl:if test="$countryVal !='' and $countryVal = 'USA'">
<tr>
      <td><xsl:value-of select="title"/></td>
      <h3>Count of titles <xsl:value-of select="count(catalog/cd/title)"/></h3>
    </tr>
    </xsl:if>
    </xsl:for-each>

<catalog>
  <cd>
    <title>USD</title>
    <artist>Bob Dylan</artist>
    <country>USA</country>
    <company>Columbia</company>
    <price>10.90</price>
    <year>1985</year>
  </cd>
  <cd>
    <title>USD</title>
    <artist>Bonnie Tyler</artist>
    <country>USA</country>
    <company>CBS Records</company>
    <price>9.90</price>
    <year>1988</year>
  </cd>
  <cd>
    <title>CAD</title>
    <artist>Dolly Parton</artist>
    <country>CAN</country>
    <company>RCA</company>
    <price>9.90</price>
    <year>1982</year>
  </cd>
  <cd>
    <title>USD</title>
    <artist>Gary Moore</artist>
    <country>USA</country>
    <company>Virgin records</company>
    <price>10.20</price>
    <year>1990</year>
  </cd>
</catalog>
#

xml shared is part of big xml sharing the snippet of it. Any pointers?

3
  • Are you looking for the number of <title> nodes in each <cd>, or the number in the whole <catalog>, or something else? If you're looking for the number in the whole catalog, you don't need the <xsl:foreach>; the count will check for all of the results below the <catalog> node. Commented May 6, 2017 at 11:08
  • I'm looking for count of title node under CD only for US countries. Commented May 6, 2017 at 11:15
  • 1
    If that is the case, you can just do <xsl:value-of select="count(catalog/cd[country='USA']/title)"/>, but this statement would need to go after the xsl:for-each, and not inside it. Note that you could also simplify your xsl:for-each to just <xsl:for-each select="catalog/cd[country='USA']"> and avoid the need for a variable and if condition inside it. Commented May 6, 2017 at 11:33

1 Answer 1

1

I would suggest you use a variable to hold the filtered node-set - so that you only need to apply the filtering once:

<xsl:variable name="eligible-cds" select="/catalog/cd[country='USA']" />

Then you can do:

<xsl:for-each select="$eligible-cds">
    <tr>
        <td><xsl:value-of select="title"/></td>
    </tr>
</xsl:for-each>
<h3>Count of titles <xsl:value-of select="count($eligible-cds)"/></h3>
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you that helped but I'm looking for based on my count result inside loop I need to show table data for example if count of title's node is more than one then I need to display only one title since in my case title has same value due to which title values are getting duplicated. I have updated xml my expected xsl logic is something like below
Ty that helped but based on my count result inside loop I need to show table data for example if count of title's node is more than one then I need to display only one title since in my case title has same value due to which title values are getting duplicated. updated xml Expected <xsl:if test="$countryVal !='' and $countryVal = 'USA' and count(title) < 2"> <tr> <td><xsl:value-of select="title"/></td> <h3>Count of titles <xsl:value-of select="count(catalog/cd/title)"/></h3> </tr> </xsl:if> </xsl:for-each>
I am afraid I don't understand your comments. Please edit your question and provide an example where the proposed method would not provide the expected output. -- Note that you cannot count within xsl:for-each, because it is not a loop.
I have scenario where I have to get count result inside for-each loop. If count() would not give the desire result then how do we get the count xml node inside for-each loop. Any other functions or pointers? Sample xml is my first post.

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.