更新:GET值:noNamespaceSchemaLocation(XPATH)
我想获得在XSI的arritbute的价值:noNamespaceSchemaLocation,即: “http://www.mypage/pagedescription.xsd”
<link
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://www.mypage/pagedescription.xsd">
...
...
链接是在我的XML文件中的元素(接近顶部)
然而,XPATH // N:链接/ @ XSI:noNamespaceSchemaLocation不起作用
我试图修改的背景,但它仍然无法正常工作
@Override
public Object executeXpathQuery(Document domDoc, String strQuery) throws Exception {
System.out.println("executeXpathQuery : strQuery:" + strQuery);
// output: executeXpathQuery: strQuery://n:link/@xsi:noNamespaceSchemaLocation
// so far, it looks OK
XPathFactory xpf = XPathFactory.newInstance();
XPath xPath = xpf.newXPath();
final String nonameNamespace = domDoc.getFirstChild().getNamespaceURI();
NamespaceContext ctx = new NamespaceContext() {
public String getNamespaceURI(String prefix) {
String uri = null;
if ("n".equals(prefix)) {
System.out.println("using prefix");
uri = nonameNamespace;
}
else if ("xsi".equals(prefix)) {
uri = "http://www.w3.org/2001/XMLSchema-instance";
}
return null;
}
public String getPrefix(String uri) {
throw new UnsupportedOperationException();
}
public Iterator getPrefixes(String uri) {
throw new UnsupportedOperationException();
}
};
xPath.setNamespaceContext(ctx);
XPathExpression xPathExp = xPath.compile(strQuery);
return (Node) xPathExp.evaluate(domDoc,XPathConstants.NODE);
}
而且我做了docuùent建设者意识到上下文:
static DocumentBuilder getDocumentBuilder() {
DocumentBuilderFactory dbf;
DocumentBuilder db;
db = null;
try {
dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
db = dbf.newDocumentBuilder();
} catch (ParserConfigurationException e) {
LogMes.log(XMLFactoryServiceImpl.class, LogMes.ERROR, "Erreur lors de la cràation d'un document DOM. Message: " + e.getMessage());
}
return db;
}
我得到“使用前缀”在输出=>这部分流行OK 我仍然得到一个空值
任何想法?
我已考虑到您提出的修改,但我仍收到空白。不过,非常感谢您的好意! – stackSaru
@ user3166206是名称空间中的link元素本身(即是否有一个具有默认'xmlns =“...”声明的祖先元素)?如果是这样的话,你可能需要XPath中的'n:link'而不是'link'。 –