XPath、平面层次结构和停止条件
我需要从非常糟糕的 XML 构造 Start 对象。我为一种情况制作了 SAX 解析器,但它很混乱,我想尝试一下 XPath。
我有以下 XML:
<doc>
<start/>
<a/>
<b/>
<item/>
<item/>
<item/>
<start/>
<item/>
<item/>
<item/>
<start/>
<b/>
<item/>
<item/>
<item/>
</doc>
但是我更喜欢这个文档(我没有):
<doc>
<start>
<a/>
<b/>
<item/>
<item/>
<item/>
<start/>
<start>
<item/>
<item/>
<item/>
<start/>
<start>
<b/>
<item/>
<item/>
<item/>
<start/>
</doc>
假设我有第二个“开始”节点对象(来自第一个 XML 示例)。现在我想获得直接跟随该节点的“a”和“b”元素。但是,如果我从该节点(带有以下兄弟节点)对“b”节点进行相对查询,我将在第三个起始节点下获得节点。是否可以说“找到此节点之后的节点 X,但停在节点 Y(返回 null)”?
我知道我可以使用“|”或多个查询但这不是我想要的(尽管它也可能解决我的问题)。
谢谢。
I need to construct Start objects from very bad XML. I made SAX parser for one case but it's messy and I would like to try XPath.
I have following XML:
<doc>
<start/>
<a/>
<b/>
<item/>
<item/>
<item/>
<start/>
<item/>
<item/>
<item/>
<start/>
<b/>
<item/>
<item/>
<item/>
</doc>
However I would much more like this document (which I don't have):
<doc>
<start>
<a/>
<b/>
<item/>
<item/>
<item/>
<start/>
<start>
<item/>
<item/>
<item/>
<start/>
<start>
<b/>
<item/>
<item/>
<item/>
<start/>
</doc>
Suppose please that I have 2nd "start" node object (from 1st XML example). Now I'd like to get "a" and "b" elements directly following this node. However if I make relative query from this node (with following-sibling) for "b" node I will get node under 3rd start node. Is it possible to say "find node X following this node but stop on node Y (return null)" ?
I know I can use "|" to OR multiple queries but this is not what I want (though it could possibly solve my problem too).
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您使用 XSLT 1.0,您还可以使用键
xsl:key
对相邻同级进行分组,从而简化 XPath 表达式:If you go with XSLT 1.0 you can also group adjacent siblings by using a key
xsl:key
, thus simplifying XPath expressions:假设上下文是特定的
元素,则此 XPath 将选择当前
和后续之间的所有节点;
。此 XSLT 应用该 XPath 以便按
元素对内容进行分组。Assuming the context is a particular
<start>
element, this XPath will select all of the nodes between the current<start>
and the following<start>
.This XSLT applies that XPath in order to group the content by the
<start>
elements.假设输入 XML 位于文件
in.xml
中,此 XQuery 脚本将执行您想要的操作:输出为:
Testet with XQilla 但所有其他 XQuery 处理器都应该产生相同的结果。
Assuming the input XML is in file
in.xml
this XQuery script does what you want:The output is:
Testet with XQilla but every other XQuery processor should produce the same result.