[jdom-interest] Two namespace-questions (revisited)

Laurent Bihanic laurent.bihanic at atosorigin.com
Wed Jun 6 08:57:02 PDT 2007


Hi,

JDOM built-in XPath support relies on Jaxen but adds support for automatic 
prefix matching, i.e. if you use prefix "foo" in your XPath and the document 
declares the same prefix for a namespace, it will automatically match.
Yet, there is no specific support for the empty prefix.

The JavaDoc for org.jaxen.NamespaceContext states : "In XPath, there is no 
such thing as a 'default namespace'. The empty prefix always resolves to the 
empty namespace URI."
While the JavaDoc of NamespaceContext.translateNamespacePrefixToUri() says 
"This method should return null for the empty prefix.", this method is not 
called when there is no prefix in the XPath (with Jaxen 1.1).

If you could get your users to add a prefix, any prefix, you could use a 
custom NamespaceContext, inspired from org.jdom.xpath.JaxenXPath.NSContext, 
and return the namespace URI for the current node when no declared or known 
prefix matches.

With the attached code, considering this document:
<foo:rootNode xmlns:foo="http://www.foo.com/" name="root">
     <foo:subNode name="subNode">text</foo:subNode>
</foo:rootNode>

you can extract the text "text" using the XPath "//x:subNode/text()" but 
//subNode/text() won't work.

Command is: java MyXPathTest ns2.xml '//x:subNode/text()'

Laurent


Kai Wörner a écrit :
> Hello again,
> 
> I'm stumbling over this same problem again and again! Now I'd like to
> simply show parts of the same input XML file as last time inside a
> Java program based on an XPath-expression. I simply provide an input
> field for the XPath-string and seeing the problems that the users
> already have drafting a XPath-expression, I simply _know_ that they'll
> fail if they have to add a namespace-declaration before each and every
> element-name they use (especially since they don't have to do this in
> any of the XML-Editors they use).
> In addition, if I use an XMLOutputter to output the matched Elements,
> they come out with another load of namespace-bulk none of my users
> will understand. So in my tool I'd have to use an expression like:
> 
> //tei:s
> 
> to get a result that looks like that:
> 
> <s xmlns="http://www.tei-c.org/ns/1.0" part="N" TEIform="s">
>                        <cl type="lMAI tDEC" part="N" TEIform="cl">
> 
> as opposed to any XML-Editor where I'd use:
> 
> //s
> 
> to get:
> 
>               <s>
>                    <cl type="lMAI tDEC">
> 
> Is it possible /at all/ to mimic the way it works in a XML-Editor at
> all in JDom or do I have to look someplace else?
> 
> Thanks again,
> 
> Kai
> 
> 
> XML: http://files.exmaralda.org/bl.xml
> 
> 
> 
> 
> 
>> There is no default namespace in XPath 1.0.
>> So you have to use a namespace prefix in your XPath expressions. You can
>> choose any prefix as there is no relationship between the prefixes 
>> used in
>> XPath and the ones in the document: prefixes are just shortcuts and the
>> matching is done on the actual namespace URIs.
>>
>> The following should work:
>> XPath xp = XPath.newInstance("/x:*");
>> xp.addNamespace("x", "http://www.tei-c.org/ns/1.0");
>> xp.selectNodes(document);
>>
> _______________________________________________
> To control your jdom-interest membership:
> http://www.jdom.org/mailman/options/jdom-interest/youraddr@yourhost.com
> 
> 

-- 
                  wWw
                 (o o)
-------------ooO-(_)-Ooo-----------------------------------------------
Laurent Bihanic                | Tel: +33 (0)1 55 91 24 95 (direct)
Division Finance               |      +33 (0)1 55 91 20 00
Atos Origin System Integration | Mob: +33 (0)6 25 73 03 74
Les Miroirs - Bat. C           | Fax: +33 (0)1 70 92 12 47
18, avenue d'Alsace            |
F-92926 La Defense Cedex       | mailto: laurent.bihanic at atosorigin.com
-----------------------------------------------------------------------

Very funny, Scotty. Now beam down my clothes.


DISCLAIMER:
The opinions expressed are entirely my own and may not necessarily be
those of my employer.  Also, I am not now nor have I ever been a
lawyer.  My opinions are provided as-is with absolutely no warrantee of
merchantability or fitness for any particular use.  Besides, you can't
prove I typed this.  No body saw me type this.  Who says I typed this?
-------------- next part --------------

import java.util.*;

import org.jaxen.*;
import org.jaxen.jdom.*;
import org.jdom.*;


