XPath/XQuery:选择一个根节点及其不带子节点的属性
我有一个 xml:
<Customer id="">
<Name />
<Address />
</Customer>
我只想选择一个具有其属性的根节点,而不选择其子节点:
<Customer id=""/ >
使用 XPath 可以实现这样的操作吗?
I have an xml:
<Customer id="">
<Name />
<Address />
</Customer>
I would like to select ONLY a root node with its attributes without its child nodes:
<Customer id=""/ >
Is such thing possible with XPath?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不,这在 XPath 中是不可能的。
您无法选择没有子节点的节点,因为没有子节点,它将是一个不同的节点,因此您不会从原始文档中选择节点。
要创建您想要的输出,您需要使用一种允许您创建新节点的语言,因此您无法在 XPath 中执行此操作。您可以使用 XQuery 创建新节点,这应该可以工作:
No, this is not possible in XPath.
You can't select the node without its children, because without its children it would be a different node, hence you would not be selecting a node from the original document.
To create the output you want you need to use a language which allows you to create new nodes, so you can't do it in XPath. You can use XQuery to create new nodes, this should work:
XPath 不会更改任何源 XML 文档,这是设计使然。
要从现有 XML 文档生成新的 XML 文档,需要进行转换。
XSLT 专门用于将一组树(包括 XML 文档)转换为结果树。
此转换:
应用于提供的 XML 文档时:
产生所需的正确结果:
XPath does not alter any source XML document and this is by design.
To produce a new XML document from an existing one, transformation is needed.
XSLT has been especially designed for transforming a set of trees (including XML documents) into result trees.
This transformation:
when applied on the provided XML document:
produces the wanted, correct result:
此 XQuery 表达式:
输出:
This XQuery expression:
Output: