2

我正在尝试匹配特定节点之前的所有节点。输入 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>

使用那作为我的输入 XML,我试图在<p>标签之前抓取所有<ul>标签并呈现它们,但未能成功。可能有 0<p>个标签或无限个标签。关于如何使用 XSLT 1.0 做到这一点的任何想法?谢谢!

4

2 回答 2

2
/story/content/p[not(preceding-sibling::ul)]
于 2012-12-13T19:47:34.953 回答
0

使用

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

这通常是错误的

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

因为它不会选择出现在 any 之前但不是 any 的兄弟的p元素。ulul

例如,给定这个 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>
于 2012-12-14T01:24:05.190 回答