javaxmlxlink

How to read xlink:label value using XPath


i need to get the xlink:label value(ASSET_1) from XML.

<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>

The Java Code i am trying is like below

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

What is the mistake i am doing to pull the value of xlink:label ? Please help.


Solution

  • You can use @*[name()='xlink:label'] in place of @xlink:label. Also switching to @*[local-name()='label'] should do the trick.