trivelt
trivelt

Reputation: 1965

XSD: Element with namespace is not expected

I have a simple XSD Schema:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns="urn:myNamespace" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" elementFormDefault="qualified" attributeFormDefault="unqualified" id="myList">
    <xs:import namespace="http://www.w3.org/2000/09/xmldsig#" schemaLocation="http://www.w3.org/TR/2002/REC-xmldsig-core-20020212/xmldsig-core-schema.xsd"/>
    <xs:element name="abc">
      <xs:complexType>
        <xs:sequence>
                <xs:element name="testElement" />
                <xs:element name="Signature" type="ds:SignatureType"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

and XML file, which I want to validate:

<?xml version="1.0" encoding="UTF-8"?>
<abc>
    <testElement>
    </testElement>
    <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
        <SignedInfo>
            <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
            <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
            <Reference URI="">
                <Transforms>
                    <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
                </Transforms>
                <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
                <DigestValue>value1</DigestValue>
            </Reference>
        </SignedInfo>
        <SignatureValue>value2</SignatureValue>
        <KeyInfo><KeyName/></KeyInfo>
    </Signature>
</abc>

I use xmllint and I'm getting the following error:

file.xml:5: element Signature: Schemas validity error : Element '{http://www.w3.org/2000/09/xmldsig#}Signature': This element is not expected. Expected is ( Signature ).
file.xml fails to validate

What am I doing wrong with XML namespaces? Is the problem in XSD or in XML?

Upvotes: 1

Views: 3192

Answers (1)

potame
potame

Reputation: 7905

Since you declared in your schema

<xs:element name="Signature" type="ds:SignatureType"/>

it expects to have a <Signature> not bound to any namespace (a different than the one declared in the imported schema). It seems you want to insert the <Signature> defined in the "http://www.w3.org/2000/09/xmldsig#" namespace, thus you should use the following in your schema :

<xs:element ref="ds:Signature" />

Upvotes: 3

Related Questions