Hello,
I am looking for some hours for a solution for a very simple problem:
Copy all leaves from a tree with all elements on the way to the root.
I tried:
java -jar saxon9he.jar -s:in.xml -xsl:test.xsl -o:out.xml
in.xml
Code:
<nodes>
<node id="a1">
<node id="b1">
<node id="c1"/>
<node id="c2"/>
</node>
</node>
<node id="a2"/>
</nodes>
test.xsl
Code:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- copy elements without children including all the elements within the path to the root (i.e. full path) -->
<xsl:template match="/">
<nodes>
<xsl:apply-templates />
</nodes>
</xsl:template>
<xsl:template match="node[not(descendant::node)]">
<xsl:for-each select="ancestor-or-self::*[count(ancestor::*)>0]">
<node id="{@id}" />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
out.xml should be a simple copy of the "full path" to the elements, that do not have any children
Code:
<nodes>
<node id="a1">
<node id="b1">
<node id="c1"/>
</node>
</node>
<node id="a1">
<node id="b1">
<node id="c2"/>
</node>
</node>
<node id="a2"/>
</nodes>
with the given stylesheet the xml transforms to a list (not surprisingly).
Code:
<nodes>
<node id="a1"/><node id="b1"/><node id="c1"/>
<node id="a1"/><node id="b1"/><node id="c2"/>
<node id="a2"/>
</nodes>
Any hints how to copy all ancestors of a given node?
Best regards
Chrsitian