quick xslt for-each question
Let's say I have an XML doc开发者_如何学JAVAument that has this:
<keywords>
<keyword>test</keyword>
<keyword>test2</keyword>
<keyword>test3</keyword>
<keyword>test4</keyword>
</keywords>
And i want to use XSLT to print it out in HTML like this:
<ul>
<li>test</li>
<li>test2</li>
<li>test3</li>
<li>test4</li>
</ul>
What would my XSLT look like? I tried this:
<ul>
<xsl:for-each select="keywords/keyword">
<li><xsl:value-of select="keyword"/></li>
</xsl:for-each>
</ul>
but that doesn't print out the keyword value, just a blank item.
<ul>
<xsl:for-each select="keywords">
<li><xsl:value-of select="keyword"/></li>
</xsl:for-each>
</ul>
doesn't work (for obvious reasons), but at least it prints the first keyword!
suggestions? thanks!
I would suggest avoiding for-each
here, and doing it using a template - it's more idiomatic in XSLT, especially when you have a clear one-to-one mapping:
<xsl:template match="keyword">
<li><xsl:value-of select="."/></li>
</xsl:template>
<xsl:template match="keywords">
<ul><xsl:apply-templates/></ul>
</xsl:template>
<ul>
<xsl:for-each select="keywords/keyword">
<li><xsl:value-of select="text()"/></li>
</xsl:for-each>
</ul>
using text() should get you the text inside the keyword node
精彩评论