This is again the dreaded default namespace problem.

The XPath expression //resource searches for all elements that are not
in a namespace and are named 'resource'.

In this case the //resource elements are in the
'http://www.imsglobal.org/xsd/imscp_v1p1' namespace.

XPath however does not allow us to specify that it should look for the
elements in the default namespace.

To solve this you should be allowed to specify a mapping between your
namespace and any arbitrary prefix, and use this prefix to search for
elements in that namespace.

So let's say you specified 'ims' as the prefix for your namespace, you
could now look for your resource elements using the following XPath
expression:
//ims:resource

Most XPath processors allow you to specify a namespace prefix mapping
and dom4j allows this as well using the 'setXPathNamespaceURIs' method
on the DocumentFactory.

The code should look something like this:

HashMap map = new HashMap();
map.put( "ims", "http://www.imsglobal.org/xsd/imscp_v1p1";);
DocumentFactory.getInstance().setXPathNamespaceURIs( map);

executeXPath( document, "//ims:resource");

Note: This mechanism can also be used to search documents where the
namespace prefix changes between documents. (some people prefer 'xs'
as the prefix for the xml-schema namespace, others prefer 'xsd')

Regards,
Edwin


-------------------------------------------------------
This SF.Net email is sponsored by: IntelliVIEW -- Interactive Reporting
Tool for open source databases. Create drag-&-drop reports. Save time
by over 75%! Publish reports on the web. Export to DOC, XLS, RTF, etc.
Download a FREE copy at http://www.intelliview.com/go/osdn_nl
_______________________________________________
dom4j-user mailing list
dom4j-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/dom4j-user

Reply via email to