XSL为每个子节点重复父节点
对于每个子节点,我想复制父节点,以便生成的 xml 仅包含父节点的一个子节点,其他节点相同。
这是一个示例输入
<a>
<a1>header1</a1>
<a2>header2</a2>
<a3>
<a31>
<a311>line_1</a311>
<a311>line_2</a311>
</a31>
<a32>5o$</a32>
<a33>Add</a33>
</a3>
<a4>account_holder</a4>
</a>
我想要做的是 - 重复 a3 与节点 a311 出现的次数一样多。其余所有节点均保留
输出
<a>
<a1>header1</a1>
<a2>header2</a2>
<a3>
<a31>
<a311>line_1</a311>
</a31>
<a32>5o$</a32>
<a33>Add</a33>
</a3>
<a3>
<a31>
<a311>line_2</a311>
</a31>
<a32>5o$</a32>
<a33>Add</a33>
</a3>
<a4>account_holder</a4>
</a>
For each child node, I want to duplicate my parent node so that the resulting xml, contains only one child for the parent node with the other nodes being the same.
Here is a sample input
<a>
<a1>header1</a1>
<a2>header2</a2>
<a3>
<a31>
<a311>line_1</a311>
<a311>line_2</a311>
</a31>
<a32>5olt;/a32>
<a33>Add</a33>
</a3>
<a4>account_holder</a4>
</a>
What I want to do is - repeat a3 for as many times as the node a311 comes. Rest all nodes are retained
Output
<a>
<a1>header1</a1>
<a2>header2</a2>
<a3>
<a31>
<a311>line_1</a311>
</a31>
<a32>5olt;/a32>
<a33>Add</a33>
</a3>
<a3>
<a31>
<a311>line_2</a311>
</a31>
<a32>5olt;/a32>
<a33>Add</a33>
</a3>
<a4>account_holder</a4>
</a>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用“隧道参数”模式更语义化,此样式表:
输出:
编辑:处理无后代情况。
More semantic with "tunnel param" pattern, this stylesheet:
Output:
EDIT: Handling no descendants case.
以下 (XSLT 1.0) 样式表产生所需的结果:
The following (XSLT 1.0) stylesheet produces the desired result:
您没有指定 XSLT 版本。下面是一个 XSLT2 样式表,它将实现您想要的功能:
它使用身份转换来传递“无趣”元素,同时捕获
节点并应用特殊处理。如果您需要 XSLT1 解决方案,只需select="* except a31"
替换为select="*[name() != 'a31']"
mode="#default"
You didn't specify the XSLT version. Here's an XSLT2 stylesheet that will accomplish what you want:
It uses an identity transform to pass through the "uninteresting" elements while trapping the
<a3>
node and applying special handling. If you need an XSLT1 solution, merelyselect="* except a31"
withselect="*[name() != 'a31']"
mode="#default"
in the first "a3" template