0
votes

I'm struggling to make a counter with conditions. My XML is:

<comments>
    <comment>
        <name>Jonh</name>
        <num>4/7</num>
    </comment>
    <comment>
        <name>Mary</name>
        <num>2/9</num>
    </comment>
    <comment>
        <name>Catie</name>
        <num>12/2</num>
    </comment>
    <comment>
        <name>Stefen</name>
        <num>127/300</num>
    </comment>
</comments>

The tag has the following structure:

number1/number2

And I want to know how often the number1 is greater than the number2 in all tags

I've tried with count:

count(tokenize(//comment/num, '/')[1] &gt; tokenize(//comment/num, '/')[2])

But no results. I thought about using a variable as an counter but they are immutable. How can I solve this?

2

2 Answers

1
votes

You can use substring-before and substring-after to calculate those values:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="/">
        number1 is greater than the number2
        <xsl:value-of select="count(//num[substring-before(.,'/') &gt; substring-after(.,'/')])" /> times.
    </xsl:template>
</xsl:stylesheet>
0
votes

You're nearly there as it is, the trick is that you need to move the gt check to a predicate

count(//comment/num[tokenize(., '/')[1] &gt; tokenize(., '/')[2]])

to select out the num elements you're interested and then count those.