使用 XPath/XSLT 转换通用 XML 文档
执行以下操作的最佳(最好是最有效)方法是什么:
考虑我有一个这样的 XML 文档:
<comments question_id="123">
<comment id="1">
This is the first comment
</comment>
<comment id="2">
This is the second comment
</comment>
</comments>
现在,假设我指定了每个数据块的“路径”,即在本例中:
path : /comments/comment
我想将文档分解为 n 个子部分,在本例中为 2:
<comments question_id="123">
<comment id="1">
This is the first comment
</comment>
</comments>
<comments question_id="123">
<comment id="2">
This is the second comment
</comment>
</comments>
所以,本质上,我想做的是获取由“/comments/comment”
,同时还保留所有“外部”父节点数据。
编辑:
注意:这需要是动态的或通用的。即上面的数据xml只是一个例子。我希望能够将任何 xml 文档转换为这种效果,给定代表每个数据节点的“路径”。剩下的就是 xml 的外部主体了。
What would be the best (preferably most efficient) method to do the following:
Consider I have an XML document as such:
<comments question_id="123">
<comment id="1">
This is the first comment
</comment>
<comment id="2">
This is the second comment
</comment>
</comments>
Now, given that I specify the “path” to each data-block, i.e. in this case:
path: /comments/comment
I would like to break up the document into n-number of sub-parts, in this case 2:
<comments question_id="123">
<comment id="1">
This is the first comment
</comment>
</comments>
<comments question_id="123">
<comment id="2">
This is the second comment
</comment>
</comments>
So, essentially, what I am trying to do is get each node produced by “/comments/comment”
, but also retain all “outer” parent nodes data.
EDIT:
Note: this needs to be dynamic, or generic. I.e. the above data xml is just an example. I want to be able to transform any xml document to this effect, given a “path” representing each data-node. And the rest is the outer body of the xml.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可以在我对此问题的回答中找到通用的“粉碎”解决方案:https://stackoverflow.com /a/8597577/36305
这是完整的转换:
当此转换应用于提供的 XML 文档时:
想要的正确结果是制作:
A generic "shredding" solution can be found in my answer to this question: https://stackoverflow.com/a/8597577/36305
Here is the complete transformation:
When this transformation is applied on the provided XML document:
the wanted, correct result is produced:
如果您确实想使用 Xslt 转换您的 Xml,它应该如下所示:
...将为您提供所需的输出:
If you actuall want to transform your Xml using Xslt it should look like this:
... will give you the desired output: