更新:
我想在xsi:noNamespaceSchemaLocation中获取arritbute的值,也就是说:“ 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:link/@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);
}
我让文档生成器了解上下文:
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;
}
我在输出中得到“使用前缀”=> 这部分 lokks OK 我仍然得到一个空值
有任何想法吗 ?