java xpath 默认命名空间,Java XPath:具有默认命名空间xmlns的查询

I want to do an XPath query on this file (excerpt shown):

This is a snippet of the code I'm using:

DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();

DocumentBuilder builder = domFactory.newDocumentBuilder();

Document document = builder.parse(new File(testFile));

XPathFactory factory = XPathFactory.newInstance();

XPath xpath = factory.newXPath();

xpath.setNamespaceContext( new NamespaceContext() {

public String getNamespaceURI(String prefix) {

...

String result = xpath.evaluate(xpathQueryString, document);

System.out.println(result);

The problem I'm facing is that when the default namespace is referenced in an XPath query, the getNamespaceURI method is not called to resolve it.

This query for example doesn't extract anything:

//xmlns:ModelClass.Parent/xmlns:Core.Reference[@type=\"Model\"]/@package

Now I've tried "tricking" the parser by replacing xmlns with a fake prefix d and then writing the getNamespaceURI method accordingly (so to return http://xml.sap.com/2002/10/metamodel/webdynpro when d is encountered). In this case, the getNamespaceURI is called but the result of the XPath expression evaluation is always an empty string.

If I strip out namespaces from the file and from the XPath query expression, I can get the string I wanted (com.test.mypackage).

Is there a way to make things work properly with the default namespace?

解决方案

In your Namespace context, bind a prefix of your choice (e.g. df) to the namespace URI in the document

xpath.setNamespaceContext( new NamespaceContext() {

public String getNamespaceURI(String prefix) {

switch (prefix) {

case "df": return "http://xml.sap.com/2002/10/metamodel/webdynpro";

...

}

});

and then use that prefix in your path expressions to qualify element names e.g. /df:ModelClass/df:ModelClass.Parent/df:Core.Reference[@type = 'Model']/@package.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值