I want to:
- select all attributes “foo”, which are string values, and store the values in a list.
- transform each value of attribute “foo” in this list using some map in my xslt to a number.
- select the max value of the list and output that.
So given the following xml:
<t>
<tag foo="A">apples</tag>
<tag foo="C">oranges</tag>
<tag foo="B">trees</tag>
</t>
And the following mapping:
<xsl:variable name="myMap">
<entry key="A">1</entry>
<entry key="B">2</entry>
<entry key="C">3</entry>
</xsl:variable>
The output would be:
<max>3</max>
Another question, why can’t I indent my code? I’m putting spaces but it’s not working.
I This standard XSLT 1.0 transformation (most resembling your approach):
when applied on the following XML document (as you didn’t provide any):
produces the wanted, correct result:
Explanation: Appropriate use of the XSLT
current()function.II. XSLT 1.0 solution using keys for speed
When this transformation is applied on the same XML document (above), the same correct result is produced.
Explanation:
Use of
<xsl:for-each select="document('')">to set the current document to the stylesheet, so that thekey()function will use the key index built for this document.Saving the node matched by the template in a variable so that we can use it inside the
xsl:for-each—current()cannot be correctly used here, because it gets the current node on whichxsl:for-eachoperates.UPDATE: The OP has now clarified in a comment that his biggest problem is finding the maximum.
When given this XML document (the one provided by the OP lacks a singlr top element):
the wanted, correct result is produced:
Remark: A more efficient way of calculating maximum (or minimum — in a similar way) in XSLT 1.0 is to do this:
This is a SO bug that they failed to fix for many months.
Most probably you are using IE. If your version is 9, then do the following:
Press F12.
In the window that pops up click on the right-most menu and select: “Document mode: IE9 Standards”
Now you should be able to see the code with indentation.