我如何使用键来转换它
我可以在不使用键的情况下编写一个非常糟糕的 xslt,但它非常慢且混乱。有谁知道我如何将以下 XML 文件干净地转换为预期结果?谢谢!
输入:
<testExecution>
<test>
<test.1/>
<test.2/>
<test.3/>
</test>
<comment>
<comment.1/>
<comment.2/>
<comment.3/>
</comment>
<executor>
<executor.1/>
<executor.2/>
<executor.3/>
</executor>
<comment>
<comment.1/>
<comment.2/>
<comment.3/>
</comment>
<comment>
<comment.1/>
<comment.2/>
<comment.3/>
</comment>
<invoker>
<invoker.1/>
<invoker.2/>
<invoker.3/>
</invoker>
<recipient>
<recipient.1/>
<recipient.2/>
<recipient.3/>
</recipient>
<comment>
<comment.1/>
<comment.2/>
<comment.3/>
</comment>
<comment>
<comment.1/>
<comment.2/>
<comment.3/>
</comment>
</testExecution>
结果:
<testExecution>
<test>
<test.1/>
<test.2/>
<test.3/>
</test>
<comment>
<comment.1/>
<comment.2/>
<comment.3/>
</comment>
<people>
<importantPeople>
<executor>
<executor.1/>
<executor.2/>
<executor.3/>
</executor>
<comment>
<comment.1/>
<comment.2/>
<comment.3/>
</comment>
<comment>
<comment.1/>
<comment.2/>
<comment.3/>
</comment>
</importantPeople>
<invoker>
<invoker.1/>
<invoker.2/>
<invoker.3/>
</invoker>
</people>
<recipient>
<recipient.1/>
<recipient.2/>
<recipient.3/>
</recipient>
<comment>
<comment.1/>
<comment.2/>
<comment.3/>
</comment>
<comment>
<comment.1/>
<comment.2/>
<comment.3/>
</comment>
</testExecution>
I'm able to write a horribly bad xslt without using keys but it's quite slow and messy. Does anyone know how i would transform the following XML file into the expected result cleanly? Thanks!
input:
<testExecution>
<test>
<test.1/>
<test.2/>
<test.3/>
</test>
<comment>
<comment.1/>
<comment.2/>
<comment.3/>
</comment>
<executor>
<executor.1/>
<executor.2/>
<executor.3/>
</executor>
<comment>
<comment.1/>
<comment.2/>
<comment.3/>
</comment>
<comment>
<comment.1/>
<comment.2/>
<comment.3/>
</comment>
<invoker>
<invoker.1/>
<invoker.2/>
<invoker.3/>
</invoker>
<recipient>
<recipient.1/>
<recipient.2/>
<recipient.3/>
</recipient>
<comment>
<comment.1/>
<comment.2/>
<comment.3/>
</comment>
<comment>
<comment.1/>
<comment.2/>
<comment.3/>
</comment>
</testExecution>
result:
<testExecution>
<test>
<test.1/>
<test.2/>
<test.3/>
</test>
<comment>
<comment.1/>
<comment.2/>
<comment.3/>
</comment>
<people>
<importantPeople>
<executor>
<executor.1/>
<executor.2/>
<executor.3/>
</executor>
<comment>
<comment.1/>
<comment.2/>
<comment.3/>
</comment>
<comment>
<comment.1/>
<comment.2/>
<comment.3/>
</comment>
</importantPeople>
<invoker>
<invoker.1/>
<invoker.2/>
<invoker.3/>
</invoker>
</people>
<recipient>
<recipient.1/>
<recipient.2/>
<recipient.3/>
</recipient>
<comment>
<comment.1/>
<comment.2/>
<comment.3/>
</comment>
<comment>
<comment.1/>
<comment.2/>
<comment.3/>
</comment>
</testExecution>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此样式表:
输出:
注意:细粒度遍历。 “仅用于此测试”的键如下。 “关闭此级别”的空模板。推送风格的模式“继续处理”。它可以是完整的“拉式”匹配“标记之前的上一个”。
This stylesheet:
Output:
Note: Fine grained traversal. Keys for "just for this test" followings. Empty templates for "close this level". Mode for push style "keep processing". It could be full "pull style" matching "the previus before the mark".
此转换使用并覆盖身份规则。使用
key()
函数检索紧随executor
的注释。紧随executor
之后的executor
、invoker
和comment
会以名为 <代码>人。在通常的匿名模式中,它们与空模板匹配,以避免再次复制它们:应用于提供的 XML 文档时:
产生所需的正确结果:
This transformation uses and overrides the identity rule. Comments immediately following
executor
are retrieved using thekey()
function.executor
,invoker
andcomment
immediately followingexecutor
are copied in special mode namedpeople
. In the usual, anonymous mode these are matched by an empty template to avoid copying them a second time:when applied on the provided XML document:
produces the wanted, correct result: