Error executing xpath 'h:head/model/instance' from node 'h:html': Inexisting target node.

16 views
Skip to first unread message

Matt Adams

unread,
Oct 21, 2010, 11:28:30 AM10/21/10
to xml...@googlegroups.com
Hello,

I have an XML file that looks like this:

<?xml version="1.0"?>
<h:html xmlns="http://www.w3.org/2002/xforms"
xmlns:h="http://www.w3.org/1999/xhtml"
xmlns:ev="http://www.w3.org/2001/xml-events"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:jr="http://openrosa.org/javarosa">
<h:head>
<h:title>Widgets</h:title>
<model>
<itext>
</itext>
<instance>
<widgets xmlns="widgets">
</widgets>
</instance>
</model>
</h:head>
<h:body>
</h:body>
</h:html>

I am able to successfully load the file and most things that I have
tried thus far have tried work fine however I am having trouble running
the following code.

String instanceChild =
mForm.gotoTag("h:head/model/instance").gotoChild().getCurrentTagName();

From what I understand this should give me the string "widgets" however
instead I am getting an exception that reads

10-21 09:13:44.463: ERROR/AndroidRuntime(2202): Caused by:
com.mycila.xmltool.XMLDocumentException: Error executing xpath
'h:head/model/instance' from node 'h:html': Inexisting target node.

I would really appreciate if someone could tell me what I'm doing wrong
here. Whatever it is it isn't apparent to me.


Thanks in advance,

Matt

Mathieu

unread,
Oct 21, 2010, 2:04:56 PM10/21/10
to xmltool
When I process xml, i use to remove all namespaces and prefixes. The
reason is simple:

these xml are equivalent:
<h:html xmlns:h="http://www.w3.org/1999/xhtml">
<d:html xmlns:d="http://www.w3.org/1999/xhtml">

but if you develop a namespace-aware parsing, it means that the second
one will crash because h:html in your xpath expression does not exist.

So i strongly advise to have some code like this to have a better
parsing:

XMLTag tag = XMLDoc.from(Xml.class.getResource("/android.xml"),
false).deletePrefixes();
String instanceChild = tag.gotoTag("head/model/
instance").gotoChild().getCurrentTagName();
System.out.println(instanceChild);

If you really want to keep namespace usage (and thus have a clearly
unreadable code) you should go like this:

tag = XMLDoc.from(Xml.class.getResource("/a.xml"), false);
String defaultPrefix = tag.getPefix("http://www.w3.org/2002/xforms");
instanceChild = tag.gotoTag("h:head/%1$s:model/%1$s:instance",
defaultPrefix).gotoChild().getCurrentTagName();
System.out.println(instanceChild);

You must always use namepsaces in your xpath expressions (this
limitation commes from the jdk w3c implementation).

Matt Adams

unread,
Oct 22, 2010, 2:19:06 PM10/22/10
to xml...@googlegroups.com
Mathieu wrote:

> You must always use namepsaces in your xpath expressions (this
> limitation commes from the jdk w3c implementation).

Thanks, Mathieu. Your explanation helped.


Cheers,

Matt

Reply all
Reply to author
Forward
0 new messages