我对 XPath 轴的理解是否正确? [英] Is my understanding of XPath axes correct?
问题描述
我制作了一个信息图,描绘了 XPath 中的各个轴.但是,我不确定它们是否正确.
I have made an info-graphic depicting the various axes in XPath. However, I am not sure as to whether they are correct.
我对following
、following-sibling
、preceding
和preceding-sibling
感到困惑
I get confused in following
, following-sibling
, preceding
and preceding-sibling
我的图表正确吗?
原图在这里:http://imgur.com/4ekJxca(摘自 Pro XML Development with Java)
以下是我对我感到困惑的节点的理解:descendant::
选择作为上下文节点的子节点和孙节点的节点(仅限元素和文本).
Here is my understanding of the nodes I get confused in:
descendant::
selects the nodes (element and text only) which are children and grandchildren of the context node.
following::
选择任何未被后代选择的节点(仅文本).
following::
selects any node (text only) which was not selected by descendant.
following-sibling::
上下文节点的所有兄弟".也就是说,文本和元素节点是上下文节点之后与上下文节点相同的父节点的子节点.
following-sibling::
all the 'brothers' of the context node. That is, text and element nodes which are children of the same parent as the context node, after the context node.
preceding::sibling
上下文节点的所有兄弟".也就是说,在上下文节点之前,文本和元素节点是与上下文节点相同的父节点的子节点.
preceding::sibling
all the 'brothers' of the context node. That is, text and element nodes which are children of the same parent as the context node, before the context node.
preceeding::
不沿 ancestor::
轴出现且未嵌套在任何元素节点中的所有节点(仅文本).(我确定我搞砸了)
preceeding::
all the nodes (text only) that do not appear along the ancestor::
axis and are not nested in any element node. (I am sure I screwed this up)
<?xml version="1.0" encoding="UTF-8"?>
<catalog xmlns:journal="http://www.apress.com/catalog/journal" >
<journal:journal title="XML" publisher="IBM developerWorks">
<article journal:level="Intermediate"
date="February-2003">
<title>Design XML Schemas Using UML</title>
<author>Ayesha Malik</author>
</article>
</journal:journal>
<journal title="Java Technology" publisher="IBM developerWorks">
<article level="Advanced" date="January-2004">
<title>Design service-oriented architecture
frameworks with J2EE technology</title>
<author>Naveen Balani</author>
</article>
<article level="Advanced" date="October-2003">
<title>Advance DAO Programming</title>
<author>Sean Sullivan </author>
</article>
</journal>
</catalog>
推荐答案
获得关于 preceding
和 following
轴的准确直觉的最佳方法是将 XML 想象为一个集合嵌套框或间隔,其中每个间隔从开始标记延伸到其匹配的结束标记.在这张图片中,您可以看到任何两个不同的区间 a 和 b 必须恰好处于以下关系之一:
The best way to gain accurate intuition about preceding
and following
axes is to imagine XML as a set of nested boxes or intervals, where each interval extends from the start tag to its matching end tag. In this picture you can see that any two distinct intervals a and b must be in exactly one of the following relationships:
- a 包含 b (
a/descendant::b
); - a 包含在 b (
a/ancestor::b
) 中; - a 后跟 b (
a/following::b
). - a 前面是 b (
a/preceding::b
);
- a contains b (
a/descendant::b
); - a is contained by b (
a/ancestor::b
); - a is followed by b (
a/following::b
). - a is preceded by b (
a/preceding::b
);
如果您坚持这个模型,您将永远不会怀疑 XPath 轴的语义.
If you keep to this model, you will never have a doubt in the semantics of the XPath axes.
顺便说一句,这就是为什么树模型不利于您的直觉:它没有将嵌套框"范式放在首位,因此很容易混淆.
Incidentally, this is why the tree model is bad for your intuition: it doesn't put the "nested boxes" paradigm to the forefront, so it's easy to get confused.
这篇关于我对 XPath 轴的理解是否正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!