XPath选择器用于选择其祖先不是特定节点的节点。

huangapple go评论86阅读模式
英文:

XPath selector for nodes that its ancestors are not a specific node

问题

public List<String> getAllOleObjectId(XmlObject wobj) {
    List<String> lstOfOleObjIds = new ArrayList<String>();
    XmlCursor cursorForOle = wobj.newCursor();
    if (cursorForOle != null) {
        cursorForOle.selectPath(
            "declare namespace w='http://schemas.openxmlformats.org/wordprocessingml/2006/main' " +
            "declare namespace o='urn:schemas-microsoft-com:office:office' " +
            ".//*/o:OLEObject[ancestor::*[not(self::w:del)]]"
        );
        while (cursorForOle.hasNextSelection()) {
            cursorForOle.toNextSelection();
            XmlObject oleObj = cursorForOle.getObject();
            Node oleDomNode = oleObj.getDomNode();
            NamedNodeMap domAttrObj = oleDomNode.getAttributes();
            lstOfOleObjIds.add(domAttrObj.getNamedItem("r:id").getNodeValue());
        }
    }
    cursorForOle.dispose();
    return lstOfOleObjIds;
}
英文:

I'm writing an XPath selector to select all the node name o:OLEObject providing that its ancestor is not w:del. but the nodes in w:del are included in the result. Can you help me to clear it?
Here is my script:

   publicList&lt;String&gt; getAllOleObjectId(XmlObject wobj) {
		List&lt;String&gt; lstOfOleObjIds = new ArrayList&lt;String&gt;();
		XmlCursor cursorForOle = wobj.newCursor();
		if(cursorForOle != null) {
			cursorForOle.selectPath(
				&quot;declare namespace w=&#39;http://schemas.openxmlformats.org/wordprocessingml/2006/main&#39; &quot; + 
				&quot;declare namespace o=&#39;urn:schemas-microsoft-com:office:office&#39; &quot; +
				&quot;.//*/o:OLEObject[ancestor::*[not(self::w:del)]]&quot;
			);
			while (cursorForOle.hasNextSelection()) {

				 cursorForOle.toNextSelection();
				 XmlObject oleObj = cursorForOle.getObject();
				 Node oleDomNode = oleObj.getDomNode();
				 NamedNodeMap domAttrObj = oleDomNode.getAttributes();
				 lstOfOleObjIds.add(domAttrObj.getNamedItem(&quot;r:id&quot;).getNodeValue());
		   }
		}
		cursorForOle.dispose();
		return lstOfOleObjIds;
	}

答案1

得分: 1

你应该将你的XPath替换为:

//*/o:OLEObject[not(ancestor::w:del)]

选择 OLEObject 元素,它是任意 (*) 元素的子元素,并且没有名为 del 的祖元素。

英文:

You should replace your XPath with :

//*/o:OLEObject[not(ancestor::w:del)]

Select OLEObject element, child of any (*) element, and which has no ancestor element named del.

huangapple
  • 本文由 发表于 2020年6月5日 20:32:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/62215460.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定