1

I have an XML which has the following content:

<ruletypes>
 <ruletype>Local</ruletype>
 <ruletype>Global</ruletype>
 ...
</ruletypes>

I'm wanting a list of the ruletypes, I tried the following:

<xsl:for-each select="//ruletypes/ruletype">
 <li><xsl:value-of select="ruletype"/></li>
</xsl:for-each>

but it's not working

2 Answers 2

3

change the select like this:

<xsl:template match="/">
<xsl:for-each select="//ruletypes/ruletype">
      <li><xsl:value-of select="."/></li>
</xsl:for-each>

Sign up to request clarification or add additional context in comments.

1 Comment

To clarify, xsl:for-each sets the context to the ruletype element that is selected, so the OP's select="ruletype" looks for another ruletype under that element (and there are none). This way, the current ruletype element is selected (.) and converted to a string, which will return the node's text value.
3

Eschew for-each and let the XSLT processor do most of the work:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="ruletype">
        <li><xsl:apply-templates/></li>
    </xsl:template>
</xsl:stylesheet>

When applied to this document:

<ruletypes>
   <ruletype>Local</ruletype>
   <ruletype>Global</ruletype>
</ruletypes>

Produces the following output:

<li>Local</li>
<li>Global</li>

Note that this takes advantage of XSLT's built-in template for elements, which keeps the processing moving until an "interesting" node is encountered, and its built-in template for text nodes, which copies text through.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.