【问题标题】:XPath - select all elements except element (and it's subelements) with given attributeXPath - 选择具有给定属性的元素(及其子元素)以外的所有元素
【发布时间】:2017-05-11 04:16:51
【问题描述】:

我正在使用 xinclude 将文档的某些部分包含在另一个文档中,例如,在我的主文档中:

<document>
  <xi:include href="included.xml"
   xpointer = "xpointer(*//[@condition="cond1" or not(@condition)])"
   xmlns:xi="http://www.w3.org/2001/XInclude" />
</document>

而我的 included.xml 看起来像:

<root>
  <chapter>
     <section condition="cond1">
       <para> Condition 1 Para </para>
     </section>

     <section condition="cond2">
       <para> Condition 2 Para </para>
     </section>
  </chapter>
</root>

我的问题是,我怎样才能选择所有内容,保留属性 condition="cond2" 的正确结构除外元素,也不是它的子元素?所以我要选择

<root>
  <chapter>
     <section condition="cond1">
       <para> Condition 1 Para </para>
     </section>
  </chapter>
</root>

我那里的 xpointer 不起作用:

xpointer(*//[@condition="cond1" or not(@condition)])

【问题讨论】:

    标签: xml xpath xinclude xpointer


    【解决方案1】:

    从修正语法开始:

    //*[@condition="cond1" or not(@condition)]
    

    然后再看需求:"EXCEPT element with attribute="cond2"

    那就是

    //*[not(@condition="cond2")]
    

    现在是棘手的一点:“也没有它的子元素”。在问题标题中,您称它们为“子元素” - 我假设您实际上是指任何深度的后代元素。

    字面意思是

    //*[not(ancestor-or-self::*[@condition="cond2"])] 
    

    但是在这一点上我们需要停下来。您已经标记了这个问题 XPath,但它实际上根本不是关于 XPath,而是关于 XPointer。具体来说,它是关于使用 xpointer() 方案的 XPointer,它仅作为 2002 年的 W3C 工作草案存在,该草案从未完成。见https://www.w3.org/TR/xptr-xpointer/。所以首先我们需要确定你实际使用的是什么 XPointer 实现,以及它符合什么规范。

    然后我们需要考虑您在 XInclude 方面想要实现的目标。我认为您正在尝试包含的不是一组选定的元素,而是删除了一些子树的整个树。当您选择要包含在 XInclude 中的节点时,它将将该节点连同以该节点为根的子树一起引入,无论子节点是否被显式选择。您不能使用 XInclude 对包含的树执行转换。

    所以这不仅仅是语法问题或 XPath 问题。你基本上是在使用错误的工具来完成这项工作。

    【讨论】:

      猜你喜欢
      • 2022-01-03
      • 1970-01-01
      • 2012-05-20
      • 1970-01-01
      • 2014-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-24
      相关资源
      最近更新 更多