Dear All,

I have the following problem:

I want retrieve data from a document which includes elements from various
namespaces. As far as I am concerned, I have two basic options considering
the xpath expression that I could use. The first option is to locate an
element via its qualified name, and the second, is to use the local-name()
xpath function in order to access an element irrespective of the namespace
that belongs to.

I have implemented the following scenario (the source code, xsd and the
respective xml are included at the end of the email):

I create the appropriate document type object (e.g. root) (parsing the
respective xml file), and I retrieve the top-level element (e.g. message).

When I apply the selectPath() method in the message object using the
following xpath expressions (EXP1 and EXP2), all works fine



EXP1
~~~~~~~~~~~~
declare namespace ns1='urn:customurn';$this/ns1:firstname

EXP2
~~~~~~~~~~~~
declare namespace any='##local';$this/*[local-name()='firstname']





BUT, when I apply the selectpath method to a “copy” of the object message
(using the XmlObject copy() method), the EXP2 fails triggering the
following error:




ERROR
~~~~~
Unsupported node type in DOM! 11 instance
[EMAIL PROTECTED]





I really appreciate any help,

Thanks in advance,

John







XSD
~~~
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema";
                xmlns:myfoons="urn:customurn"
                targetNamespace="urn:customurn" elementFormDefault="qualified">

        <xs:element name="info" type="myfoons:information"/>

        <xs:complexType name="information">
                <xs:sequence>
                                <xs:element name="firstname" 
type="myfoons:Max32Text"/>
                                <xs:element name="lastname" 
type="myfoons:Max32Text"/>
                                <xs:element name="address" 
type="myfoons:Max32Text"/>
                        </xs:sequence>
        </xs:complexType>
        <xs:simpleType name="Max32Text">
                <xs:restriction base="xs:string">
                        <xs:minLength value="1"/>
                        <xs:maxLength value="32"/>
                </xs:restriction>
        </xs:simpleType>
</xs:schema>







XML
~~~
<?xml version="1.0" encoding="UTF-8"?>
<myfoons:info xmlns:myfoons="urn:customurn"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
xsi:schemaLocation="urn:customurn simpleSchema.xsd">
  <myfoons:firstname>fooName</myfoons:firstname>
  <myfoons:lastname>foolastname</myfoons:lastname>
  <myfoons:address>fooaddress</myfoons:address>
</myfoons:info>



config.xsdconfig
~~~~~~~~~~~~~~~~
<xb:config  xmlns:xb="http://www.bea.com/2002/09/xbean/config";>
                        <!--<xb:namespace uri="##local">-->
                        <xb:namespace uri="urn:customurn">
                        <xb:package>com.test.bug</xb:package>
                        </xb:namespace>
</xb:config>



SOURCE CODE
~~~~~~~~~~~

package testXmlBug;


import java.io.File;
import java.util.ArrayList;

import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.XmlOptions;
import org.apache.xmlbeans.XmlValidationError;

import com.test.bug.InfoDocument;
import com.test.bug.Information;


public class TestXmlBeans {
        public static void main(String[] args) {
                TestXmlBeans t = new TestXmlBeans();
                t.test();
        }



public void test() {


                InfoDocument root = null;
                try {


                        File msg = new File("src/testXmlBug/simpleXml.xml");

                        root = InfoDocument.Factory.parse(msg);

                        Information message = root.getInfo();

                        //PLAYS WITH and WITHOUT copy()
//                      String xpath = "declare namespace ns1='urn:customurn';" 
+
//                              "$this/ns1:firstname";

                        //PLAYS WITHOUT copy
//                      String xpath = "declare namespace any='##local';" +
//                      "$this/*[local-name()='firstname']";

                        String xpath = "declare namespace any='##local';" +
                        "$this/*[local-name()='firstname']";


                        XmlOptions opts = new XmlOptions();

                        //XmlObject res[] = message.selectPath(xpath, opts);
                        XmlObject res[] = message.copy().selectPath(xpath, 
opts);


                        System.out.println("~~~RESULTS~~~: " + res.length);
                        for(int i=0; i<res.length; i++) {
                                System.out.println("res: " + res[i]);
                        }

                } catch (Exception e) {
                        System.err.println(e.getMessage());
                        System.exit(1);
                }

                ArrayList errors = validate(root); // This function call 
validates the
message.
                System.err.println("Message is "+((errors.isEmpty())?
"valid.":"invalid."));


        }




public final ArrayList validate(XmlObject message){
        ArrayList errors = new ArrayList();
        XmlOptions xmlOptions = new XmlOptions();
        xmlOptions.setErrorListener(errors);
        message.validate(xmlOptions);
        ArrayList ret = new ArrayList();

        for(int i=0; i<errors.size(); i++){
                XmlValidationError error = (XmlValidationError)errors.get(i);
                ret.add(error.getMessage());
        }
        return ret;
}
}





---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to