java - 如何读取xlink :label value using XPath

标签 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));
OutPut
SequenceNumber:: SequenceNumber="1"

null

我在提取 xlink:label 的值时犯了什么错误?请帮忙。

最佳答案

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

关于java - 如何读取xlink :label value using XPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52672018/

相关文章:

java - 混合实例和静态同步方法,得到意外的测试输出

java - JSON 到 XML 的转换而不改变序列

javascript - 更新 xlink :href for a <use> element 时,Internet Explorer 无法正确绘制 SVG

jaxb - 如何强制 JAXB 编码器使用 xlink 引用?

java - 如何将 Thread.currentThread() 与保存的 Thread 引用进行比较? ==,等于,..?

java - 查询以获取 2 个给定日期之间的所有日期

java - S3 : Generating Pre-Signed URL for a given Key. [ key 可能/不存在]

xml - 将每个计数器值从一个传递到另一个

python - OpenERP 7 用户角色的访问权限

javascript - 是否可以使用 HTML 的 .querySelector() 通过 SVG 中的 xlink 属性进行选择?