xml - XSLT: Match N-level node -
need match node id. (say a2-2)
xml:
<node id="a" title="title a"> <node id="a1" title="title a1" /> <node id="a2" title="title a2" > <node id="a2-1" title="title a2-1" /> <node id="a2-2" title="title a2-2" /> <node id="a2-3" title="title a2-3" /> </node> <node id="a3" title="title a3" /> </node> <node id="b"> <node id="b1" title="title b1" /> </node>
current solution:
<xsl:apply-templates select="node" mode="all"> <xsl:with-param name="id" select="'a2-2'" /> </xsl:apply-templates> <xsl:template match="node" mode="all"> <xsl:param name="id" /> <xsl:choose> <xsl:when test="@id=$id"> <xsl:apply-templates mode="match" select="." /> </xsl:when> <xsl:otherwise> <xsl:apply-templates mode="all" select="node"> <xsl:with-param name="id" select="$id" /> </xsl:apply-templates> </xsl:otherwise> </xsl:choose> </xsl:template> <xsl:template match="node" mode="match"> <h3><xsl:value-of select="@title"/>.</h3> </xsl:template>
problem:
above solution seem bulky quite common problem.
over-complicating? there simpler solution.
this solution twice shorter , simpler (a single template , no modes):
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:param name="pid" select="'a2-2'"/> <xsl:template match="node"> <xsl:choose> <xsl:when test="@id = $pid"> <h3><xsl:value-of select="@title"/>.</h3> </xsl:when> <xsl:otherwise><xsl:apply-templates/></xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet>
Comments
Post a Comment