Hi Mike,

here is a small example code which hopefully demonstrates my problem.
Every time when I add a new element, the XPath result list is empty.
The same XPath query after reading the document yields the expected
elements.
As you can see in the code, I used NamespaceContext/NamespaceURI but that
seems to have no effect at all.
But if you remove the namespace from the "xml:lang" attribute (keeping just
"lang") in the document and in the XPath query then everything is ok.
I don't know why.

Andreas

-- code --

import org.dom4j.*;
import org.dom4j.io.*;
import java.io.*;
import java.util.*;

public class DOMTest {

    public static void main (String args[]) throws Exception {
        DOMTest domTest = new DOMTest();
    }

    DocumentFactory factory;
    Document document;

    public DOMTest() throws Exception {
        factory = DocumentFactory.getInstance();
        Map uris = new HashMap();
        uris.put("xml", "http://www.w3.org/XML/1998/namespace";);
        uris.put("ims", "http://www.imsglobal.org/xsd/imsmd_v1p2";);
        factory.setXPathNamespaceURIs(uris);

        document = createDocument();
        XMLWriter writer = new XMLWriter(new
FileOutputStream("domtest.xml"), OutputFormat.createPrettyPrint());
        writer.write(document);
        writer.close();
        test("de");
        SAXReader sr = new SAXReader();
        document = sr.read(getClass().getResourceAsStream("domtest.xml"));
        System.out.println(document.asXML());
        test("de");
        addLang();
        System.out.println(document.asXML());
        test("en");
    }

    public Document createDocument() {
        Document document = factory.createDocument();
        Element kern = document.addElement("kern")
                               .addNamespace("xsi",
"http://www.w3.org/2001/XMLSchema-instance";)

.addAttribute("xsi:noNamespaceSchemaLocation", "movii-kern.xsd")
                               .addAttribute("xsi:schemaLocation",
"http://www.imsglobal.org/xsd/imsmd_v1p2 imsmd_v1p2p2.xsd")
                               .addNamespace("xml",
"http://www.w3.org/XML/1998/namespace";)
                               .addNamespace("ims",
"http://www.imsglobal.org/xsd/imsmd_v1p2";);
        Element metadata = kern.addElement("metadata");
        Element lom = metadata.addElement("ims:lom");
        Element general = lom.addElement("ims:general");
        Element title = general.addElement("ims:title");
        title.addElement("ims:langstring")
             .addAttribute("xml:lang", "de").setText("Licht");
        return document;
    }
    private void addLang() {
        Element title = (Element)document.selectSingleNode("//ims:title");
        if (title != null) {
            title.addElement("ims:langstring")
                 .addAttribute("xml:lang", "en").setText("Light");
        }
    }
    private void test(String lang) {
        String xpathString = "//ims:langstring[@xml:lang='" + lang + "']";
        Map uris = new HashMap();
        uris.put("xml", "http://www.w3.org/XML/1998/namespace";);
        uris.put("ims", "http://www.imsglobal.org/xsd/imsmd_v1p2";);
        XPath xpath = document.createXPath(xpathString);
        //xpath.setNamespaceContext(new
org.jaxen.SimpleNamespaceContext(uris));
        xpath.setNamespaceURIs(uris);
        System.out.println(xpath.selectNodes(document));
    }
}

-- code --

> -----Ursprüngliche Nachricht-----
> Von: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED]]Im Auftrag von Mike
> Skells
> Gesendet: Donnerstag, 29. August 2002 11:59
> An: [EMAIL PROTECTED]
> Cc: 'Dom4j-User@Lists. Sourceforge. Net'
> Betreff: RE: [dom4j-user] Does XPath consider the namespace of
> attributes?
>
>
> Andreas,
> No there are other ways, at least by setting the xpath mapping uri on
> the document factory
> Do you have some sample code?
>
> Mike
>
> > -----Original Message-----
> > From: [EMAIL PROTECTED]
> > [mailto:[EMAIL PROTECTED]]
> > Sent: Thursday 29 August 2002 09:44
> > To: Mike Skells
> > Cc: Dom4j-User@Lists. Sourceforge. Net
> > Subject: Re: [dom4j-user] Does XPath consider the namespace
> > of attributes?
> >
> >
> > Hi,
> >
> > you are rigth.
> > The problem does only appear when I'm creating a new document
> > in memory and then execute the mentioned XPath query. If I
> > read an existing XML document, like your example class b2,
> > everything is all right.
> >
> > So, is this behaviour normal to newly-created documents and
> > if yes, is the only alternative solution to store the
> > document and then read it again before executing the XPath query ?
> >
> > --
> > Andreas
> >
> >
> > > -----Ursprüngliche Nachricht-----
> > > Von: Mike Skells [mailto:[EMAIL PROTECTED]]
> > > Gesendet: Mittwoch, 28. August 2002 20:33
> > > An: [EMAIL PROTECTED]
> > > Cc: Dom4j-User@Lists. Sourceforge. Net
> > > Betreff: RE: [dom4j-user] Does XPath consider the namespace of
> > > attributes?
> > >
> > >
> > > I cant reproduce the problem
> > >
> > > --- code ---
> > > import org.dom4j.*;
> > > import org.dom4j.io.*;
> > >
> > > public class b2  {
> > >
> > >     public static void main(java.lang.String[] args) throws
> >  Exception {
> > >         new bug();
> > >     }
> > >     private Document doc ;
> > >     public b2() throws Exception{
> > >         SAXReader sr = new SAXReader();
> > >         Document doc =
> > sr.read(getClass().getResourceAsStream("b2.xml"));
> > >         System.out.println(doc.asXML());
> > >
> > > System.out.println(doc.selectNodes("//ims:title[@ims:lang='de']"));
> > >
> > >     }
> > > }
> > > ---- output -----
> > > <?xml version="1.0" encoding="UTF-8"?>
> > > <kern xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
> > > xmlns:ims="http://www.imsglobal.org/xsd/imsmd_v1p2";
> > > xsi:noNamespaceSchemaLocation="movii-kern.xsd"
> > > xsi:schemaLocation="http://www.imsglobal.org/xsd/imsmd_v1p2
> > > imsmd_v1p2p2.xsd">
> > >     <metadata>
> > >         <ims:title ims:lang="de">...</ims:title>
> > >     </metadata>
> > > </kern>
> > > [org.dom4j.tree.DefaultElement@908ca1 [Element: <ims:title uri:
> > > http://www.imsglobal.org/xsd/imsmd_v1p2 attributes:
> > > [org.dom4j.tree.DefaultAttribute@dd46f7 [Attribute: name ims:lang
> > > value "de"]]/>]]
> > > -- end ---
> > >
> > > > -----Original Message-----
> > > > From: [EMAIL PROTECTED]
> > > > [mailto:[EMAIL PROTECTED]] On Behalf Of
> > > > Andreas Wille
> > > > Sent: Wednesday 28 August 2002 15:24
> > > > To: [EMAIL PROTECTED]
> > > > Subject: [dom4j-user] Does XPath consider the namespace
> > of attributes?
> > > >
> > > >
> > > > Hello,
> > > >
> > > > my question is whether XPath does consider the namespace of
> > > > attributes or not ?
> > > >
> > > > for example:
> > > >
> > > > <?xml version="1.0" encoding="UTF-8"?>
> > > > <kern xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
> > > >       xmlns:ims="http://www.imsglobal.org/xsd/imsmd_v1p2";
> > > >       xsi:noNamespaceSchemaLocation="movii-kern.xsd"
> > > >
> > > > xsi:schemaLocation="http://www.imsglobal.org/xsd/imsmd_v1p2
> > > > imsmd_v1p2p2.xsd"> ... <metadata>
> > > >     <ims:title ims:lang="de">...</ims:title>
> > > > </metadata>
> > > > ...
> > > > </kern>
> > > >
> > > > If I start the following XPath query
> > > >
> > > > rootElement.selectNodes("//ims:title[@ims:lang='de']")
> > > >
> > > > on the XML document below, I get an empty List.
> > > > But if I remove the namespace for the 'lang' attribute in the
> > > > document (<ims:title lang="de">...</ims:title>) and in the
> > > > XPath query ("//ims:title[@lang='de']") I get the expected
> > > > list of elements.
> > > >
> > > >
> > > > Has somebody similar experiences or can tell me what
> > XPath is doing?
> > > >
> > > > --
> > > > Andreas
> > > >
> > > > --
> > > > berlin.de - meine stadt im netz. Jetzt eigene eMail-adresse
> > > > @berlin.de sichern! http://webmail.berlin.de
> > > >
> > > >
> > > >
> > > > -------------------------------------------------------
> > > > This sf.net email is sponsored by: Jabber - The world's
> > > > fastest growing
> > > > real-time communications platform! Don't just IM. Build it in!
> > > > http://www.jabber.com/osdn/xim
> > > > _______________________________________________
> > > > dom4j-user mailing list
> > > > [EMAIL PROTECTED]
> > > > https://lists.sourceforge.net/lists/listinfo/d> om4j-user
> > > >
> > >
> >
>
>
>
>
> -------------------------------------------------------
> This sf.net email is sponsored by:ThinkGeek
> Welcome to geek heaven.
> http://thinkgeek.com/sf
> _______________________________________________
> dom4j-user mailing list
> [EMAIL PROTECTED]
> https://lists.sourceforge.net/lists/listinfo/dom4j-user
>
>



-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
_______________________________________________
dom4j-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/dom4j-user

Reply via email to