How can I use XSLT to permutate a node hierarchy? -
first of all: i'm total beginner xslt
. in project synthesizing tree transformations in more abstract manner. proof of concept trying extend domain simple xslt
.
but let's @ 1 example, have several leaves in xml document, in here:
<input> <a> <b> <c>foo </c> <c>bar </c> </b> </a> <x> <y> <z>foobar </z> </y> </x> </input>
for want do, it's easier @ paths. a/b/c/foo
, a/b/c/bar
, /x/y/z/foobar
what want change hierarchy based on index in path. example want first have in order: third level, first level, second level. paths mentioned above: c/a/b/foo
, /c/a/b/bar
, /z/x/y/foobar
.
my approach looked this:
<xsl:template name="leaf"> <xsl:copy> <!-- copy attributes--> <xsl:copy-of select="@*" /> <!-- take level--> <xsl:copy select="../"/> </xsl:copy> </xsl:template>
but apparently when i'm in <copy>
cannot use "../" anymore parent element. i'm looking solution kind of transformations. either using different (my perspective on xslt narrow) or tweaking approach.
desired output:
<output> <c> <a> <b>bar</b> </a> </c> <c> <a> <b>foo</b> </a> </c> <z> <x> <y>foobar</y> <x> <z> </output>
additional information
- the input xml has depth 3 (counted without )
second example
<input> <one> <two> <three> bla </three> </two> <two> <three> blub </three> </two> </one> </input> <output> <three> <one> <second>bla</second> </one> <one> <second>blub</second> </one> </three> </output>
i'm still not sure if made clear transformation want achieve. maybe idea help: imagine totally decompose input xml: every leaf consider leaf , path leaf , transform path according rule (third, first, second).
this not elegant approach, works:
xslt 1.0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:template match="/input"> <output> <xsl:apply-templates select="*/*/*/text()"/> </output> </xsl:template> <xsl:template match="text()"> <xsl:element name="{name(ancestor::*[1])}"> <xsl:element name="{name(ancestor::*[3])}"> <xsl:element name="{name(ancestor::*[2])}"> <xsl:copy-of select="."/> </xsl:element> </xsl:element> </xsl:element> </xsl:template> </xsl:stylesheet>
Comments
Post a Comment