当前文本节点的前兄弟

时间:2011-08-04 13:53:51

标签: xml xslt xpath axes

我有这种XML:

<nav:objectList>
    <nav:item >
      <nav:attribute name="display">1</nav:attribute> 
      <nav:attribute name="className">document.Document</nav:attribute> 
      <nav:attribute name="title">item 1</nav:attribute> 
      <nav:attribute name="getFileExtension">pdf</nav:attribute> 
    </nav:item>
    <nav:item >
      <nav:attribute name="display">2</nav:attribute> 
      <nav:attribute name="className">video.Video</nav:attribute> 
      <nav:attribute name="title">item 2</nav:attribute> 
      <nav:attribute name="getFileExtension">mp4</nav:attribute> 
    </nav:item>
    <nav:item >
      <nav:attribute name="display">3</nav:attribute> 
      <nav:attribute name="className">document.Document</nav:attribute> 
      <nav:attribute name="title">item 3</nav:attribute> 
      <nav:attribute name="getFileExtension">pdf</nav:attribute> 
    </nav:item>
    <nav:item >
      <nav:attribute name="display">4</nav:attribute> 
      <nav:attribute name="className">video.Video</nav:attribute> 
      <nav:attribute name="title">item 4</nav:attribute> 
      <nav:attribute name="getFileExtension">mp4</nav:attribute> 
    </nav:item>
    <nav:item >
      <nav:attribute name="display">5</nav:attribute> 
      <nav:attribute name="className">document.Document</nav:attribute> 
      <nav:attribute name="title">item 5</nav:attribute> 
      <nav:attribute name="getFileExtension">pdf</nav:attribute> 
    </nav:item>
</nav:objectList>

我想要计算当前document.Document之前的所有 document.Document 。 (我不想数视频。视频) 例如,如果我在5,我想返回2而不是4。

看起来像帖子:XSLT - Comparing preceding-sibling's elements with current's node element

我实际上正在尝试(很多事情):

count(preceding-sibling::nav:attribute[@name='type.className']='com.arsdigita.cms.document.Document'

感谢的

罗曼

2 个答案:

答案 0 :(得分:1)

preceding-sibling更改为preceding,因为您尝试计算的属性不是您尝试计算的节点的兄弟(他们有不同的父级)。您还需要从= 'document.Document'部分制作完整的条款:

count(preceding::nav:attribute[@name='type.className'
                               and . = 'document.Document'])

(或者你想要完整的'com.arsdigita.cms.document.Document'?)

答案 1 :(得分:0)

我不熟悉xslt,但我使用xpath很多。这会产生一系列nav:item元素与document.Document后代:

//nav:item[nav:attribute[@name="className" and text()="document.Document"]]

从其中一个nav:item元素运行,会告诉你在它之前有多少相似的元素:

count(preceding-sibling::nav:item[nav:attribute[@name="className" and text()="document.Document"]])