Reputation: 7306
I got xml
<FictionBook xmlns="http://www.gribuser.ru/xml/fictionbook/2.0" xmlns:l="http://www.w3.org/1999/xlink">
<description>
<title-info>
<genre>love_contemporary</genre>
<author>
<first-name>Sylvain</first-name>
<last-name>Reynard</last-name>
</author>
<book-title>Gabriel's Inferno</book-title>
<annotation>
<p>Enigmatic and sexy, Professor Gabriel Emerson is a well respected Dante specialist by day, but by night he devotes himself to an uninhibited life of pleasure. He uses his notorious good looks and sophisticated charm to gratify his every whim, but is secretly tortured by his dark past and consumed by the profound belief that he is beyond all hope of redemption. When the sweet and innocent Julia Mitchell enrolls as his graduate student, his attraction and mysterious connection to her not only jeopardizes his career, but sends him on a journey in which his past and his present collide. An intriguing and sinful exploration of seduction, forbidden love and redemption, Gabriel's Inferno is a captivating and wildly passionate tale of one man's escape from his own personal hell as he tries to earn the impossible…forgiveness and love.</p>
</annotation>
<date/>
<coverpage>
<image l:href="#_0.jpg"/>
</coverpage>
<lang>en</lang>
<src-lang>en</src-lang>
<sequence name="Gabriel's Inferno" number="1"/>
</title-info>
<document-info>
<author>
<first-name/>
<last-name/>
</author>
<date/>
<id>2aec7273-a8a4-4edc-803a-820c4d76bc3f</id>
<version>1.0</version>
</document-info>
<publish-info>
<book-name>Gabriel's Inferno</book-name>
<year>2011</year>
</publish-info>
</description>
</FictionBook>
My expression to get value of attribute
string(//coverpage/image/@l:href)
Code in android programm
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
String expression;
String attrValue;
expression = "string(//coverpage/image/@l:href)";
try {
attrValue = xpath.compile(expression).evaluate(obj,
XPathConstants.STRING).toString();
System.out.println("VAL XML:"+attrValue);
} catch (XPathExpressionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
But on console i get only:
VAL XML:
Why? What i doing wrong?
I try http://www.freeformatter.com/xpath-tester.html#ad-output for online testtings - everything works fine. Get string #_0.jpg
Upvotes: 0
Views: 200
Reputation: 9644
Your problem is that the node you're trying to catch is using the XML namespace, and the factory isn't aware of it. I see two solutions for this:
Without defining the namespace
Avoid the issue using local-name()
to ignore namespaces altogether.
//*[local-name() = 'coverpage']/*[local-name() = 'image']/@*[local-name() = 'href']
(//coverpage/image/@*[local-name() = 'href']
might work as well)
Defining the namespace
Make XPathFactory aware of the different namespaces so that it knows which one to use.
import javax.xml.namespace.NamespaceContext;
...
xpath.setNamespaceContext(new MyNamespaceContext());
attrValue = xpath.compile(expression).evaluate(obj,
XPathConstants.STRING).toString();
...
private static class MyNamespaceContext implements NamespaceContext {
public String getNamespaceURI(String prefix) {
if("l".equals(prefix)) {
return "http://www.w3.org/1999/xlink";
}
return null;
}
public String getPrefix(String namespaceURI) {
return null;
}
public Iterator getPrefixes(String namespaceURI) {
return null;
}
}
(possible duplicate: How to use XPath on xml docs having default namespace)
Upvotes: 1