[英]XPath - select all elements except element (and it's subelements) with given attribute
我正在使用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”且結構正確的EXCEPT元素,並且都不是其子元素? 所以我要選擇
<root>
<chapter>
<section condition="cond1">
<para> Condition 1 Para </para>
</section>
</chapter>
</root>
我在那里的xpointer不起作用:
xpointer(*//[@condition="cond1" or not(@condition)])
首先修復語法:
//*[@condition="cond1" or not(@condition)]
然后再次查看需求:“具有屬性condition =“ cond2的EXCEPT元素”
那會是
//*[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問題。 您基本上是在使用錯誤的工具來完成這項工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.