1

我有这种 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 - 将前兄弟的元素与当前的节点元素进行比较

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

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

谢谢

罗曼

4

2 回答 2

1

更改preceding-siblingpreceding,因为您要计数的属性不是您要计数的节点的兄弟姐妹(它们有不同的父节点)。您还需要从该= 'document.Document'部分中制作一个完整的子句:

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

(或者也许你想要完整'com.arsdigita.cms.document.Document'的在那里?)

于 2011-08-04T14:28:09.757 回答
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"]])
于 2011-08-04T14:22:56.163 回答