Di seguito un esempio di ricorsione.
Nel particolare, per ogni livello di profondità del nodo vengono stampati due spazi.
L'esempio può essere applicato a qualsiasi tipo di xml.
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:call-template name="ShowXml"/>
</xsl:template>
<xsl:template name="someIndentation">
<xsl:param name="var"/>
<xsl:choose>
<xsl:when test="$var > 0">
<xsl:text disable-output-escaping="yes"> </xsl:text>
<xsl:call-template name="someIndentation">
<xsl:with-param name="var">
<xsl:number value="number($var)-1" />
</xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="ShowXml">
<xsl:for-each select="//*">
<br/>
<xsl:call-template name="ShowOneNode"/>
</xsl:for-each>
</xsl:template>
<xsl:template name="ShowOneNode">
<xsl:call-template name="someIndentation">
<xsl:with-param name="var">
<xsl:value-of select="count(ancestor::*)"/>
</xsl:with-param>
</xsl:call-template>
<xsl:value-of select="@Name"/>
</xsl:template>
</xsl:stylesheet>
Nessun commento:
Posta un commento