Reputation: 13
I am having trouble with my XSLT for-each statements. When I run the XML through the XSLT, it only comes up with the first iteration of the list, and then stops. It doesn't post the values either. Here is the XML code.
<?xml version="1.0" encoding="UTF-8"?>
<template>
<L>
<Q>Hey</Q>
<Q>There</Q>
<Q>Thank
<R>You</R>
<R>For</R>
<R>The</R>
<R>Help</R>
<R>I</R>
<R>Hope</R>
<R>This</R>
</Q>
<Q> will work!
</Q>
</L>
</template>
here is the xslt code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/template/L">
<html>
<body>
<ul><xsl:for-each select="Q">
<li><xsl:value-of select="Q"/>
<ul><xsl:for-each select="R">
<li><xsl:value-of select="R"/></li>
</xsl:for-each></ul>
</li>
</xsl:for-each></ul>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
the output should look like an unordered list with another list embedded inside of it (the R tags are the embedded list values). It should look something like this:
here
Upvotes: 1
Views: 77
Reputation: 116959
Try it this way?
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/template/L">
<html>
<body>
<ul>
<xsl:for-each select="Q">
<li>
<xsl:value-of select="text()"/>
<ul>
<xsl:for-each select="R">
<li>
<xsl:value-of select="."/>
</li>
</xsl:for-each>
</ul>
</li>
</xsl:for-each>
</ul>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Explanation:
When you are in the context of <xsl:for-each select="Q">
, the instruction <xsl:value-of select="Q"/>
does not select anything (unless the Q
element has another Q
element as a child). Similarly, <xsl:value-of select="R"/>
does not select anything when the context is <xsl:for-each select="R">
.
This is in addition to the comment regarding self-closing the xsl:for-each
element.
the sub-list should only be created if the
<Q>
tag has a<R>
tag child
I don't see what difference it makes (in HTML), but if you want, you can do:
<xsl:template match="/template/L">
<html>
<body>
<ul>
<xsl:for-each select="Q">
<li>
<xsl:value-of select="text()"/>
<xsl:if test="R">
<ul>
<xsl:for-each select="R">
<li>
<xsl:value-of select="."/>
</li>
</xsl:for-each>
</ul>
</xsl:if>
</li>
</xsl:for-each>
</ul>
</body>
</html>
</xsl:template>
Upvotes: 1