Hey Stephen,

The extra namespaces are actually needed in this particular case because attribute nodes in XML do not inherit the "default" namespace. An unprefixed attribute node doesn't belong to a namespace. So in this case, in order to preserve the namespaces of the attributes Castor is outputing the ns1 declaration. Castor ends up declaring the namespace multiple times (one for each child element) because at the time that it's marshalling the attribute that requires the namespace, the namespace is not currently in scope. If the namespace is declared up-front on the root this wouldn't occur multiple times.

The only real workaround to the attribute namespace issue would be to make sure the attribute nodes are not defined using any namespace.

HTH,

--Keith

Stephen Bash wrote:
Keith and Jitesh-

I get the behavior Jitesh is seeing if I leave the ns-uri attribute out of the <map-to> element in the mapping file (note: I have no ns-prefix attribute). If I put the ns-uri argument in, Castor knows the correct namespace for everything, but seems to generate extra namespace declarations (this sounds familiar... has this come up before?).

I'm unmarshalling and marshalling with full schema validation turned on in the parser (since this was Jitesh's original question), and everything works until Castor adds the ns1 namespaces on the round trip.

Any thoughts?

Stephen

P.S. For completeness, here is the code I'm using to generate the XML:

  Mapping myMap = new Mapping();
  myMap.loadMapping(LoggingDriver.class.getResource("loggingMap.xml"));

  Unmarshaller um1 = new Unmarshaller(myMap);
  TMSettings settings =
     (TMSettings)um1.unmarshal(new FileReader("logging.xml"));

  StringWriter myWriter = new StringWriter();
  Marshaller m1 = new Marshaller(myWriter);
  m1.setMapping(myMap);
  m1.setNamespaceMapping("", "http://www.ne.jp/method";);
  m1.setSchemaLocation("http://www.ne.jp/method method_settings.xsd");
  m1.marshal(settings);

  System.out.println("Castor Output: ");
  System.out.println(myWriter.getBuffer().toString());

Without the ns-uri in the map-to element, this produces:

  <?xml version="1.0" encoding="UTF-8"?>
  <tmsettings xmlns=""
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
    xsi:schemaLocation="http://www.ne.jp/method method_settings.xsd">
    <logging>
      <ns1:logger xmlns:ns1="http://www.ne.jp/method";
        ns1:name="tmcompiler" ns1:boundrylevel="10"/>
      <ns2:logger xmlns:ns2="http://www.ne.jp/method";
        ns2:name="tmwebapp" ns2:boundrylevel="10"/>
      <ns3:logger xmlns:ns3="http://www.ne.jp/method";
        ns3:name="tmclient" ns3:boundrylevel="10"/>
      </logging>
  </tmsettings>

With the ns-uri:

  <?xml version="1.0" encoding="UTF-8"?>
  <tmsettings xmlns="http://www.ne.jp/method";
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
    xsi:schemaLocation="http://www.ne.jp/method method_settings.xsd">
    <logging>
      <logger xmlns:ns1="http://www.ne.jp/method";
        ns1:name="tmcompiler" ns1:boundrylevel="10"/>
      <logger xmlns:ns2="http://www.ne.jp/method"; ns2:name="tmwebapp"
        ns2:boundrylevel="10"/>
      <logger xmlns:ns3="http://www.ne.jp/method"; ns3:name="tmclient"
        ns3:boundrylevel="10"/>
      </logging>
  </tmsettings>

And finally, just in case, here's the mapping file:

  <ca:mapping xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
    xmlns:ca="http://castor.exolab.org/";
    xmlns="http://www.ne.jp/method";
    xsi:schemaLocation="http://castor.exolab.org/ mapping.xsd">
    <ca:class name="TMSettings">
      <ca:map-to xml="tmsettings" ns-uri="http://www.ne.jp/method"/>
      <ca:field name="loggerList" collection="map"
        get-method="getLoggerList" set-method="addLogger"
        type="castor.kalyani.two.TMLogger">
        <ca:bind-xml name="logger" location="logging"/>
      </ca:field>
    </ca:class>

    <ca:class name="TMLogger">
      <ca:field name="name" type="string">
        <ca:bind-xml name="name" node="attribute"/>
      </ca:field>
      <ca:field name="boundryLevel" type="string">
        <ca:bind-xml name="boundrylevel" node="attribute"/>
      </ca:field>
    </ca:class>
  </ca:mapping>

Kalyani Jitesh wrote:

Hi Keith,

Thanks for your reply.

#When you declare the namespace as such: myMarshaller.setNamespaceMapping("", "http://www.ne.jp/method/";); You get the below output?

Yes Keith I am getting below output. In this case I think I will have to instance my XML with namespace prefix OR without namespace. I can not use the default namespace attribute in my xml. Actully I do not wish to prefix my elements as this file is manually updated by user.
So in such cases deafult namespace is very useful.

Thanks And Regards,
Jitesh



----- Original Message ----- From: "Keith Visco" <[EMAIL PROTECTED]>
To: <[email protected]>
Sent: Tuesday, August 23, 2005 1:43 PM
Subject: Re: [castor-user] xsi:schemaLocation Attribute


Jitesh,

When you declare the namespace as such:
myMarshaller.setNamespaceMapping("", "http://www.ne.jp/method/";);
You get the below output?

If so then it may be a bug, as it shouldn't try and use "ns1" in that case.

Also, I agree that if the xsi prefix is used anywhere that it should
declare the namespace, even with supress namespaces enabled because the
resulting xml is invalid. So that is definately a bug.

--Keith


Kalyani Jitesh wrote:

HI Andrew,
Thanks for your suggestion. When I tried that with null and empty ("") prefix, it is producing output like below,
<?xml version="1.0" encoding="UTF-8"?>
<tmsettings xmlns=""
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
    xsi:schemaLocation="http://www.nec.jp/method settings.xsd">
    <ns1:logging xmlns:ns1="http://www.ne.jp/method/";>
        <ns1:logger ns1:name="tmcompiler" ns1:boundrylevel="10"/>
        <ns1:logger ns1:name="tmwebapp" ns1:boundrylevel="10"/>
        <ns1:logger ns1:name="tmclient" ns1:boundrylevel="10"/>
    </ns1:logging>
</tmsettings>
 Any help apprecited.
 Thanks And Regards,
Jitesh

    ----- Original Message -----
    *From:* Andrew Fawcett <mailto:[EMAIL PROTECTED]>
    *To:* [email protected] <mailto:[email protected]>
    *Sent:* Monday, August 22, 2005 9:16 PM
    *Subject:* RE: [castor-user] xsi:schemaLocation Attribute

    If you just want to avoid prefixing your elements, you should be
    able to map your namespace to an empty string “” prefix without
    having to disable namespaces.


------------------------------------------------------------------------

    *From:* Kalyani Jitesh [mailto:[EMAIL PROTECTED]
    *Sent:* 22 August 2005 13:15
    *To:* [email protected] <mailto:[email protected]>
    *Subject:* [castor-user] xsi:schemaLocation Attribute


    Dear All,


    I am having problem with **xsi:schemaLocation** Attribute during
    marshalling. "xsi:" namespace in schemaLocation is also suppresed by
    method marshaller.setSuppressNamespaces(true). This makes my XML
    invalid.

    I am using defaultnamespace (xmlns) attribute in root, because I do
    not wish to tag my xml elements with prefix.

    I am validating the xml against schema during Unmarshalling by
    setting org.exolab.castor.parser.validation=true.

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    1. Initially my XML looks like below, (Before Unmarshalling)


    _my XML:_

    <?xml version="1.0" encoding="UTF-8"?>
    <tmsettings xmlns="http://www.ne.jp/method";
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
        **xsi:schemaLocation**="http://www.ne.jp/method settings.xsd">
    <logging> <logger name="tmcompiler" boundrylevel="10"/>
<logger name="tmclient" boundrylevel="10"/> </logging> </tmsettings> ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    2. After marshalling, It look like below,

    <?xml version="1.0" encoding="UTF-8"?>
    <tmsettings xmlns="http://www.ne.jp/method";
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; **schemaLocation**="http://www.ne.jp/method settings.xsd">

    ...
    </tmsettings>


    Here "xsi:" namespace is also surppresed by following code. Now this
XML I can not validate next time. So I need to produc same XML as in 1.

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    I am using following code for Marshalling,

    Marshaller marshaller = new Marshaller(new
    OutputStreamWriter(System.out));
    marshaller.setMapping(mapping);
marshaller.setSchemaLocation("http://www.nec.jp/method settings.xsd");

marshaller.setNamespaceMapping(null,"http://www.nec.jp/mcone/tracemethod";);
    **marshaller.setSuppressNamespaces(true);***
    *marshaller.marshal(tmsettings);
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    I am using **marshaller.setSuppressNamespaces(true); **because if I
do'nt use it adds some default namespace prefix "ns1" to all elements.

    And xmlns=null.


    Can anybody help me to get exact XML as in 1 after Marshlling?
    Thanks in Advance.


    Thanks And Regards,

    Jitesh

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


    **PS : my mapping.xml is as below,**

    <?xml version="1.0"?>
    <ca:mapping  xmlns:ca="http://castor.exolab.org/";
       xmlns="http://www.ne.jp/method/";
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
        xsi:schemaLocation="http://castor.exolab.org/ mapping.xsd">


    <ca:class name="TMSettings">
     <ca:map-to xml="tmsettings"/>
     <ca:field name="loggerList" type="TMLoggerList">
      <ca:bind-xml name="logging"/>
     </ca:field>
     </ca:class>


    <ca:class name="TMLoggerList">
     <ca:map-to xml="logging"/>
      <ca:field name="loggerList" collection="map"
    get-method="getLoggerList"
       set-method="addLogger" type="TMLogger">
       <ca:bind-xml name="logger"/>
      </ca:field>
     </ca:class>
     <ca:class name="TMLogger">
     <ca:map-to xml="logger"/>
      <ca:field name="name" type="string">
       <ca:bind-xml name="name" node="attribute"/>
        </ca:field>
      <ca:field name="boundryLevel" type="string">
       <ca:bind-xml name="boundrylevel" node="attribute"/>
        </ca:field>
     </ca:class>


    </ca:mapping>

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------



    CODA has a new world-class consolidation package.
    Find out more about www.coda.com/ocra <http://www.coda.com/ocra>



------------------------------------------------------------------------
    *
    **The information in this message is confidential and may be legally
    privileged. It may not be disclosed to, or used by, anyone other
    than the addressee. If you receive this message in error, please
    advise us immediately.

    Internet emails are not necessarily secure. CODA does not accept
    responsibility for changes to any email which occur after the email
    has been sent. Attachments to this email may contain software
    viruses, which could damage your systems. CODA has checked the
    attachments for viruses before sending, but you should virus-check
    them before opening.*



-------------------------------------------------
If you wish to unsubscribe from this list, please
send an empty message to the following address:

[EMAIL PROTECTED]
-------------------------------------------------


-------------------------------------------------
If you wish to unsubscribe from this list, please send an empty message to the following address:

[EMAIL PROTECTED]
-------------------------------------------------



-------------------------------------------------
If you wish to unsubscribe from this list, please send an empty message to the following address:

[EMAIL PROTECTED]
-------------------------------------------------




-------------------------------------------------
If you wish to unsubscribe from this list, please send an empty message to the following address:

[EMAIL PROTECTED]
-------------------------------------------------

Reply via email to