public class MyXPath
{
   /**
    * The compiled XPath object to select nodes.  This attribute can
    * not be made final as it needs to be set upon object
    * deserialization.
    */
   private transient JDOMXPath xPath;

   /**
    * The current context for XPath expression evaluation.
    */
   private           Object    currentContext;

   /**
    * Creates a new XPath wrapper object, compiling the specified
    * XPath expression.
    *
    * @param  expr   the XPath expression to wrap.
    *
    * @throws JDOMException   if the XPath expression is invalid.
    */
   public MyXPath(String expr) throws JDOMException {
      setXPath(expr);
   }

   /**
    * Evaluates the wrapped XPath expression and returns the list
    * of selected items.
    *
    * @param  context   the node to use as context for evaluating
    *                   the XPath expression.
    *
    * @return the list of selected items, which may be of types: {@link Element},
    *         {@link Attribute}, {@link Text}, {@link CDATA},
    *         {@link Comment}, {@link ProcessingInstruction}, Boolean,
    *         Double, or String.
    *
    * @throws JDOMException   if the evaluation of the XPath
    *                         expression on the specified context
    *                         failed.
    */
   public List selectNodes(Object context) throws JDOMException {
      try {
         currentContext = context;

         return xPath.selectNodes(context);
      }
      catch (JaxenException ex1) {
         throw new JDOMException("XPath error while evaluating \"" +
                        xPath.toString() + "\": " + ex1.getMessage(), ex1);
      }
      finally {
         currentContext = null;
      }
   }

   /**
    * Evaluates the wrapped XPath expression and returns the first
    * entry in the list of selected nodes (or atomics).
    *
    * @param  context   the node to use as context for evaluating
    *                   the XPath expression.
    *
    * @return the first selected item, which may be of types: {@link Element},
    *         {@link Attribute}, {@link Text}, {@link CDATA},
    *         {@link Comment}, {@link ProcessingInstruction}, Boolean,
    *         Double, String, or <code>null</code> if no item was selected.
    *
    * @throws JDOMException   if the evaluation of the XPath
    *                         expression on the specified context
    *                         failed.
    */
   public Object selectSingleNode(Object context) throws JDOMException {
      try {
         currentContext = context;

         return xPath.selectSingleNode(context);
      }
      catch (JaxenException ex1) {
         throw new JDOMException("XPath error while evaluating \"" +
                        xPath.toString() + "\": " + ex1.getMessage(), ex1);
      }
      finally {
         currentContext = null;
      }
   }

   /**
    * Returns the string value of the first node selected by applying
    * the wrapped XPath expression to the given context.
    *
    * @param  context   the element to use as context for evaluating
    *                   the XPath expression.
    *
    * @return the string value of the first node selected by applying
    *         the wrapped XPath expression to the given context.
    *
    * @throws JDOMException   if the XPath expression is invalid or
    *                         its evaluation on the specified context
    *                         failed.
    */
   public String valueOf(Object context) throws JDOMException {
      try {
         currentContext = context;

         return xPath.stringValueOf(context);
      }
      catch (JaxenException ex1) {
         throw new JDOMException("XPath error while evaluating \"" +
                        xPath.toString() + "\": " + ex1.getMessage(), ex1);
      }
      finally {
         currentContext = null;
      }
   }

   /**
    * Returns the number value of the first item selected by applying
    * the wrapped XPath expression to the given context.
    *
    * @param  context   the element to use as context for evaluating
    *                   the XPath expression.
    *
    * @return the number value of the first item selected by applying
    *         the wrapped XPath expression to the given context,
    *         <code>null</code> if no node was selected or the
    *         special value {@link java.lang.Double#NaN}
    *         (Not-a-Number) if the selected value can not be
    *         converted into a number value.
    *
    * @throws JDOMException   if the XPath expression is invalid or
    *                         its evaluation on the specified context
    *                         failed.
    */
   public Number numberValueOf(Object context) throws JDOMException {
      try {
         currentContext = context;

         return xPath.numberValueOf(context);
      }
      catch (JaxenException ex1) {
         throw new JDOMException("XPath error while evaluating \"" +
                        xPath.toString() + "\": " + ex1.getMessage(), ex1);
      }
      finally {
         currentContext = null;
      }
   }

   /**
    * Defines an XPath variable and sets its value.
    *
    * @param  name    the variable name.
    * @param  value   the variable value.
    *
    * @throws IllegalArgumentException   if <code>name</code> is not
    *                                    a valid XPath variable name
    *                                    or if the value type is not
    *                                    supported by the underlying
    *                                    implementation
    */
   public void setVariable(String name, Object value)
                                        throws IllegalArgumentException {
      Object o = xPath.getVariableContext();
      if (o instanceof SimpleVariableContext) {
           ((SimpleVariableContext)o).setVariableValue(null, name, value);
      }
   }

