Stefan,

Below is copied and pasted from a Java class I use:

      /**
            Applying a technique suggested by James Strachan of dom4j.org
to overcome the
            default namespace/XPath issue.  XPath maps namespaces to
elements based upon
            the namespaceURI, not the prefix in the source document.  When
there is a default
            prefix, i.e., "xmlns=<namespaceURI>", each non-prefixed element
STILL matches
            the URI, but there is no prefix available for use in XPath
expressions.  So we
            tell XPath to map the same namespaceURI to a non-"" prefix,
which we then use
            in our XPath expressions.  XPath will match the URIs, and find
the non-prefixed
            elements in the source document as a result.

            Ex:
            In source document:
            <tgML
                xmlns="http://www.aigtrading.com/tgML/2002/07";
            >
                <trade ID="309430320"/>
                <.../>
            </tgML>

            By defining an XPath-only prefix, "tg" for the same namespace,
I can match:
            elem.selectNodes("/tg:tgML/tg:trade/@ID")
            to get the value of the ID attribute.

      */
      public static Document getDom4jDocument (
            String xml,
            String xPathPrefix
      ) throws DocumentException
      {
            // register prefixes with my factory
            Map uris = new HashMap();
            // don't allow "default" prefix
            if (xPathPrefix == null || xPathPrefix.equals(""))
                xPathPrefix = "tg";

            uris.put (xPathPrefix,
"http://www.aigtrading.com/tgML/2002/07";);

            // create the actual Document
            return getDocFromString (xml, uris);
      }

      /**
       *
       * @param xml String containing any form of XML for which a document
is desired, but without any
       *   namespace.
       * @return org.dom4j.Document
       * @throws org.dom4j.DocumentException
       */
      public static Document getNoNamespaceDocument (String xml) throws
DocumentException
      {
            return getDocFromString (xml, null);
      }

      /**
       *
       * @param xml String containing any form of XML for which a document
is desired.
       * @param uris List of URIs that will be used as namespace
attributes.
       * @return org.dom4j.Document
       * @throws org.dom4j.DocumentException
       */
      protected static Document getDocFromString (String xml, Map uris)
throws DocumentException
      {
            // create my own factory
            DocumentFactory factory = new DocumentFactory();
            // maybe add namespace nodes
            if (uris != null)
                factory.setXPathNamespaceURIs (uris);

            // create a string Reader wrapped around the String parameter
            StringReader source = new StringReader (xml);

            // now parse and return a document using my factory
                  SAXReader reader = new SAXReader();
            reader.setDocumentFactory (factory);
            return reader.read (source);
      }




                                                                                       
                                                              
                      Stefan Lischke                                                   
                                                              
                      <[EMAIL PROTECTED]>              To:                             
                                                            
                      Sent by:                           cc:       [EMAIL PROTECTED]   
                                               
                      [EMAIL PROTECTED]        Subject:  [dom4j-user] selectingNodes 
with xpath and namespace                              
                      ceforge.net                                                      
                                                              
                                                                                       
                                                              
                                                                                       
                                                              
                      11/21/2003 01:43 PM                                              
                                                              
                                                                                       
                                                              
                                                                                       
                                                              




Hi,

I want to select a singleNode by Xpath with

Node member =
this.e.selectSingleNode("/association/member[topicRef/@xlink:href='
#TMSWSS03']");

It worked, when there was no namespace xlink for the @href attribute,
but with xlink namespace it does not work.

How can i specifiy a namespace when selecting a single node.

this.e.asXML() is as follow:
-------------------------------------------------------------------------------------------------------------------


<association xmlns:exist="http://exist.sourceforge.net/NS/exist";
exist:id="[EMAIL PROTECTED]"
exist:source
="/db/xtm4xmldb/ivsData.xtm/assocs/[EMAIL PROTECTED]"
id="[EMAIL PROTECTED]">
<instanceOf>
<topicRef xmlns:xlink="http://www.w3.org/1999/xlink";
xlink:href="ivsTypes.xtm#VERANSTALTET">
</topicRef>
</instanceOf>

<member>
<roleSpec>
<topicRef xmlns:xlink="http://www.w3.org/1999/xlink";
xlink:href="ivsTypes.xtm#PERSON">
</topicRef>
</roleSpec>
<topicRef xmlns:xlink="http://www.w3.org/1999/xlink";
xlink:href="[EMAIL PROTECTED]">
</topicRef>
</member>

<member>
<roleSpec>
<topicRef xmlns:xlink="http://www.w3.org/1999/xlink";
xlink:href="ivsTypes.xtm#VERANSTALTUNG">
</topicRef>
</roleSpec>
<topicRef xmlns:xlink="http://www.w3.org/1999/xlink"; xlink:href="
#TMSWSS03">
</topicRef>
</member>

</association>




-------------------------------------------------------
This SF.net email is sponsored by: SF.net Giveback Program.
Does SourceForge.net help you be more productive?  Does it
help you create better code?  SHARE THE LOVE, and help us help
YOU!  Click Here: http://sourceforge.net/donate/
_______________________________________________
dom4j-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/dom4j-user







-------------------------------------------------------
This SF.net email is sponsored by: SF.net Giveback Program.
Does SourceForge.net help you be more productive?  Does it
help you create better code?  SHARE THE LOVE, and help us help
YOU!  Click Here: http://sourceforge.net/donate/
_______________________________________________
dom4j-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/dom4j-user

Reply via email to