【问题标题】:preceding-sibling of current text node当前文本节点的前同级
【发布时间】:2011-10-20 00:54:46
【问题描述】:

我有这种 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'

谢谢

罗曼

【问题讨论】:

  • @LarsH:你说得对,我想要全名 com.arsdigita.cms.document.Document),我简化了 XML。感谢你们俩 !!你的两个答案很有趣,我下周一试试(我之前不能测试),然后告诉你一个

标签: xml xslt xpath axes


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

    我不熟悉 xslt,但我经常使用 xpath。这会产生一个带有document.Document 后代的nav:item 元素序列:

    //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"]])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-17
      • 1970-01-01
      相关资源
      最近更新 更多