2014-02-21 76 views
0

我想知道为什么下面的Xpath表达式将计数设置为2而不是3.感谢您的帮助。Xpath计数函数说明

Xpath-

<xsl:value-of select="count(//x[1]/y[1])"/> 

XML

<?xml version="1.0"?> 
<test> 
    <x a="1"> 
     <x a="2"> 
     <x> 
      <y>y31</y> 
      <y>y32</y> 
     </x> 
     </x> 
    </x> 
    <x a="1"> 
     <x a="2"> 
     <y>y21</y> 
     <y>y22</y> 
     </x> 
    </x> 
    <x a="1"> 
     <y>y11</y> 
     <y>y12</y> 
    </x> 
    <x> 
     <y>y03</y> 
     <y>y04</y> 
    </x> 
</test> 

//count (//x[1]/y[1]) is selecting the following 2 elements. 

1) <x> 
     <y>y31</y> 

2) <x a="2"> 
     <y>y21</y> 

并且它不选择在相同的电平以下元件中的一个,以添加数作为3.我想澄清这一点。

<x a="1"> 
     <y>y11</y> 


     or 

    <x> 
     <y>y03</y> 

感谢, 马修

回答

0

//x[1]/y[1]选择y元素是x元件的第一孩子是1米的儿童或者它们的母体。

test

<x a="1"> 
    <x a="2">  <!-- x is first child of its parent --> 
    <y>y21</y> <!-- y is first child of its parent x --> 
    <y>y22</y> 
    </x> 
</x> 

所以第一个孩子和第二个孩子或test

<x a="1"> 
    <x a="2"> 
    <x>   <!-- x is first child of its parent --> 
     <y>y31</y> <!-- y is first child of its parent x --> 
     <y>y32</y> 
    </x> 
    </x> 
</x> 

都是或含有x元素本身具有x元素作为第一个孩子与y作为第一个孩子,这是匹配的。

<x a="1">   <!-- x is 3rd child of its parent test --> 
     <y>y11</y> 
     ... 

test

<x>    <!-- x is 4th child of its parent test --> 
     <y>y03</y> 

第三个孩子的test

的第4个孩子,使他们不会匹配

+0

谢谢您的回答保罗,想知道为什么它匹配第二个孩子x也(因为它已经matche d第一个X孩子) 第二个X子匹配 Y21 user3053015

+0

添加于为什么2'X/y'比赛 –

+0

非常感谢保罗评论。感谢你的帮助。 – user3053015