XSD Validation : Namespace causing root element to not be found

2.8k views Asked by At

I'm trying to a update an existing schema to use its own namespace, so I can import it into another schema later and make it clear the types being used are from the imported schema.

I tried changing the default and targeted namespaces but it's caused the schema validation to break and hide the root node. From what I can see i've hidden my root element in another namespace but I am unsure how to configure this to get my desired result.

Here is a basic example of what I've tried

XML

<Parent Id="P">
   <Child Id="C"/>
</Parent>

XSD

<xsd:schema targetNamespace="http://myNameSpace.com" 
            xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
            xmlns="http://myNameSpace.com" 
            elementFormDefault="qualified" >

   <xsd:element name="Child">
      <xsd:complexType>
         <xsd:attribute name="Id" />
      </xsd:complexType>
   </xsd:element>


   <xsd:element name="Parent">
      <xsd:complexType>
         <xsd:sequence>
            <xsd:element ref="Child" minOccurs="0"/>
         </xsd:sequence>
         <xsd:attribute name="Id" />
      </xsd:complexType>
   </xsd:element>

</xsd:schema>

Validation

Not valid.

Error - Line 1, 19: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 19; cvc-elt.1: Cannot find the declaration of element 'Parent'.

2

There are 2 answers

2
kjhughes On BEST ANSWER

You need to make a few changes to your XML:

  • Actually place the root element in the namespace given by target namespace of the XSD by adding xmlns="http://myNameSpace.com" to P.
  • Optionally use xsi:schemaLocation to provide a hint to the XSD to use.

And to your XSD:

  • Define a namespace prefix and use it to reference the Child declaration from the Parent declaration.

Altogether then, this XML,

<?xml version="1.0" encoding="UTF-8"?>
<Parent Id="P"
        xmlns="http://myNameSpace.com"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://myNameSpace.com try.xsd">
   <Child Id="C"/>
</Parent>

will then be valid against this XSD,

<xsd:schema targetNamespace="http://myNameSpace.com" 
            xmlns:m="http://myNameSpace.com"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
            elementFormDefault="qualified" >

  <xsd:element name="Child">
    <xsd:complexType>
      <xsd:attribute name="Id" />
    </xsd:complexType>
  </xsd:element>

  <xsd:element name="Parent">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element ref="m:Child" minOccurs="0"/>
      </xsd:sequence>
      <xsd:attribute name="Id" />
    </xsd:complexType>
  </xsd:element>

</xsd:schema>

as requested.

0
Vadim On

when you have namespace in XSD you must have it in XML as well.

So, your XML must look like:

<Parent Id="P" xmlns="http://myNameSpace.com">
    <Child Id="C"/>
</Parent>

Or if you'd like to use prefix:

<mysp:Parent Id="P" xmlns:mysp="http://myNameSpace.com">
    <mysp:Child Id="C"/>
</mysp:Parent>