`
favey
  • 浏览: 121598 次
  • 性别: Icon_minigender_1
  • 来自: 东莞
社区版块
存档分类
最新评论

XPath教程——实例介绍<二>

 
阅读更多

12、descendant (后代)轴包含上下文节点的后代,一个后代是指子节点或者子节点的子节点等等, 因此descendant轴不会包含属性和命名空间节点

(1)."/descendant::*"表示选择文档根元素的所有后代.即所有的元素被选择:

     <AAA>
          <
BBB>
               <
DDD>
                    <
CCC>
                         <
DDD/>
                         <
EEE/>
                    </
CCC>
               </
DDD>
          </
BBB>
          <
CCC>
               <
DDD>
                    <
EEE>
                         <
DDD>
                              <
FFF/>
                         </
DDD>
                    </
EEE>
               </
DDD>
          </
CCC>
     </
AAA>

(2)."/AAA/BBB/descendant::*"表示选择/AAA/BBB的所有后代元素

    <AAA>
          <BBB>
               <DDD>
                    <
CCC>
                         <
DDD/>
                         <
EEE/>
                    </
CCC>
               </
DDD>
          </BBB>
          <CCC>
               <DDD>
                    <EEE>
                         <DDD>
                              <FFF/>
                         </DDD>
                    </EEE>
               </DDD>
          </CCC>
     </AAA>

(3)."//CCC/descendant::*"表示选择CCC元素的所有子元素:

     <AAA>
          <BBB>
               <DDD>
                    <CCC>
                         <DDD/>
                         <
EEE/>
                    </CCC>
               </DDD>
          </BBB>
          <CCC>
               <DDD>
                    <
EEE>
                         <
DDD>
                              <
FFF/>
                         </
DDD>
                    </
EEE>
               </
DDD>
          </CCC>
     </AAA>

(4)."//CCC/descendant::DDD"表示选择CCC元素下的所有DDD元素:

     <AAA>
          <BBB>
               <DDD>
                    <CCC>
                         <DDD/>
                         <EEE/>
                    </CCC>
               </DDD>
          </BBB>
          <CCC>
               <DDD>
                    <EEE>
                         <DDD>
                              <FFF/>
                         </DDD>
                    </EEE>
               </DDD>
          </CCC>
     </AAA>

13、parent轴(axis)包含上下文节点的父节点, 如果有父节点的话

(1)."//DDD/parent::*"表示选择DDD元素的所有父节点:

     <AAA>
          <BBB>
               <DDD>
                    <CCC>
                         <DDD/>
                         <EEE/>
                    </CCC>
               </DDD>
          </BBB>
          <CCC>
               <DDD>
                    <EEE>
                         <DDD>
                              <FFF/>
                         </DDD>
                    </EEE>
               </DDD>
          </CCC>
     </AAA>

14、ancestor轴(axis)包含上下节点的祖先节点, 该祖先节点由其上下文节点的父节点以及父节点的父节点等等诸如此类的节点构成,所以ancestor轴总是包含有根节点,除非上下文节点就是根节点本身

(1)."/AAA/BBB/DDD/CCC/EEE/ancestor::*"表示选择一个绝对路径上的所有节点:

   <AAA>
          <
BBB>
               <
DDD>
                    <
CCC>
                         <DDD/>
                         <EEE/>
                    </CCC>
               </
DDD>
          </
BBB>
          <CCC>
               <DDD>
                    <EEE>
                         <DDD>
                              <FFF/>
                         </DDD>
                    </EEE>
               </DDD>
          </CCC>
     </AAA>

(2)."//FFF/ancestor::*"表示选择FFF元素的所有祖先节点:

     <AAA>
          <BBB>
               <DDD>
                    <CCC>
                         <DDD/>
                         <EEE/>
                    </CCC>
               </DDD>
          </BBB>
          <CCC>
               <
DDD>
                    <
EEE>
                         <
DDD>
                              <FFF/>
                         </DDD>
                    </
EEE>
               </
DDD>
          </
CCC>
     </
AAA>

15、following-sibling轴(axis)包含上下文节点之后的所有兄弟节点

(1)."/AAA/BBB/following-sibling::*":

     <AAA>
          <BBB>
               <CCC/>
               <DDD/>
          </BBB>
          <XXX>
               <DDD>
                    <EEE/>
                    <DDD/>
                    <CCC/>
                    <FFF/>
                    <FFF>
                         <GGG/>
                    </FFF>
               </DDD>
          </XXX>
          <CCC>
               <DDD/>
          </CCC>
     </AAA>

(2)."//CCC/following-sibling::*":

     <AAA>
          <BBB>
               <CCC/>
               <DDD/>
          </BBB>
          <XXX>
               <DDD>
                    <EEE/>
                    <DDD/>
                    <CCC/>
                    <FFF/>
                    <
FFF>
                         <GGG/>
                    </FFF>
               </DDD>
          </XXX>
          <CCC>
               <DDD/>
          </CCC>
     </AAA>

16、preceding-sibling 轴(axis)包含上下文节点之前的所有兄弟节点

(1)."//CCC/preceding-sibling::*"

     <AAA>
          <BBB>
               <CCC/>
               <DDD/>
          </BBB>
          <
XXX >

               <DDD>
                    <EEE/>
                    <
DDD/>
                    <CCC/>
                    <FFF/>
                    <FFF>
                         <GGG/>
                    </FFF>
               </DDD>
          </XXX>
          <CCC>
               <DDD/>
          </CCC>
     </AAA>

17、following轴(axis)包含同一文档中按文档顺序位于上下文节点之后的所有节点, 除了祖先节点,属性节点和命名空间节点

(1)."//ZZZ/following::*":

     <AAA>
          <BBB>
               <CCC/>
               <ZZZ>
                    <DDD/>
                    <DDD>
                         <EEE/>
                    </DDD>
               </ZZZ>
               <FFF>
                    <
GGG/>
               </
FFF>
          </BBB>
          <XXX>
               <
DDD>
                    <
EEE/>
                    <
DDD/>
                    <
CCC/>
                    <
FFF/>
                    <
FFF>
                         <
GGG/>
                    </
FFF>
               </
DDD>
          </
XXX>
          <
CCC>
               <
DDD/>
          </
CCC>
     </AAA>

18、descendant-or-self 轴(axis)包含上下文节点本身和该节点的后代节点

(1)."/AAA/XXX/descendant-or-self::*":

     <AAA>
          <BBB>
               <CCC/>
               <ZZZ>
                    <DDD/>
               </ZZZ>
          </BBB>
          <XXX>
               <
DDD>
                    <
EEE/>
                    <
DDD/>
                    <
CCC/>
                    <
FFF/>
                    <
FFF>
                         <
GGG/>
                    </
FFF>
               </
DDD>
          </
XXX>
          <CCC>
               <DDD/>
          </CCC>
     </AAA>

20、ancestor-or-self 轴(axis)包含上下文节点本身和该节点的祖先节点

(1)."/AAA/XXX/DDD/EEE/ancestor-or-self::*":

     <AAA>
          <BBB>
               <CCC/>
               <ZZZ>
                    <DDD/>
               </ZZZ>
          </BBB>
          <XXX>
               <DDD>
                    <EEE/>
                    <DDD/>
                    <CCC/>
                    <FFF/>
                    <FFF>
                         <GGG/>
                    </FFF>
               </DDD>
          </
XXX>
          <CCC>
               <DDD/>
          </CCC>
     </AAA>

21、ancestor, descendant, following, preceding 和self轴(axis)分割了XML文档(忽略属性节点和命名空间节点), 不能交迭, 而一起使用则包含所有节点

(1)."//GGG/ancestor::*":
     <AAA>
          <BBB>
               <CCC/>
               <ZZZ/>
          </BBB>
          <XXX>
               <DDD>
                    <EEE/>
                    <FFF>
                         <HHH/>
                         <GGG>
                              <JJJ>
                                   <QQQ/>
                              </JJJ>
                              <JJJ/>
                         </GGG>
                         <HHH/>
                    </FFF>
               </DDD>
          </XXX>
          <CCC>
               <DDD/>
          </CCC>
     </AAA>

(2)."//GGG/descendant::*":

    <AAA>
          <BBB>
               <CCC/>
               <ZZZ/>
          </BBB>
          <XXX>
               <DDD>
                    <EEE/>
                    <FFF>
                         <HHH/>
                         <GGG>
                              <JJJ>
                                   <
QQQ/>
                              </
JJJ>
                              <JJJ/>
                         </GGG>
                         <HHH/>
                    </FFF>
               </DDD>
          </XXX>
          <CCC>
               <DDD/>
          </CCC>
     </AAA>

(3)."//GGG/following::*":

     <AAA>
          <BBB>
               <CCC/>
               <ZZZ/>
          </BBB>
          <XXX>
               <DDD>
                    <EEE/>
                    <FFF>
                         <HHH/>
                         <GGG>
                              <JJJ>
                                   <QQQ/>
                              </JJJ>
                              <JJJ/>
                         </GGG>
                         <HHH/>
                    </FFF>
               </DDD>
          </XXX>
          <CCC>
               <
DDD/>
          </
CCC>
     </AAA>

(4)."//GGG/preceding::*":

     <AAA>
          <BBB>
               <
CCC/>
               <
ZZZ/>
          </
BBB>
          <XXX>
               <DDD>
                    <EEE/>
                    <FFF>
                         <
HHH/>
                         <GGG>
                              <JJJ>
                                   <QQQ/>
                              </JJJ>
                              <JJJ/>
                         </GGG>
                         <HHH/>
                    </
FFF>
               </DDD>
          </XXX>
          <CCC>
               <DDD/>
          </CCC>
     </AAA>

(5)."//GGG/self::*":

     <AAA>
          <BBB>
               <CCC/>
               <ZZZ/>
          </BBB>
          <XXX>
               <DDD>
                    <EEE/>
                    <FFF>
                         <HHH/>
                         <GGG>
                              <JJJ>
                                   <QQQ/>
                              </JJJ>
                              <JJJ/>
                         </GGG>
                         <HHH/>
                    </FFF>
               </DDD>
          </XXX>
          <CCC>
               <DDD/>
          </CCC>
     </AAA>

(6)."//GGG/ancestor::* | //GGG/descendant::* | //GGG/following::* | //GGG/preceding::* | //GGG/self::*":

    <AAA>
          <
BBB>
               <
CCC/>
               <
ZZZ/>
          </
BBB>
          <
XXX>
               <
DDD>
                    <
EEE/>
                    <
FFF>
                         <
HHH/>
                         <
GGG>
                              <
JJJ>
                                   <
QQQ/>
                              </
JJJ>
                              <
JJJ/>
                         </
GGG>
                         <
HHH/>
                    </
FFF>
               </
DDD>
          </
XXX>
          <
CCC>
               <
DDD/>
          </
CCC>
     </
AAA>

22、div运算符做浮点除法运算, mod运算符做求余运算, floor函数返回不大于参数的最大整数(趋近于正无穷), ceiling返回不小于参数的最小整数(趋近于负无穷)

(1)."//BBB[position() mod 2 = 0 ]"表示选择偶数位置的BBB元素:

 <AAA>
          <BBB/>
          <BBB/>
          <BBB/>
          <BBB/>
          <BBB/>
          <BBB/>
          <BBB/>
          <BBB/>
          <CCC/>
          <CCC/>
          <CCC/>
     </AAA>

(2)."//BBB[ position() = floor(last() div 2 + 0.5) or position() = ceiling(last() div 2 + 0.5) ]"表示选择中间的BBB元素:

     <AAA>
          <BBB/>
          <BBB/>
          <BBB/>
          <BBB/>
          <
BBB/>
          <BBB/>
          <BBB/>
          <BBB/>
          <CCC/>
          <CCC/>
          <CCC/>
     </AAA>

(3)."//CCC[ position() = floor(last() div 2 + 0.5) or position() = ceiling(last() div 2 + 0.5) ]"表示选中中间的CCC元素:

     <AAA>
          <BBB/>
          <BBB/>
          <BBB/>
          <BBB/>
          <BBB/>
          <BBB/>
          <BBB/>
          <BBB/>
          <CCC/>
          <CCC/>
          <CCC/>
     </AAA>

参考:http://www.zvon.org/xxl/XPathTutorial/General_chi/examples.html

分享到:
评论

相关推荐

    C#XML入门经典 C#编程人员必备的XML技能.part2

    在.NET中实现DOM&lt;br&gt;&lt;br&gt;6.1 文档对象模型&lt;br&gt;6.1.1 文档对象模型与流...实例&lt;br&gt;6.3.4 编辑XML文档&lt;br&gt;6.4 案例分析&lt;br&gt;6.4.1 体系结构&lt;br&gt;6.4.2 应用程序详细信息&lt;br&gt;6.4.3 加载用户联系人&lt;br&gt;6.4.4 搜索联系人&lt;br...

    digester jar包大全

    例如,如果XML中有`&lt;server&gt;&lt;database&gt;`这样的结构,我们可以设置规则,让Digester在遇到`&lt;database&gt;`元素时创建一个新的数据库对象,并将其添加到服务器对象中。 Digester的核心概念包括: 1. **Rules**: 规则...

    JDOM中XPath.selectNodes()和XPath.selectSingleNode()用法

    例如,如果你有一个XML文档,并且想要找到所有的`&lt;item&gt;`元素,你可以使用`XPath.selectNodes("/root/item")`。这个方法对处理大量结果非常有用,因为返回的结果集可以方便地进行迭代。 2. `XPath.selectSingleNode...

    商业编程-源码-XML同学录系统源码.zip

    &lt;name&gt;张三&lt;/name&gt; &lt;id&gt;001&lt;/id&gt; &lt;class&gt;1班&lt;/class&gt; &lt;contact&gt; &lt;email&gt;zhangsan@example.com&lt;/email&gt; &lt;phone&gt;1234567890&lt;/phone&gt; &lt;/contact&gt; &lt;/student&gt; ``` 这样的结构使得数据清晰易读,也方便通过XML...

    XML数据操作

    doc.LoadXml("&lt;INFO&gt;&lt;agentpickID&gt;123&lt;/agentpickID&gt;&lt;agentcorpid&gt;456&lt;/agentcorpid&gt;&lt;TotalNum&gt;789&lt;/TotalNum&gt;&lt;ROW&gt;&lt;agentpickitemID&gt;111&lt;/agentpickitemID&gt;&lt;CardtypeID&gt;222&lt;/CardtypeID&gt;&lt;Typename&gt;Type1&lt;/Typename&gt;...

    xpath表达式完整实例

    ### Xpath表达式完整实例知识点解析 #### 一、Xpath基本概念 Xpath是一种用于在XML文档中查询和定位节点的语言。它提供了一种简洁有效的方式来选择文档中的元素和属性。 - **节点类型**: - **Element(元素节点...

    XML初学进阶——XML基础帮手

    &gt;`)、元素(如`&lt;element&gt;`和`&lt;/element&gt;`)、属性、文本内容、注释和处理指令等部分组成。文档必须符合一定的语法规则,例如所有元素都必须有相应的闭合标签。 3. **元素与属性**:XML的主体是元素,元素通过标签...

    XML实例.rar

    在描述的“SM3中xml实例代码”中,SM3可能是指中国的国密算法标准——SM3散列函数,它通常用于数字签名和消息认证码的生成。XML实例与SM3的结合,可能涉及到将SM3计算的结果以XML格式存储或传输。 XML的基本结构...

    Dom4jProject.zip

    List&lt;Node&gt; nodes = xpath.selectNodes(document); for (Node node : nodes) { // 处理查询结果 } ``` 5. 修改XML文档:通过`Element`接口提供的方法,可以对元素进行添加、删除和修改操作。 ```java Element new...

    XML实用教程课程2006

    10. **XML实例**:本教程中可能包含的PPT可能讲解了XML的基本元素创建、文档结构设计以及如何编写简单的XML文档。部分代码可能涉及XML的解析、数据提取以及如何使用XPath和XSLT进行操作。 通过这个“XML实用教程...

    轻松搞定XML语言(要下的快)

    例如,`&lt;book&gt;`可以表示一本书,而`&lt;title&gt;`和`&lt;author&gt;`则分别表示书名和作者。属性则为元素提供额外的信息,如`&lt;book id="123"&gt;`中的"id"属性。标签用于包围元素内容,比如`&lt;title&gt;编程之道&lt;/title&gt;`。 XML的规范...

    《无废话XML》——两只老虎

    - 所有元素必须正确闭合,例如`&lt;element&gt;&lt;/element&gt;`或者`&lt;element/&gt;`。 - 属性值必须用引号括起来,例如`&lt;element name="value"&gt;`。 #### 2.2 注释与处理指令 - **注释**:XML文档中的注释用`&lt;!-- --&gt;`标记,例如...

    xStream完美转换XML、JSON

    &lt;name&gt;John Doe&lt;/name&gt; &lt;age&gt;30&lt;/age&gt; &lt;/person&gt; ``` xStream也支持自定义转换规则,允许开发者指定特定字段的转换逻辑,甚至创建完全自定义的XML或JSON结构。 在进行XML到JSON的转换时,xStream使用了JSONPath...

    C#操作xml文件生成实例源码

    &lt;Child2&gt;Value2&lt;/Child2&gt; &lt;/Root&gt; ``` 在实际应用中,你可能还需要处理更复杂的XML结构,例如包含属性、命名空间或处理指令的节点。C#提供了丰富的API来支持这些操作: - 使用`XmlAttribute`类来添加属性,如`...

    MLDN+李兴华+Java+Web开发实战经典.part3.rar )

    5.2.2、第二种Scriptlet:&lt;%!%&gt; 5.2.3、第三种Scriptlet:&lt;%=%&gt; 5.3、Scriptlet标签 5.4、page指令 5.4.1、设置页面的MIME 5.4.2、设置文件编码 5.4.3、错误页的设置 5.4.4、数据库连接操作 5.5、包含指令 ...

    XSLT入门教程

    第一章“XSLT的概念”会介绍XSLT的基本原理,包括它的主要作用——数据转换,以及与XPath的关系。XPath是XML路径语言,用于在XML文档中查找信息,它是XSLT中的重要组成部分。这一章还会解释XSLT的模板和模式匹配机制...

    网站建设——XML初级教程

    元素是XML的基本构建块,用尖括号(&lt;&gt;)包围。元素可以有属性,提供额外的信息。内容可以用字符数据(CDATA)或实体引用来表示。 2. **命名规则**:XML的元素、属性、命名空间等都遵循一定的命名规则,如名称必须以...

Global site tag (gtag.js) - Google Analytics