Eyal Lewin
Eyal Lewin

Reputation: 11

Two different xmlns:xs in XML Schema

Here is my problem: i have an xml file which has some values and also this file is certificate signed. my xsd schema file knows how to handle only the values, while when adding the signature lines the schema fails. the error is: The element 'Header' has invalid child element 'Signature' in namespace 'http://www.w3.org/2000/09/xmldsig#'. List of possible elements expected 'Signature' here is my code following, Thanks :)

File test.xml:

<Header>
<tank>
    <code>1</code>
    <level>0</level>
</tank>

<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
    <SignedInfo>
        ...
    <SignedInfo>
</Signature>
</Header>

SchemaTest.xsd:

<?xml version="1.0"?>
<xs:schema id="SchemaTest"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefalut="qualified"
attributeFormDefault="unqualified">
<xs:element name="Header">
    <xs:complexType>
    <xs:sequence>
        <xs:element name="tank">
        <xs:complexType>
        <xs:sequence>
            <xs:element name="code"/>
            <xs:element name="level"/>
            </xs:sequence>
        </xs:complexType>
        </xs:element>           
        <xs:element name="Signature">
                <xs:complexType>
                <xs:sequence>
                    <xs:element name="SignedInfo">
                    <xs:complexType>
                    <xs:sequence>
                    </xs:sequence>
                    </xs:complexType>
                    </xs:element>
                </xs:sequence>
                </xs:complexType>
        </xs:element>
        </xs:sequence>
        </xs:complexType>
</xs:element>
</xs:schema>

Upvotes: 0

Views: 4908

Answers (1)

OkieOth
OkieOth

Reputation: 3704

You need a second schema to describe the Signature namespace. The following code build a simple example for a xml file based on two different namespaces.

Here the second namespace schema ...

<?xml version="1.0" encoding="UTF-8"?>
<!-- second namespace schema -->
<schema xmlns="http://www.w3.org/2001/XMLSchema" 
    targetNamespace="http://my.second.namespace" 
    xmlns:tns="http://my.second.namespace" 
    elementFormDefault="qualified">
    <element name="SomethingElse" type="string"></element>
</schema>

The schema for the main namespace ...

<?xml version="1.0" encoding="UTF-8"?>
<!-- first namespace schema -->
<schema xmlns="http://www.w3.org/2001/XMLSchema" 
    targetNamespace="http://my.first.namespace" xmlns:tns="http://my.first.namespace"
    xmlns:S="http://my.first.namespace"      
    xmlns:T="http://my.second.namespace"
        elementFormDefault="qualified">
    <import namespace="http://my.second.namespace"
        schemaLocation="./SecondNamespaceSchema.xsd"/>
    <element name="Root">
        <complexType>
            <sequence>
                <element name="Head">
                    <complexType>
                        <sequence>
                            <element name="Something" type="string"></element>
                            <element ref="T:SomethingElse"/>
                        </sequence>
                    </complexType>
                </element>
                <element name="Body" type="string"></element>
            </sequence>
        </complexType>
    </element>
</schema>

... and a sample xml that puts all together.

<?xml version="1.0"?>
<!-- validatable xml file - proved with eclipse validator -->
<S:Root xmlns:S="http://my.first.namespace"
    xmlns:T="http://my.second.namespace" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://my.first.namespace ./Sample.xsd">
   <S:Head>
        <S:Something/>
        <T:SomethingElse/>
   </S:Head>
   <S:Body>
   </S:Body>
</S:Root>

Upvotes: 1

Related Questions