【发布时间】:2016-10-27 23:29:59
【问题描述】:
我有一些令人沮丧的扁平 XML,如下所示:
<groups>
<group id="1" depth="1" />
<group id="2" depth="2" />
<group id="3" depth="2" />
<group id="4" depth="3" />
<group id="5" depth="2" />
<group id="6" depth="3" />
</groups>
depth 的最大值似乎是 5。
我想把它变成如下所示的 XML:
<groups>
<group id="1" depth="1">
<group id="2" depth="2">
</group>
<group id="3" depth="2">
<group id="4" depth="3">
</group>
</group>
<group id="5" depth="2">
<group id="6" depth="3">
</group>
</group>
</group>
</groups>
我认为我需要使用xsl:key,然后为每个parentDepth匹配具有depth = parentDepth + 1的以下节点直到 我到达一个带有depth <= parentDepth 的节点,但我不确定如何实现这样的规则。
我想另一种合乎逻辑的方法可能是从“底部”开始并找到具有depth = childDepth - 1 的最新节点,但我不确定这是否可以在 XSLT 中实现。
我也不确定这是否可以针对每个深度递归完成?
我正在使用 XSLT 1.0。
【问题讨论】:
-
Flat to Nested structure based on attribute value using XSLT 的可能副本 还有一个 XSLT 1.0 解决方案!
标签: xml xslt recursion xpath hierarchical-data