Reputation: 33
In the xsd file, I define a element has more occurrences:
<xs:element name="Type" type="xs:string" maxOccurs="unbounded"/>
so in the xml file the object may contain more "Type" elements. In the xsl file, What I do is:
<xsl:for-each select="Movies/Movie">
<tr>
<td><xsl:value-of select="Type"/></td>
</tr>
</xsl:for-each>
By this method, I can only get the first "Type" element in that node set. But I want to select all the "Type" elements existed in the "Movies/Movie" node set, is there a way to realize this?
Upvotes: 3
Views: 251
Reputation: 52888
You would need to use another xsl:for-each
or use xsl:apply-templates
instead.
Here's an example that doesn't use xsl:for-each
...
XML Input
<Movies>
<Movie>
<Type>a</Type>
<Type>b</Type>
<Type>c</Type>
</Movie>
<Movie>
<Type>1</Type>
<Type>2</Type>
<Type>3</Type>
</Movie>
</Movies>
XSLT 1.0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/*">
<html>
<xsl:apply-templates/>
</html>
</xsl:template>
<xsl:template match="Movie">
<tr>
<xsl:apply-templates/>
</tr>
</xsl:template>
<xsl:template match="Type">
<td><xsl:value-of select="."/></td>
</xsl:template>
</xsl:stylesheet>
Output
<html>
<tr>
<td>a</td>
<td>b</td>
<td>c</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
</html>
Upvotes: 2
Reputation: 163625
In XSLT 1.0, when xsl:value-of selects more than one node, all but the first are ignored. In XSLT 2.0, you get the space-separated concatenation of all the selected nodes. It sounds from your evidence as if you are using XSLT 1.0. If you want to select multiple elements in XSLT 1.0, you need a for-each:
<xsl:for-each select="Type">
<xsl:value-of select="."/>
</xsl:for-each>
Upvotes: 2
Reputation: 34387
Try something as below(match and select with index as 1):
<xsl:template match="/Movies/Movie">
<xsl:value-of select="Type[1]"/>
</xsl:template>
Upvotes: 0