C# xpath

时间:2022-09-06 20:04:21

XPath最通俗的教程(ZZ)

  以下是本人找到的最完整最易懂的XPath教程,不敢私藏,拿出来与大家分享。帮我点旁边的google广告呀。

实例 1
基本的XPath语法类似于在一个文件系统中定位文件,如果路径以斜线 / 开始, 那么该路径就表示到一个元素的绝对路径

/AAA

选择根元素AAA


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

/AAA/CCC

选择AAA的所有CCC子元素


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

/AAA/DDD/BBB

选择AAA的子元素DDD的所有子元素


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

实例 2

如果路径以双斜线 // 开头, 则表示选择文档中所有满足双斜线//之后规则的元素(无论层级关系)

//BBB

选择所有BBB元素


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

//DDD/BBB

选择所有父元素是DDD的BBB元素


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

实例 3

星号 * 表示选择所有由星号之前的路径所定位的元素

/AAA/CCC/DDD/*

选择所有路径依附于/AAA/CCC/DDD的元素


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

/*/*/*/BBB

选择所有的有3个祖先元素的BBB元素


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

//*

选择所有元素


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

实例 4

方块号里的表达式可以进一步的指定元素, 其中数字表示元素在选择集里的位置, 而last()函数则表示选择集中的最后一个元素.

/AAA/BBB[1]

选择AAA的第一个BBB子元素


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

/AAA/BBB[last()]

选择AAA的最后一个BBB子元素


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

实例 5

//@id

选择所有的id属性


     <AAA> 
          <BBB
 id = "b1"/> 
          <BBB
 id = "b2"/> 
          <BBB name = "bbb"/> 
          <BBB/> 
     </AAA>

//BBB[@id]

选择有id属性的BBB元素


     <AAA> 
          <
BBB id = "b1"/> 
          <
BBB id = "b2"/> 
          <BBB name = "bbb"/> 
          <BBB/> 
     </AAA>

//BBB[@name]

选择有name属性的BBB元素


     <AAA> 
          <BBB id = "b1"/> 
          <BBB id = "b2"/> 
          <
BBB name = "bbb"/> 
          <BBB/> 
     </AAA>

//BBB[@*]

选择有任意属性的BBB元素


     <AAA> 
          <
BBB id = "b1"/> 
          <
BBB id = "b2"/> 
          <
BBB name = "bbb"/> 
          <BBB/> 
     </AAA>

//BBB[not(@*)]

选择没有属性的BBB元素


     <AAA> 
          <BBB id = "b1"/> 
          <BBB id = "b2"/> 
          <BBB name = "bbb"/> 
          <
BBB/> 
     </AAA>

实例 6

属性的值可以被用来作为选择的准则, normalize-space函数删除了前部和尾部的空格, 并且把连续的空格串替换为一个单一的空格

//BBB[@id='b1']

选择含有属性id且其值为'b1'的BBB元素


     <AAA> 
          <
BBB id = "b1"/> 
          <BBB name = " bbb "/> 
          <BBB name = "bbb"/> 
     </AAA>

//BBB[@name='bbb']

选择含有属性name且其值为'bbb'的BBB元素


     <AAA> 
          <BBB id = "b1"/> 
          <BBB name = " bbb "/> 
          <
BBB name = "bbb"/> 
     </AAA>

//BBB[normalize-space(@name)='bbb']

选择含有属性name且其值(在用normalize-space函数去掉前后空格后)为'bbb'的BBB元素


     <AAA> 
          <BBB id = "b1"/> 
          <
BBB name = " bbb "/> 
          <
BBB name = "bbb"/> 
     </AAA>

实例 7

count()函数可以计数所选元素的个数

//*[count(BBB)=2]

选择含有2个BBB子元素的元素


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

//*[count(*)=2]

选择含有2个子元素的元素


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

//*[count(*)=3]

选择含有3个子元素的元素


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

实例 8

name()函数返回元素的名称, start-with()函数在该函数的第一个参数字符串是以第二个参数字符开始的情况返回true, contains()函数当其第一个字符串参数包含有第二个字符串参数时返回true.

//*[name()='BBB']

选择所有名称为BBB的元素(这里等价于//BBB)


     <AAA> 
          <BCC> 
               <
BBB/> 
               <
BBB/> 
               <
BBB/> 
          </BCC> 
          <DDB> 
               <
BBB/> 
               <
BBB/> 
          </DDB> 
          <BEC> 
               <CCC/> 
               <DBD/> 
          </BEC> 
     </AAA>

//*[starts-with(name(),'B')]

选择所有名称以"B"起始的元素


     <AAA> 
          <
BCC
               <
BBB/> 
               <
BBB/> 
               <
BBB/> 
          </
BCC
          <DDB> 
               <
BBB/> 
               <
BBB/> 
          </DDB> 
          <
BEC
               <CCC/> 
               <DBD/> 
          </
BEC
     </AAA>

//*[contains(name(),'C')]

选择所有名称包含"C"的元素


     <AAA> 
          <
BCC
               <BBB/> 
               <BBB/> 
               <BBB/> 
          </
BCC
          <DDB> 
               <BBB/> 
               <BBB/> 
          </DDB> 
          <
BEC
               <
CCC/> 
               <DBD/> 
          </
BEC
     </AAA>

实例 10

多个路径可以用分隔符 | 合并在一起

//CCC | //BBB

选择所有的CCC和BBB元素


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

/AAA/EEE | //BBB

选择所有的BBB元素和所有是AAA的子元素的EEE元素


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

/AAA/EEE | //DDD/CCC | /AAA | //BBB

可以合并的路径数目没有限制


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

实例 11

child轴(axis)包含上下文节点的子元素, 作为默认的轴,可以忽略不写.

/AAA

等价于 /child::AAA


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

/child::AAA

等价于/AAA


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

/AAA/BBB

等价于/child::AAA/child::BBB


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

/child::AAA/child::BBB

等价于/AAA/BBB


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

/child::AAA/BBB

二者都可以被合并


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

 

 

 

 

实例 12

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

/descendant::*

选择文档根元素的所有后代.即所有的元素被选择


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

/AAA/BBB/descendant::*

选择/AAA/BBB的所有后代元素


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

//CCC/descendant::*

选择在祖先元素中有CCC的所有元素


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

//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)包含上下文节点的父节点, 如果有父节点的话

//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轴总是包含有根节点,除非上下文节点就是根节点本身.

/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>

//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)包含上下文节点之后的所有兄弟节点

/AAA/BBB/following-sibling::*


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

//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)包含上下文节点之前的所有兄弟节点

/AAA/XXX/preceding-sibling::*


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

//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)包含同一文档中按文档顺序位于上下文节点之后的所有节点, 除了祖先节点,属性节点和命名空间节点

/AAA/XXX/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>

//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

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

/AAA/XXX/preceding::*


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

//GGG/preceding::*


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

 

 

 

 

实例 19

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

/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>

//CCC/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)包含上下文节点本身和该节点的祖先节点

/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>

//GGG/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文档(忽略属性节点和命名空间节点), 不能交迭, 而一起使用则包含所有节点

//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>

//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>

//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>

//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>

//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>

//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返回不小于参数的最小整数(趋近于负无穷)

//BBB[position() mod 2 = 0 ]

选择偶数位置的BBB元素


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

//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>

//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>

C# xpath的更多相关文章

  1. xpath提取多个标签下的text

    title: xpath提取多个标签下的text author: 青南 date: 2015-01-17 16:01:07 categories: [Python] tags: [xpath,Pyth ...

  2. C&num;&plus;HtmlAgilityPack&plus;XPath带你采集数据&lpar;以采集天气数据为例子&rpar;

    第一次接触HtmlAgilityPack是在5年前,一些意外,让我从技术部门临时调到销售部门,负责建立一些流程和寻找潜在客户,最后在阿里巴巴找到了很多客户信息,非常全面,刚开始是手动复制到Excel, ...

  3. 在Java中使用xpath对xml解析

    xpath是一门在xml文档中查找信息的语言.xpath用于在XML文档中通过元素和属性进行导航.它的返回值可能是节点,节点集合,文本,以及节点和文本的混合等.在学习本文档之前应该对XML的节点,元素 ...

  4. XPath 学习二: 语法

    XPath 使用路径表达式来选取 XML 文档中的节点或节点集.节点是通过沿着路径 (path) 或者步 (steps) 来选取的. 下面列出了最有用的路径表达式: 表达式 描述 nodename 选 ...

  5. xpath 学习一: 节点

    xpath 中,有七种类型的节点: 元素.属性.文本.命名空间.处理指令.注释.以及根节点 树的根成为文档节点或者根节点. 节点关系: Parent, Children, sibling(同胞), A ...

  6. Python爬虫利器三之Xpath语法与lxml库的用法

    前面我们介绍了 BeautifulSoup 的用法,这个已经是非常强大的库了,不过还有一些比较流行的解析库,例如 lxml,使用的是 Xpath 语法,同样是效率比较高的解析方法.如果大家对 Beau ...

  7. 使用python&plus;xpath 获取https&colon;&sol;&sol;pypi&period;python&period;org&sol;pypi&sol;lxml&sol;2&period;3&sol;的下载链接

    使用python+xpath 获取https://pypi.python.org/pypi/lxml/2.3/的下载链接: 使用requests获取html后,分析html中的标签发现所需要的链接在& ...

  8. 关于robotframework&comma;app&comma;appium的xpath定位问题及常用方法

    关于类似的帖子好像很多,但是没有找到具体能帮我解决问题的办法.还是自己深究了好久才基本知道app上面的xpath定位和web上的不同点: 先放一个图: A,先说说不用xpath的场景,一般是用于存在i ...

  9. Selenium Xpath Tutorials - Identifying xpath for element with examples to use in selenium

    Xpath in selenium is close to must required. XPath is element locator and you need to provide xpath ...

  10. xpath定位中starts-with、contains和text&lpar;&rpar;的用法

    starts-with 顾名思义,匹配一个属性开始位置的关键字 contains 匹配一个属性值中包含的字符串 text() 匹配的是显示文本信息,此处也可以用来做定位用 eg //input[sta ...

随机推荐

  1. 改造过的JS颜色选择器

    项目中用到颜色选择功能,在网上找了个颜色选择器,自己改了改代码.做成了一个可动态加载的颜色选择器. 把代码贴上,当是记录. /*Copyright(c)2009,www.supersite.me*/ ...

  2. java隐士类型转换和强制类型转换

    ,byte和short型在计算时会自动转换为int型计算,结果也是int 型.所以a1*a2的结果是int 型的. byte+byte=int,低级向高级是隐式类型转换,高级向低级必须强制类型转换,b ...

  3. Jquer学习之jQuery&lpar;function&lpar;&rpar;&lbrace;&rcub;&rpar;与&lpar;function&lpar;&rpar;&lbrace;&rcub;&rpar;&lpar;jQuery&rpar;之间的区别

    Jquery是优秀的Javascrīpt框架.我们现在来讨论下在 Jquery 中两个页面载入后执行的函数. $(document).ready(function(){ // 在这里写你的代码... ...

  4. 《转》 Openstack Grizzly 指定 compute node 创建 instance

    声明:此文档仅仅做学习交流使用,请勿用作其它商业用途 作者:朝阳_tony 邮箱:linzhaolover@gmail.com 2013年6月4日9:37:44 星期二 转载请注明出处:http:// ...

  5. PyCharm实现高效远程调试代码

      PyCharm实现高效远程调试代码   (薛刚强)    为方便Python代码学习和项目开发,目前选择专业的 IDE 开发工具 ,如 PyCham.针对个人使用的技巧做个笔记,分享给大家,有描述 ...

  6. &lbrack; Linux运维学习 &rsqb; 路径及实战项目合集

    我们知道运维工程师(Operations)最基本的职责就是负责服务的稳定性并确保整个服务的高可用性,同时不断优化系统架构.提升部署效率.优化资源利用率,确保服务可以7*24H不间断地为用户提供服务. ...

  7. 20155210 Exp5 MSF基础应用

    Exp5 MSF基础应用 一个主动攻击实践,MS08-067 首先利用msfconsole启用msf终端 然后利用search MS08-067搜索漏洞,会显示相应漏洞模块 如图: 根据上图,我们输入 ...

  8. 雷林鹏分享:Ruby 面向对象

    Ruby 面向对象 Ruby 是纯面向对象的语言,Ruby 中的一切都是以对象的形式出现.Ruby 中的每个值都是一个对象,即使是最原始的东西:字符串.数字,甚至连 true 和 false 都是对象 ...

  9. IOS地理信息使用

    概览 现在很多社交.电商.团购应用都引入了地图和定位功能,似乎地图功能不再是地图应用和导航应用所特有的.的确,有了地图和定位功能确实让我们的生活更加丰富多彩,极大的改变了我们的生活方式.例如你到了一个 ...

  10. js琐碎知识点

    1.javascript发展史 javascript首先由Netscape设计,为改善浏览器用户体验,名为liveScript, 网景公司被sun公司收购,为了宣传改名为javascript 后来su ...