0

I am looking to have xslt grab every occurance of a field, in this case //field and then find the largest value held and output that.. would something like the below work? I have used something similair before but i am a little unsure on sort select'"value"

<xsl:choose>
    <xsl:when test="count(//field1) &gt; 0">
        <xsl:for-each select="//field1">
            <xsl:sort select="value" order="descending" />
                <xsl:if test="position() = 1">
                    <xsl:value-of select="."/>
                </xsl:if>
        </xsl:for-each>
    </xsl:when>
    <xsl:otherwise>0</xsl:otherwise>
</xsl:choose>

the xml will be in a format something like below

<1>
   <2>
       <field>125</field>
   </2>
</1>
<1>
   <2>
       <field>10</field>
   </2>
</1>
<1>
   <2>
       <field>0</field>
   </2>
</1>

(Not my choice of format for the XML, but it is what i have been dumped with to work with... in msot cases this field appears 28 times, but i always need the highest value.

2 Answers 2

1

Not sure about XSLT 2 but EXSLT provides math:max.

<h1>Max value is <xsl:value-of select='math:max(//field)' /></h1>

http://www.xmlplayground.com/3D3tUE

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

1 Comment

Perfect, using the math namespace has worked a treat, thank you so much!
1

Try using max()...

<xsl:value-of select="max(//field)"/>

3 Comments

javax.xml.transform.TransformerException: org.xml.sax.SAXException: Could not find function: max javax.xml.transform.TransformerException: Could not find function: max, i will have a look around for some examples of the field see if i missed anything
@JonathanSpickernell- You're not using an XSLT 2.0 processor. I suggest removing the 2.0 tag. Also, you can change your xsl:sort to <xsl:sort select="." order="descending" /> and it should work.
As far as i am aware this is using XSLT 2.0 but i will remove and then double check whats running on the service.

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.