This discussion is archived
3 Replies Latest reply: Jan 25, 2013 10:47 AM by S_27 RSS

XSL Question

S_27 Newbie
Currently Being Moderated
Hi All,

I want to increment a counter in a loop based on a condition.

Here is how my xml looks like

<result>
<resultset id=1>
<value>
<300>
</value>
</resultset>
<resultset id=2>
<value>
<200>
</value>
</resultset>
<resultset id=2>
<value>
<500>
</value>
</resultset>
<result>

I need to look at each and every resultset and check if the value is >400 and if it is then display some text. Something like this

<outputvalue>
Yes, there is a value greater than 400
</outputvalue>

In my XSL, I declared a variable called count with value 0. I created a for-loop which goes through these results. Then inside the loop there is a condition to cehck if the value is > 400. If the value is >400, then the counter is incremented.

````````````````````
<outputvalue>
<xsl:variable name="Count" select="0"></xsl:variable>

<xsl:for-each select="/results/resultset">
<xsl:variable name="recnum" select="./@id"/>
<xsl:if test="//results/resultset[@id=$recnum]//value &gt; 400'">
<xsl:value-of select="$Count+1"/>
</xsl:if>
</xsl:for-each>


<xsl:if test="$Count &gt; 1">
Yes, There is a resultset with greater than 400
</xsl:if>
</outputvalue>

``````````````````````

There is a problem with the above code is that result is like this
<outputvalue>
*1*
Yes there is a resultset with greater than 400
</outputvalue>

Is there a way I can just remove the character 1 from the output? Or is there a better way to increment?

Thanks,
S
  • 1. Re: XSL Question
    odie_63 Guru
    Currently Being Moderated
    Hi,
    S_27 wrote:
    I want to increment a counter in a loop based on a condition.
    ...
    Is there a way I can just remove the character 1 from the output? Or is there a better way to increment?
    XSLT basic concepts.
    You can't do that, XSLT is not a procedural language.
    When you declare an xsl:variable, it takes the value assigned to it at creation time and that's the end of it, it's immutable, it now acts as a constant in the scope of its existence (top-level or template).

    Here is how my xml looks like

    <result>
    <resultset id=1>
    <value>
    <300>
    ...
    <result>
    Yes, "looks like" is the right term, what you've posted is far from being wellformed XML.

    If the requirement is as simple as you describe then you don't have to count, increment or loop, a simple XPath predicate is sufficient.
    result/resultset[value &gt; 400]
    This XPath targets all resultset nodes that possess a value element greater than 400. If the resulting sequence is not empty then the test yields "true", else "false" :
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
      <xsl:output method="xml" encoding="UTF-8"/>
      <xsl:template match="/">
        <output>
          <xsl:if test="result/resultset[value &gt; 400]">
            <xsl:text>Yes, There is a resultset with greater than 400</xsl:text>
          </xsl:if>
        </output>
      </xsl:template>
    </xsl:stylesheet>
    SQL> select xmltransform(
      2  xmlparse(document
      3  '<result>
      4    <resultset id="1">
      5       <value>300</value>
      6    </resultset>
      7    <resultset id="2">
      8       <value>200</value>
      9    </resultset>
     10    <resultset id="3">
     11       <value>500</value>
     12    </resultset>
     13  </result>'),
     14  xmlparse(document
     15  '<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
     16    <xsl:output method="xml" encoding="UTF-8"/>
     17    <xsl:template match="/">
     18      <output>
     19        <xsl:if test="result/resultset[value &gt; 400]">
     20          <xsl:text>Yes, There is a resultset with greater than 400</xsl:text>
     21        </xsl:if>
     22      </output>
     23    </xsl:template>
     24  </xsl:stylesheet>')
     25  )
     26  from dual;
    
    XMLTRANSFORM(XMLPARSE(DOCUMENT'<RESULT><RESULTSETID="1"><VALUE>300</VALUE></RESU
    --------------------------------------------------------------------------------
    <?xml version="1.0" encoding="WINDOWS-1252"?>
    <output>Yes, There is a resultset with greater than 400</output>
  • 2. Re: XSL Question
    S_27 Newbie
    Currently Being Moderated
    Hi Odie,

    Thanks for the inputs. I really appreciate it.

    I couldn't post the original input message in forums and hence I madeup the sample XML message which is not well formed.

    Your inputs really helped!

    Thank You!

    -S
  • 3. Re: XSL Question
    S_27 Newbie
    Currently Being Moderated
    Merci ;)

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points