0

I want to get the code for the medias node with the lowest index. The source XML looks similar to this:

<item>
    <medias>
        <index>999997</index>
        <code>0001</code>
    </medias>
    <medias>
        <index>1</index>
        <code>0002</code>
    </medias>
    <medias>
        <index>999998</index>
        <code>0003</code>
    </medias>
    <medias>
        <index>999999</index>
        <code>0004</code>
    </medias>
</item>

In my XSLT I tried this:

<xsl:variable name="minIndex" select="math:min(item/medias/index)" />
<xsl:value-of select="item/medias[index=$minIndex]/code" />

But for some reason, the minIndex variable does not get the lowest value of all values, but simply the value of the first node, in this case 999997.

When sorting the nodes though, the values are ordered correctly:

<xsl:for-each select="item/medias">
    <xsl:sort select="index" />
    <xsl:value-of select="." /><xsl:text>,</xsl:text>
</xsl:for-each>

This outputs:

1,999997,999998,999999

Is there something else I have to take care for when using math:min? I also tried adding /text() to the end of my selection (after index), but that didn't work either.

EDIT: I'm constructing my XSL transformer in Java using simply this code and giving it the XSLT file as the source:

Transformer transformer = TransformerFactory.newInstance().newTransformer(xsltSource);
michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
maxdev
  • 2,491
  • 1
  • 25
  • 50
  • 1
    How is the namespace prefix `math` declared? It suggests you are using an extension function. However you've tagged this XSLT 2.0, but and `min` is an built-in XPath 2.0 function, so you can just do `` – Tim C Jan 18 '17 at 13:28
  • See http://xsltransform.net/ncntCSF where Tim's suggestion works fine. – Martin Honnen Jan 18 '17 at 13:29
  • It also seems to work fine when using the Exslt math extension. See http://xsltransform.net/pPJ8LVH. Can you say exactly what XSLT processor you are using? Thanks! – Tim C Jan 18 '17 at 13:31
  • Sorry for the missing information in the question. The `math` namespace seems to be provided by Java; simply using `min` without a prefix tells me that this function does not exist :( – maxdev Jan 18 '17 at 13:56
  • Can you post a minimal but complete stylesheet? Does using `` work better? – Martin Honnen Jan 18 '17 at 14:03
  • @MartinHonnen I tried using the same as in Tim's example. `lowest` seems to do the same for me! – maxdev Jan 18 '17 at 14:07
  • http://xsltransform.net/bwdwsq uses Xalan and the suggested `math:lowest` from EXSLT and seems to do the job. So which Java version do you use where the code does not work? I guess you are better off switching to Saxon 9 and XSLT 2.0 if you are running into a bug in the default transformer and need to install/integrate a different transformer anyway. – Martin Honnen Jan 18 '17 at 14:15
  • @MartinHonnen when explicitly adding the namespace `xmlns:math="http://exslt.org/math"` to my transformation file it works! Curious why it doesn't throw an error otherwise.. oO – maxdev Jan 18 '17 at 14:22
  • @maxdev Perhaps you're not capturing the error. – michael.hor257k Jan 18 '17 at 16:55

2 Answers2

0

When explicitly specifying the namespace to use the EXSLT extensions, both math:min and math:lowest do the job.

<?xml version="1.0"?>
<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:math="http://exslt.org/math">
                                       ...
maxdev
  • 2,491
  • 1
  • 25
  • 50
  • It only does the job if the XSLT processor supports those extension functions. And since you're obviously using an XSLT 1.0 processor, you should not be declaring `version="2.0"`. – michael.hor257k Jan 18 '17 at 16:53
0

math:min(...) is used e.g. in Java (precisely - math.min(...)).

Write just min(...).

I tried this and got the proper result.

Valdi_Bo
  • 30,023
  • 4
  • 23
  • 41
  • `math:min()` is not in Java, it's an EXSLT extension function. And you can only use `min()` if you're using an XSLT 2.0 processor - which OP is obviously not. – michael.hor257k Jan 18 '17 at 16:58