Reputation: 23285
What XSLT will transform the following pattern of XML into the HTML output below?
<ELEMENT1>
<!--
Comment for element 2
-->
<ELEMENT2>
<ELEMENT3>ABC</ELEMENT3>
</ELEMENT2>
<!--
Comment for element 4
-->
<ELEMENT4>
<ELEMENT5>0534564117</ELEMENT5>
<!--
Comment for element 6
-->
<ELEMENT6>123456</ELEMENT6>
</ELEMENT4>
</ELEMENT1>
Output:
- ELEMENT1
- Comment for Element 2
- ELEMENT2
- ELEMENT3
- Comment for Element 4
- ELEMENT4
- ELEMENT5
- Comment for Element 6
- ELEMENT6
Upvotes: 0
Views: 439
Reputation: 163458
The thing you may be looking for is the xsl:comment instruction, which creates a comment in the output.
Upvotes: 0
Reputation: 25034
You'll want a stylesheet with three templates. One template matches the document root and emits the outer HTML structure, and recurs as usual on all child nodes. It wraps everything else in an HTML unordered list.
<xsl:template match="/">
<html>
<head><title>Demo document</title></head>
<body>
<ul>
<xsl:apply-templates/>
</ul>
</body>
</html>
</xsl:template>
One template handles elements by emitting a list item containing the element type name. If the element has children, it recurs on them, wrapping their output in a nested ul
element.
<xsl:template match="*">
<li>
<xsl:value-of select="name()"/>
<xsl:if test="node()">
<ul>
<xsl:apply-templates select="node()"/>
</ul>
</xsl:if>
</li>
</xsl:template>
If you don't mind emitting an empty ul
element (most browsers don't particularly care), you can do without the xsl:if
surrounding the nested ul
.
The third template matches comments and emits list items for them, wrapping the content of the comment in an i
element.
<xsl:template match="comment()">
<li><i><xsl:value-of select="."/></i></li>
</xsl:template>
You may also want a template to deal with text nodes, if the default template for text nodes is not acceptable.
Upvotes: 1