   /**
    * Adds a namespace definition to the list of namespaces known of
    * this XPath expression.
    * <p>
    * <strong>Note</strong>: In XPath, there is no such thing as a
    * 'default namespace'.  The empty prefix <b>always</b> resolves
    * to the empty namespace URI.</p>
    *
    * @param  namespace   the namespace.
    */
   public void addNamespace(Namespace namespace) {
      try {
         xPath.addNamespace(namespace.getPrefix(), namespace.getURI());
      }
      catch (JaxenException ex1) { /* Can't happen here. */ }
   }

   /**
    * Returns the wrapped XPath expression as a string.
    *
    * @return the wrapped XPath expression as a string.
    */
   public String getXPath() {
      return (xPath.toString());
   }

   /**
    * Compiles and sets the XPath expression wrapped by this object.
    *
    * @param  expr   the XPath expression to wrap.
    *
    * @throws JDOMException   if the XPath expression is invalid.
    */
   private void setXPath(String expr) throws JDOMException {
      try {
         xPath = new JDOMXPath(expr);
         xPath.setNamespaceContext(new NSContext());
      }
      catch (Exception ex1) {
         throw new JDOMException(
                        "Invalid XPath expression: \"" + expr + "\"", ex1);
      }
   }

   public String toString() {
      return (xPath.toString());
   }

   public boolean equals(Object o) {
      if (o instanceof MyXPath) {
         MyXPath x = (MyXPath)o;

         return (super.equals(o) &&
                 xPath.toString().equals(x.xPath.toString()));
      }
      return false;
   }

   public int hashCode() {
      return xPath.hashCode();
   }

   private class NSContext extends SimpleNamespaceContext {
      public NSContext() {
         super();
      }

      /**
       * <i>[Jaxen NamespaceContext interface support]</i> Translates
       * the provided namespace prefix into the matching bound
       * namespace URI.
       *
       * @param  prefix   the namespace prefix to resolve.
       *
       * @return the namespace URI matching the prefix.
       */
      public String translateNamespacePrefixToUri(String prefix) {
         if ((prefix == null) || (prefix.length() == 0)) {
            return null;
         }

         String uri = super.translateNamespacePrefixToUri(prefix);
         if (uri == null) {
            Object ctx = currentContext;
            if (ctx != null) {
               Element elt = null;

               // Get closer element node
               if (ctx instanceof Element) {
                  elt = (Element)ctx;
               } else if (ctx instanceof Attribute) {
                  elt = ((Attribute)ctx).getParent();
               } else if (ctx instanceof Content) {
                  elt = ((Content) ctx).getParentElement();
               } else if (ctx instanceof Document) {
                  elt = ((Document)ctx).getRootElement();
               }

               if (elt != null) {
                  Namespace ns = elt.getNamespace(prefix);
                  if (ns != null) {
                     uri = ns.getURI();
                  } else {
                     uri = elt.getNamespaceURI();
                  }
               }
            }
         }
         return uri;
      }
   }
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ns2.xml
Type: text/xml
Size: 128 bytes
Desc: not available
Url : http://www.jdom.org/pipermail/jdom-interest/attachments/20070606/2e6b4f8f/ns2.xml
-------------- next part --------------

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;

import java.util.List;
import java.util.Iterator;

public class MyXPathTest
{
    public static void main(String[] args)
    {
        if (args.length != 2)
        {
            System.err.println("usage: MyXPathTest <document url> <xpath expr>");
            System.exit(1);
        }

        try
        {
            Document doc     = new SAXBuilder().build(args[0]);
            XMLOutputter out = new XMLOutputter();

            MyXPath xpath = new MyXPath(args[1]);

            System.out.println("Document: " + args[0]);
            System.out.println("XPath:    \"" + args[1] + "\"");
            System.out.println();
            System.out.println("Results:");
            System.out.println("--------");

            Iterator i = xpath.selectNodes(doc).iterator();
            while (i.hasNext())
            {
                Object o = i.next();

                try
                {
                    out.output((Element)o, System.out);
                }
                catch (Exception e)
                {
                    System.out.print(o);
                }
                System.out.println();
            }
        }
        catch (JDOMException e)
        {
            e.printStackTrace();
        }
        catch (java.io.IOException e)
        {
            e.printStackTrace();
        }
    }
}


More information about the jdom-interest mailing list