Friday, July 6, 2007

XSL: A "showAll" template - unpacking XML node

This XSL template "showAll" will show the contents of a node.

Call it this way:
<xsl:call-template name="showAll">
<xsl:with-param name="showText" select="1" />
</xsl:call-template>

Where the param "showText" decides if you wish to see the text contents from
the nodes.

Here is the template:


<xsl:template name="showAll">
<xsl:param name="showText" />
<div style="padding-left: 10px; border-left: dotted 1px #e0e0e0;">
<xsl:for-each select="*">
<div style="padding-left: 10px;">
&lt;<xsl:value-of select="name(.)"/>
<xsl:for-each select="@*">
&#160;<xsl:value-of select="name(.)"/>="<xsl:value-of select="."/>"
</xsl:for-each>&gt;
<xsl:if test="$showText=1">
<xsl:value-of select="text()"/>
</xsl:if>
<xsl:if test ="count(*)>0">
<br />
<xsl:call-template name="showAll">
<xsl:with-param name="showText" select="$showText" />
</xsl:call-template>
</xsl:if>
<xsl:if test ="string-length(.)=0
and $showText!=1">
/&gt;<br />
</xsl:if>
<xsl:if test ="string-length(.)>0">
&lt;/<xsl:value-of select="name(.)" />&gt;<br />
</xsl:if>
</div>
</xsl:for-each>
</div>
</xsl:template>

No comments: