> I do think this affects portability, but are you suggesting that we use
> xpath such as:
> //http://www.w3.org/1999/xhtml:html/http://www.w3.org/1999/xhtml:body/http://www.w3.org/1999/xhtml:a
> ?
> I'm not even sure this is the right syntax, because you would probably
> have to escape the colon and /'s within the URLs.

Such a syntax does not exist.

> If I'm using an xhtml document and only concerning myself with xhtml
> tags, then why should I care about namespaces, and have an abomination
> such as this within my code?

I don't think you can equate 'verboseness' with bad coding practices.

No, what I'm saying that the following code is portable because you
are not depending on the prefix that has been defined for the
namespace.

HashMap map = new HashMap();
map.put("xhtml", "http://www.w3.org/1999/xhtml";);

XPath xpath = DocumentHelper.createXPath("//xhtml:c");
xpath.setNamespaceContext( new SimpleNamespaceContext( map));

List result = xpath.selectNodes(document);

This will return the 'c' element for the following code fragments:

<a xmlns="http://www.w3.org/1999/xhtml";>
   <b>
      <c><d /></c>
   </b>
</a>

<htm:a xmlns:htm="http://www.w3.org/1999/xhtml";>
   <htm:b>
      <htm:c><htm:d /></htm:c>
   </htm:b>
</htm:a>

<htm:a xmlns:htm="http://www.w3.org/1999/xhtml";>
   <htm:b>
      <xhtml:c xmlns:xhtml="http://www.w3.org/1999/xhtml";><htm:d /></xhtml:c>
   </htm:b>
</htm:a>

> Again I think you're equating "the default namespace" to "".

I use the term 'default namespace' for the following:

<a xmlns="http://www.w3.org/1999/xhtml";>

This sets the default namespace to "http://www.w3.org/1999/xhtml"; for
this element and its children.

To un-declare a 'default namespace' for the current element and its
children you can use the following construction:

<a xmlns="">

> I would agree that'd it'd be better to be able to setup the namespace map so
> that if you leave off the namespace, it'd use your mapped one.  However,
> the context is used to decide what the default will be for the
> expression, so would it not be better to default to the context's
> namespace or the default namespace within the scope of the context?
> When I say "the default namespace within the scope of the context"  what
> I'm getting at is:

I don't think you should depend on the namespaces in your context
because your code would now depend on the information your trying to
parse. (although Jaxen for dom4j partly does already and I can see how
this would confuse people)

> If <b> is our context, then the default namespace in <b>'s scope is
> defined by <a> and is "http://www.w3.org/1999/xhtml";.  So,
> <b>.selectNodes('c/d') would return <d> because they have the same
> namespace as the default namespace in <b>'s scope.  Jaxen would not
> return <d> because both <c> and <d> are in the
> "http://www.w3.org/1999/xhtml"; namespace and the xpath expression would
> default to "", which is a poor choice.

However the "c/d" XPath expression does not query a set of elements in
the "http://www.w3.org/1999/xhtml"; namespace, if you want this XPath
expression to work the way you suggest, you will have to change the
XPath specification first.

Regards,
Edwin
-- 
http://www.edankert.com/

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
dom4j-user mailing list
dom4j-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/dom4j-user

Reply via email to