I am trying to transform where the output is returned as xpaths for each element in the xml.
Here is my sample xml
<NodeRoot>
<NodeA class="3">
<NodeB xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true">
<NodeC abc="1">103</NodeC>
<NodeD>103</NodeD>
</NodeB>
</NodeA>
<NodeA class="1">
<NodeGroup xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true">
<NodeC name="z" asc="2">103</NodeC>
</NodeGroup>
</NodeA>
</NodeRoot>
My XSL
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="utf-8" media-type="text/plain"/>
<xsl:template match="@*|text()|comment()|processing-instruction()"/>
<xsl:template match="@xsi:nil" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<xsl:template match="*">
<xsl:for-each select="ancestor-or-self::*">
<xsl:value-of select="concat('/',local-name(.))"/>
</xsl:for-each>
<xsl:text>
</xsl:text>
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
This gives me the below output
/NodeRoot
/NodeRoot/NodeA
/NodeRoot/NodeA/NodeB
/NodeRoot/NodeA/NodeB/NodeC
/NodeRoot/NodeA/NodeB/NodeD
/NodeRoot/NodeA
/NodeRoot/NodeA/NodeGroup
/NodeRoot/NodeA/NodeGroup/NodeC
The xpaths are returned but the order in which they are returned is not what I want. Currently, the order is parent followed by all its child. What I want is that the order is in a way that I get results based on depth of nodes. So first root (level 0) should be returned followed by its immediate children (level 1 elements), followed by level 2 child nodes and so on.
Expected outcome
/NodeRoot
/NodeRoot/NodeA
/NodeRoot/NodeA
/NodeRoot/NodeA/NodeB
/NodeRoot/NodeA/NodeGroup
/NodeRoot/NodeA/NodeB/NodeC
/NodeRoot/NodeA/NodeB/NodeD
/NodeRoot/NodeA/NodeGroup/NodeC
So basically
All level 0 elements All level 1 elements . . . All level n elements