其祖先不是特定节点的节点的 XPath 选择器
XPath selector for nodes that its ancestors are not a specific node
我正在编写 XPath select 或 select 所有节点名称 o:OLEObject,前提是其祖先不是 w:del。但 w:del 中的节点包含在结果中。你能帮我清除它吗?
这是我的脚本:
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;
}
您应该将 XPath 替换为:
//*/o:OLEObject[not(ancestor::w:del)]
Select OLEObject
元素,任何 (*
) 元素的子元素,并且没有名为 del
.
的祖先元素
我正在编写 XPath select 或 select 所有节点名称 o:OLEObject,前提是其祖先不是 w:del。但 w:del 中的节点包含在结果中。你能帮我清除它吗? 这是我的脚本:
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;
}
您应该将 XPath 替换为:
//*/o:OLEObject[not(ancestor::w:del)]
Select OLEObject
元素,任何 (*
) 元素的子元素,并且没有名为 del
.