将多个节点与嵌套节点合并
我是 XSL 的新手,但我需要 XSL 文件,将具有相同名称属性的节点合并到一个元素中 - 并且对子节点执行相同的操作 - 如果存在具有不同名称的节点,则将其作为它的一个示例XML 如下。
<?xml version="1.0" encoding="UTF-8"?>
<test>
<component name="root">
<component name="c2">
<component name="c3">
<component name="c4" />
</component>
</component>
</component>
<component name="root">
<component name="c2">
<component name="A4" />
</component>
<component name="root">
<component name="A3" />
</component>
<component name="root">
<component name="X1">
<component name="X2" />
</component>
</component>
</component>
<component name="difRoot">
</component>
</test>
期望的输出如下
<output>
<component name="root">
<component name="c2">
<component name="c3">
<component name="c4"/>
</component>
<component name="A4"/>
</component>
<component name="A3"/>
<component name="X1">
<component name="X2"/>
</component>
</component>
<component name="difRoot"/>
</output>
谢谢
I am newbie in XSL ,but i need XSL file that merge nodes with the same name attribute into one element-and the same done for the son nodes-, and if there is nodes' with different names it put as it , a example of XML is as follows .
<?xml version="1.0" encoding="UTF-8"?>
<test>
<component name="root">
<component name="c2">
<component name="c3">
<component name="c4" />
</component>
</component>
</component>
<component name="root">
<component name="c2">
<component name="A4" />
</component>
<component name="root">
<component name="A3" />
</component>
<component name="root">
<component name="X1">
<component name="X2" />
</component>
</component>
</component>
<component name="difRoot">
</component>
</test>
The desired output is as follows
<output>
<component name="root">
<component name="c2">
<component name="c3">
<component name="c4"/>
</component>
<component name="A4"/>
</component>
<component name="A3"/>
<component name="X1">
<component name="X2"/>
</component>
</component>
<component name="difRoot"/>
</output>
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此样式表:
输出:
注意:递归层次结构未完全解决,仅阻止我自己作为孩子。
更新:命名模板说明:如果
pSequence
不为空,则获取第一个节点,然后复制自身并将模板应用于所有组件
的子节点具有相同@name
的元素(也过滤具有此@name
的元素);最后调用自身过滤与已处理元素具有相同@name
的component
元素的pSequence
。因此,它逐级、逐节点过滤兄弟姐妹和子代。完全的循环预防应该是传递带有祖先名称的序列以便进行过滤。剩下的就是练习了...This stylesheet:
Output:
Note: Recursive hierarchy is not fully addressed, only prevents myself as child.
Update: Named template explanation: if
pSequence
isn't empty, take the first node then copy itself and apply templates to children of all thecomponent
elements with the same@name
(filtering those with this@name
also); lastly call itself filteringpSequence
of thecomponent
elements with the same@name
as the processed one. So, it goes level by level and node by node filtering siblings and children. Full circularity preventions should be do it passing a sequence with ancestors names in order to filter. That's left as exercise...