Reputation: 41
I am trying to write a xsl which will return xpaths of element and its immediate children
Sample XML
<level0>
<level1>
<level2>
<level3>
<level4/>
</level3>
<level3/>
</level2>
</level1>
</level0>
For example, if I want to return the xpaths of level2
then it should return
/level0/level1/level2
/level0/level1/level2/level3
/level0/level1/level2/level3
My XSL
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" media-type="text/plain"/>
<xsl:template match="@*|comment()|text()|processing-instruction()"/>
<xsl:template match="//level2|//level2/*">
<xsl:for-each select="ancestor-or-self::*">
<xsl:value-of select="concat('/',name(.))"/>
</xsl:for-each>
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
In this xsl, I am trying to match the template for //level2|//level2/*
xpath (which should return the desired result). However, I am only getting
/level0/level1/level2
How do I get the desired result?
Upvotes: 2
Views: 628
Reputation: 3435
Just add <xsl:apply-templates/>
after <xsl:text>
</xsl:text>
<xsl:template match="//level2|//level2/*">
<xsl:for-each select="ancestor-or-self::*">
<xsl:value-of select="concat('/',name(.))"/>
</xsl:for-each>
<xsl:text>
</xsl:text>
<xsl:apply-templates/>
</xsl:template>
Output
/level0/level1/level2
/level0/level1/level2/level3
/level0/level1/level2/level3
Upvotes: 3