简体   繁体   English

XML格式未知时的Java XPath命名空间

[英]Java XPath Namespaces when format of XML is unknown

I've seen quite a bit of discussion on the topic of Java / XPath, and Namepsaces, but I'm not sure the answers to those questions can help with my particular problem. 我已经在Java / XPath和Namepsaces主题上进行了很多讨论,但是我不确定这些问题的答案是否可以解决我的特定问题。

I have a program that gets hold of some XML - I don't know the format up-front, but it is likely to contain namespaces. 我有一个可以容纳一些XML的程序-我事先不知道格式,但是它可能包含名称空间。 The user can supply an xpath expression that will be used to extract a particular piece of data from the XML. 用户可以提供一个xpath表达式,该表达式将用于从XML中提取特定的数据。 The code is generic - I can't conpile in any namespaces or anything like that as I don't know the format of the XML up-front. 代码是通用的-我无法在任何名称空间或类似名称中进行编译,因为我不预先知道XML的格式。

So XML may look like this: 因此,XML可能如下所示:

<ns1:item id='myroot' xmlns:ns1='http://www.myplace.com/place'>
    <ns1:category id='cat1'>
        <ns1:item id='item1'>
            <ns1:category id='cat2'></ns1:category>
        </ns1:item>
    </ns1:category>
</ns1:item>

and the xpath that the user enters may be, say: 用户输入的xpath可能是:

/ns1:item[@id='myroot']/ns1:category[@id='cat1']

but with the following code, I always get nothing (no matter what XPath I seem to try): 但是使用以下代码,我始终一无所获(无论我尝试使用哪种XPath):

public static String evaluate(String xml, String xpathExpression) throws Exception
{
    String result = "";
    InputSource is = new InputSource(new StringReader(xml));
    XPath xPath = XPathFactory.newInstance().newXPath();
    result = xPath.evaluate(xpathExpression, is);
    System.out.println("result=" +result);
}

Any help greatfully appreciated. 任何帮助,不胜感激。

Thanks 谢谢

Essentially, an expression like 本质上,类似

/ns1:item[@id='myroot']/ns1:category[@id='cat1']

is meaningless unless you know what ns1 refers to. 除非您知道ns1指的是什么,否则它是没有意义的。 So if the user supplies an expression in this form, then they need also to tell you what ns1 means. 因此,如果用户以这种形式提供表达式,那么他们还需要告诉您ns1的含义。

XPath 3.0 (at long last) provides a way of defining XPath expressions that don't depend on knowing prefix bindings: you can write XPath 3.0(最后)提供了一种定义不依赖前缀绑定的XPath表达式的方法:您可以编写

/Q{http://www.myplace.com/place}item[@id='myroot']/Q{http://www.myplace.com/place}category[@id='cat1']

But the only way of achieving this in 1.0 and 2.0 is to write 但是在1.0和2.0中实现此目标的唯一方法是编写

/*[local-name()='item' and namespace-uri()='http://www.myplace.com/place'][@id='myroot']/*[local-name()='category' and namespace-uri()='http://www.myplace.com/place'][@id='cat1']

The comment from "MadProgrammer" helped me find the answer I was looking for: 来自“ MadProgrammer”的评论帮助我找到了所需的答案:

String result = "";

DocumentBuilderFactory xmlFactory = DocumentBuilderFactory.newInstance();

xmlFactory.setNamespaceAware(false);

DocumentBuilder builder = xmlFactory.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(xml));
Document document = builder.parse(is);
XPath xPath = XPathFactory.newInstance().newXPath();

result = xPath.evaluate(xpathExpression, document);

return result;

Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM