xslt匹配特定节点之前的所有节点

时间:2012-12-13 19:42:27

标签: xslt xpath

我正在尝试匹配特定节点之前的所有节点。输入XML

<story>
  <content>
    <p>This is the text I want</p>
    <p>This is the text I want</p>
    <p>This is the text I want</p>
    <ul>
       <li></li>
       ...
    </ul>
    ....
    ....
  </content>
</story>

使用That作为我的输入XML,我正在尝试并且未能抓取<p>标记之前的所有<ul>标记并进行渲染。可能有0 <p>个标签或无限。有关如何使用XSLT 1.0执行此操作的任何想法?谢谢!

2 个答案:

答案 0 :(得分:2)

/story/content/p[not(preceding-sibling::ul)]

答案 1 :(得分:0)

使用

//p[not(preceding::ul or ancestor::ul)]

这通常是错误的

//p[not(preceding-sibling::ul)]

因为它没有选择任何p之前的ul个元素,但不是任何ul的兄弟元素。

例如,给定此XML文档

<story>
  <div>
    <p>Must be selected</p>
  </div>
  <ul>
    <li><p>Must not be selected</p></li>
  </ul>
  <content>
    <p>Must not be selected</p>
    <div>
      <p>Must not be selected</p>
    </div>
    <p>Must not be selected</p>
    <p>Must not be selected</p>
    <ul>
       <li></li>
       <li><p>This must not be selected</p></li>
    </ul>
    ....
    ....
  </content>
</story>

以上错误的表达式选择

<p>Must not be selected</p>
<p>Must not be selected</p>
<p>Must not be selected</p>

并没有选择想要的元素:

<p>Must be selected</p>

但是,此答案开头的正确表达式仅选择了所需的p元素:

<p>Must be selected</p>