Question

I am creating an xml pendant to Couchdb, with validation of the documents using xsd.

Right now the embedding of project:project is not working and gives a validation error.

How can I change my xsd, so my xml file is valid?

I want the following xml layout:

 <document:document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xmlns:document="urn:JCouch.Document"
                   xmlns:attachment="urn:JCouch.Attachment"
                   xmlns:project="urn:JCouch.Project"
                   xsi:schemaLocation="
                   urn:JCouch.Document document.xsd
                   urn:JCouch.Project project.xsd
                   ">
    <document:meta>
        <document:id>123</document:id>
        <document:revision>12321</document:revision>
        <document:deleted>false</document:deleted>
        <document:md5>md5 of everything including</document:md5>
    </document:meta>
    <!-- Auto generated based on files attached -->
    <document:attachments>
        <attachment:attachment>
            <attachment:path></attachment:path>
            <attachment:length>1</attachment:length>
            <attachment:md5></attachment:md5>
        </attachment:attachment>
    </document:attachments>
    <!-- The actual document content -->
    <document:content>
        <project:project>
            <project:name>dsa</project:name>
            <project:projectNumber>123</project:projectNumber>
        </project:project>
    </document:content> 
    </document:document>

I have 3 xsd files. 1 for document, 1 for attachment and 1 for project. Have in mind project is just an example. I would later want to add blog:blog blog:entry and so forth.

Document.xsd:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       targetNamespace="urn:JCouch.Document"
       xmlns="urn:JCouch.Document"
       xmlns:attachment="urn:JCouch.Attachment"
       xmlns:project="urn:JCouch.Project"
       xsi:schemaLocation="
       urn:JCouch.Attachment attachment.xsd
       urn:JCouch.Project project.xsd"
       elementFormDefault="qualified"
    >

<xs:element name="document" type="documentType"/>

<xs:complexType name="documentType">
    <xs:sequence>
        <xs:element name="meta" minOccurs="1" maxOccurs="1">
            <xs:complexType>
                <xs:sequence>
                    <xs:element minOccurs="1" maxOccurs="1" name="id" type="xs:string"/>
                    <xs:element minOccurs="1" maxOccurs="1" name="revision" type="xs:string"/>
                    <xs:element minOccurs="1" maxOccurs="1" name="deleted" type="xs:boolean"/>
                    <xs:element minOccurs="1" maxOccurs="1" name="md5" type="xs:string"/>
                </xs:sequence>
            </xs:complexType>
        </xs:element>
        <xs:element name="attachments" minOccurs="1" maxOccurs="1" type="attachment:attachmentType"/>
        <xs:element name="content" minOccurs="1" maxOccurs="1" type="contentType">
        </xs:element>
    </xs:sequence>
</xs:complexType>


<xs:complexType name="contentType">
    <xs:sequence>
        <xs:element ref="project:project" minOccurs="0" maxOccurs="1"/>
    </xs:sequence>
</xs:complexType>
</xs:schema>

project.xsd:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
       targetNamespace="urn:JCouch.Project"
       elementFormDefault="qualified">

<xs:complexType name="projectType">
    <xs:sequence>
        <xs:sequence>
            <xs:element name="name" type="xs:string"/>
            <xs:element name="projectNumber" type="xs:string"/>
        </xs:sequence>
    </xs:sequence>
</xs:complexType>

<xs:element name="project" type="projectType"/>

</xs:schema>

and lastly attachment.xsd, though this part works!

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
       targetNamespace="urn:JCouch.Attachment"
       elementFormDefault="qualified">

<xs:complexType name="attachmentType">
    <xs:sequence>
        <xs:element name="attachment">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name="path" type="xs:anyURI"/>
                    <xs:element name="length" type="xs:integer"/>
                    <xs:element name="md5" type="xs:string"/>
                </xs:sequence>
            </xs:complexType>
        </xs:element>
    </xs:sequence>
</xs:complexType>
</xs:schema>

Any help will be greatly appreciated.

Best regards Kresten Kjaer

Was it helpful?

Solution

Since all three schemas have different target namespaces, you may use one of them as the main schema (or the driver schema), which you will specify to the validator, and all other schemas must be imported in it.

Let's Document.xsd will be the main schema (after all, it defines the root element of your XML). Then, you can write it like this:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       targetNamespace="urn:JCouch.Document"
       xmlns="urn:JCouch.Document"
       xmlns:attachment="urn:JCouch.Attachment"
       xmlns:project="urn:JCouch.Project"
       xsi:schemaLocation="
       urn:JCouch.Attachment attachment.xsd
       urn:JCouch.Project project.xsd"
       elementFormDefault="qualified"
    >

  <!-- import all other related schemas here -->
  <xs:import namespace="urn:JCouch.Project" schemaLocation="project.xsd"/>
  <xs:import namespace="urn:JCouch.Attachment" schemaLocation="attachment.xsd"/>

  <!-- all other definitions of Document.xsd -->
  ...

</xs:schema>

Now, Document.xsd should validate your XML.

(In fact, you can make a driver from any other schema by specifying in it similar imports of all other schemas.)

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