首先:我是一个完全的初学者XSLT
。在一个项目中,我们正在以更抽象的方式综合树转换。为了概念证明,我试图将域扩展到简单的XSLT
.
但让我们看一个例子,我的 XML 文档中有几页,如下所示:
<input>
<a>
<b>
<c>Foo </c>
<c>Bar </c>
</b>
</a>
<x>
<y>
<z>Foobar </z>
</y>
</x>
</input>
对于我想做的事情,查看路径更容易。a/b/c/Foo
, a/b/c/Bar
, /x/y/z/Foobar
我想要做的是根据路径中的索引更改层次结构。例如,我想首先按顺序排列:第三级、第一级、第二级。对于上面提到的路径:c/a/b/Foo
, /c/a/b/Bar
, /z/x/y/Foobar
.
我的方法如下:
<xsl:template name="leaf">
<xsl:copy>
<!-- copy attributes-->
<xsl:copy-of select="@*" />
<!-- take another level-->
<xsl:copy select="../"/>
</xsl:copy>
</xsl:template>
但显然当我处于<copy>
我不能再使用“../”来获取父元素。我正在寻找任何解决方案来实现这种转变。要么使用完全不同的方法(我对 XSLT 的看法非常狭窄),要么调整我的方法。
期望的输出:
<output>
<c>
<a>
<b>Bar</b>
</a>
</c>
<c>
<a>
<b>Foo</b>
</a>
</c>
<z>
<x>
<y>Foobar</y>
<x>
<z>
</output>
附加信息
第二个例子
<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>
我仍然不确定我是否明确了我想要实现的转变。也许这个想法会有所帮助:想象一下,我完全分解了输入 XML:对于每个叶子,考虑叶子本身以及到叶子的路径,然后根据规则(第三、第一、第二)转换路径。