如何使用XPath读取xlink:label值

时间:2018-10-05 19:16:37

标签: java xml xlink

我需要从XML获取xlink:label值(ASSET_1)。

<MESSAGE xmlns:xlink="http://www.w3.org/1999/xlink">
<ABOUT_VERSIONS>
<ABOUT_VERSION SequenceNumber="1"  xlink:label="ASSET_1" >
<CreatedDatetime>2015-08-24T09:30:47Z</CreatedDatetime>
<DataVersionName>Purchase Example</DataVersionName>
</ABOUT_VERSION>
</ABOUT_VERSIONS>
</MESSAGE>

我正在尝试的Java代码如下

XPathFactory xpf = XPathFactory.newInstance();            
XPath xPath = xpf.newXPath();

XPathExpression pathExpression = xPath.compile("MESSAGE/ABOUT_VERSIONS/ABOUT_VERSION");   
InputSource inputSource = new InputSource("C:/Sample.xml");  
NodeList Nodes = (NodeList) xPath.evaluate("MESSAGE/ABOUT_VERSIONS/ABOUT_VERSION", inputSource, XPathConstants.NODESET);

System.out.println("SequenceNumber:: "+xPath.evaluate("MESSAGE/ABOUT_VERSIONS/ABOUT_VERSION/@SequenceNumber", inputSource, XPathConstants.NODE));
System.out.println(" "+xPath.evaluate("MESSAGE/ABOUT_VERSIONS/ABOUT_VERSION/@xlink:label", inputSource, XPathConstants.NODE));

输出 SequenceNumber :: SequenceNumber =“ 1”

我要提取xlink:label的值有什么错误?请帮忙。

1 个答案:

答案 0 :(得分:1)

您可以使用@*[name()='xlink:label']代替@xlink:label。 同样,切换到@*[local-name()='label']也可以解决问题。