Question

What is the difference between (focus on prefixes h:, c:, a: and default):

<?xml version="1.0" encoding="UTF-8"?>
<xmlBoo xmlns="http://www.example.org/boo" 
        xmlns:c="http://www.example.org/customer"
        xmlns:a="http://www.example.org/address"
        xmlns:h="http://www.example.org/header">
   <h:header>
      <h:id>101</h:id>
   </h:header>
   <c:customer>
      <c:id>1</c:id>
      <c:name>John</c:name>
      <a:address>
         <a:street>Long street</a:street>
      </a:address>
   </c:customer>
   <someBooSpecificField>Specific data in Boo</someBooSpecificField>
</xmlBoo>

and

<?xml version="1.0" encoding="UTF-8"?>
<xmlBoo xmlns="http://www.example.org/boo" 
        xmlns:c="http://www.example.org/customer"
        xmlns:a="http://www.example.org/address"
        xmlns:h="http://www.example.org/header">
   <header>
      <h:id>101</h:id>
   </header>
   <customer>
      <c:id>1</c:id>
      <c:name>John</c:name>
      <address>
         <a:street>Long street</a:street>
      </address>
   </customer>
   <someBooSpecificField>Specific data in Boo</someBooSpecificField>
</xmlBoo>

I'm asking because I have implemented mapping via JAXB and marshaller created the first xml. But when I add schema validation I'm getting exception:

cvc-complex-type.2.4.a: Invalid content was found starting with element 'ns3:header'. One of '{"http://www.example.org/boo":header}' is expected.

It looks that validator expects (based on schema) the second xml.

Which prefix should be before element header? Default (connected with namespace [http://www.example.org/boo]) or h: (connected with namespace [http://www.example.org/header]).

I thought that the whole element header is connected completely with h: not just its sub elements like in second example. What is the best practices, what is the explanation for that.

My XML schema:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:h="http://www.example.org/header"
            xmlns:c="http://www.example.org/customer"
            targetNamespace="http://www.example.org/boo"
            elementFormDefault="qualified">

    <xsd:import namespace="http://www.example.org/header" schemaLocation="header.xsd"/>
    <xsd:import namespace="http://www.example.org/customer" schemaLocation="customer.xsd"/>

    <xsd:element name="xmlBoo">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="header" type="h:header"/>
                <xsd:element name="customer" type="c:customer"/>
                <xsd:element name="someBooSpecificField" type="xsd:string"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>

</xsd:schema>

and eg. header.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://www.example.org/header"
            elementFormDefault="qualified">

    <xsd:complexType name="header">
        <xsd:sequence>
            <xsd:element name="id" type="xsd:long"/>
        </xsd:sequence>
    </xsd:complexType>

</xsd:schema>
Was it helpful?

Solution

The second XML example is correct as indicated by your schema validation.

I think you are confusing the namespace of the type with the namespace of the element. The line <xsd:element name="header" type="h:header"> in your schema defines an element named "header" in the default namespace which is of the type "header" in the "http://www.example.org/header" namespace.

Consider how the xml would look if the line was instead <xsd:element name="myHeader" type="h:header">

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top