XSD namespace "ns2"

I have a schema defined in Request.xsd that will reference common.xsd. I expect the output should appear below

<Request xmlns="http://ws.myref.com/schemas/test" 
        xmlns="http://ps.myref.com/schemas/2008/Common">
 <EmailList>
     <Mail>test@gmail.com</Mmail>
  </EmailList>
</Request>

      

But I am getting an additional namespace "ns2". Can anyone help me solve this problem?

<ns2:Request xmlns:ns2="http://ps.myref.com/schemas/test" 
             xmlns="http://ps.myref.com/schemas/Common">
    <ns2:EmailList>
       <Mail>test@gmail.com</Mail>
    </ns2:EmailList>
</ns2:Request>

      

Request.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            elementFormDefault="qualified" targetNamespace="http://ps.myref.com/schemas/schemas/test"
            xmlns="http://ps.myref.com/schemas/schemas/test" xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
            xmlns:com="http://ps.myref.com/schemas/Common">
    <xsd:import namespace="http://ps.myref.com/schemas/Common" schemaLocation="../schemas/common/common.xsd"/>
    <xsd:element name="Request">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="EmailLists" type="com:EmailList" minOccurs="0" maxOccurs="1"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

      

Common.xsd

<?xml version="1.0"?>
<xsd:schema xmlns="http://ps.myref.com/schemas/2008/Common" elementFormDefault="unqualified"
            targetNamespace="http://ps.myref.com/schemas/Common"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <xsd:complexType name="EmailList">
        <xsd:sequence>
            <xsd:element name="Mail" type="xsd:string" minOccurs="0" maxOccurs="unbounded"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:schema>

      

+3


source to share


1 answer


In this case, your expectation is unreasonable.

Since the "EmailList" type is defined under the namespace http://ps.myref.com/schemas/2008/Common

in the common.xsd file, you have no option, but to differentiate it in some way when you use the EmailList type in a different schema. If you look at request.xsd, you can see this happening, what's happening here:

<xsd:element name="EmailLists" type="com:EmailList" />

      

com:

in this case it is a prefix intended to indicate that the type is defined in a different schema and under a different namespace to the one used.

Likewise, when the xsd validator uses request.xsd to validate the schema instance, it has to make sure that the EmailList type you are using in your instance is the same EmailList type that is defined in the common.xsd schema and the only way, which it can do is use a namespace.



So your expectation can be summed up like this:

"I have to be able to mix the types defined in two different schema definitions loosely together without differentiating them, and the analyzer needs to understand that."

So, you should be able to see how your expectation does not make logical sense.

If you don't want to use "ns2:" there, your only alternative is to do this:

<Request xmlns"http://ps.myref.com/schemas/test">
    <EmailList xmlns"http://ps.myref.com/schemas/test">
       <Mail xmlns="http://ps.myref.com/schemas/Common">test@gmail.com</Mail>
    </EmailList>
</Request>

      

+3


source







All Articles