英文:
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<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;
	}
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论