Skip to Content.
Sympa Menu

xom-interest - Re: [XOM-interest] Xpath parsing problem

xom-interest AT lists.ibiblio.org

Subject: XOM API for Processing XML with Java

List archive

Chronological Thread  
  • From: Peter Murray-Rust <pm286 AT cam.ac.uk>
  • To: XOM API for Processing XML with Java <xom-interest AT lists.ibiblio.org>
  • Subject: Re: [XOM-interest] Xpath parsing problem
  • Date: Thu, 3 Mar 2011 08:49:50 +0000

Although yours isn't primarily a XOM-related question it's worth noting that
XOM has good support for namespaces.

Firstly it's easy to go wrong in this area. The XOM serialization should
give you a pointer to what namespaces are carried by which nodes.

Yopu can also get them programmatically
>From the XOM docs:

public final class Namespace

extends Node <http://www.xom.nu/apidocs/nu/xom/Node.html>

Represents a namespace in scope. It is used by XOM's XPath implementation
for the namespace axis. However, it is not really part of the XOM data
model. Namespace objects are only created as needed when evaluating XPath.
While a namespace node has a parent element (which may be null), that
element does not know about these namespace nodes and cannot remove them.
(This is an inconsistency in the XPath data model, and is shared with
attributes which also have parents but are not children.)

and you can get them programmatically through:

Element.getNamespaceDeclarationCount

public final int getNamespaceDeclarationCount()

Returns the number of namespace declarations on this element. This counts
the namespace of the element itself (which may be the empty string), the
namespace of each attribute, and each namespace added by
addNamespaceDeclaration. However, prefixes used multiple times are only
counted once; and the xml prefix used for xml:base, xml:lang, and
xml:spaceis not counted even if one of these attributes is present on
the element.

The return value is almost always positive. It can be zero if and only if
the element itself has the prefix xml; e.g. <xml:space />. This is not
endorsed by the XML specification. The prefix xml is reserved for use by the
W3C, which has only used it for attributes to date. You really shouldn't do
this. Nonetheless, this is not malformed so XOM allows it.

Returns:the number of namespaces declared by this element
and also:
Element.getNamespacePrefix<http://www.xom.nu/apidocs/nu/xom/Element.html#getNamespacePrefix%28int%29>
(int index)
Returns the indexth namespace prefix *declared* on this element.
and
Element.getNamespaceURI<http://www.xom.nu/apidocs/nu/xom/Element.html#getNamespaceURI%28java.lang.String%29>
(String prefix)
Returns the namespace URI mapped to the specified prefix within
this element.

Apply these to your elements and you will probably be surprised, bewildered
and enlightened in that order.

As a matter of practice I often avoid "/a/b/c" in Xpath because it is easy
to overlook empty namespaces and similar problems and use:
*[local-name()='a']/*[local-name()='b']/*[local-name()='c']
if there is no chabce of collision (if there is, use additional
namespace-uri() values]

The problem is that failure to find nodes in XPath is often not a machine
error, simply a human error. And XOM, great as it is, cannot look into the
mind of the programmer.

P.

--
Peter Murray-Rust
Reader in Molecular Informatics
Unilever Centre, Dep. Of Chemistry
University of Cambridge
CB2 1EW, UK
+44-1223-763069




Archive powered by MHonArc 2.6.24.

Top of Page