I have an XML schema that contains the following type :
<xs:simpleType name="valuelist">
<xs:list itemType="xs:double"/>
</xs:simpleType>
A sample XML fragment would be:
<values>1 2 3.2 5.6</values>
In an XSLT transform, how do I get the number of elements in the list?
How do I iterate over the elements?
I. XPath 2.0 (XSLT 2.0) solution:
count(tokenize(., ' '))
II. XPath 1.0 (XSLT 1.0) solution:
string-length()
-
string-length(translate(normalize-space(), ' ', ''))
+ 1
As for iteration over the items of this list:
select attribute:--
for $i in tokenize(., ' '),
$n in number($i)
return
yourXPathExpression
--
2. In XSLT 1.0 you need to have some more code for splitting/tokenization. There are several good answers to this question (part of them mine) -- just search for something like "xslt split a string"
If list is strictly spaced, you can count spaces based on string length.
<values>1 2 3.2 5.6</values>
XSLT:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:template match="/">
<xsl:value-of select="string-length(values)
- string-length(translate(values, ' ', '')) + 1"/>
</xsl:template>
</xsl:stylesheet>
To iterate over elements you have to split this string. There a lot of examples at SO.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With