XSD invalid content was found by namespace in element

Viewed 288

I want to vlidate a signed XML file against a XSD schema. The problem is that the Signature XML Element contains a namespace attribute and the namespace is not recognized by the XSD schema. So i always getting this error during validation:

<"org.xml.sax.SAXParseException; lineNumber: 2867; columnNumber: 55; cvc-complex-type.2.4.a: Invalid content was found starting with element '{"http://www.w3.org/2000/09/xmldsig#":Signature}'. One of '{Signature}' is expected.

Here is my XML section of the xml i want to parse:

    <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#dsa-sha1"/>
            <Reference URI="">
                <Transforms>
                    <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
                </Transforms>
                <DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
                <DigestValue>HNSlwP/h00NyO01CLexn+gbYxahOgjFfUKGJG3ggSMU=</DigestValue>
            </Reference>
        </SignedInfo><SignatureValue>EaLbG4wvdELbRw1uIou01WlJAX+J233aTUZI5c0yELkIjfBeV+XNbQ==</SignatureValue>
        <KeyInfo>
            <KeyValue>
                <DSAKeyValue><P>/KaCzo4Syrom78z3EQ5SbbB4sF7ey80etKII864WF64B81uRpH5t9jQTxeEu0ImbzRMqzVDZkVG9&#13;
xD7nN1kuFw==</P>
                    <Q>li7dzDacuo67Jg7mtqEm2TRuOMU=</Q><G>Z4Rxsnqc9E7pGknFFH2xqaryRPBaQ01khpMdLRQnG541Awtx/XPaF5Bpsy4pNWMOHCBiNU0Nogps&#13;
QW5QvnlMpA==</G>
<Y>7c2FbHxE5zTY6PkKQfHDdgGySWNSKYQ+j9nnD6FjwialpVuy6AKAbV8kdOYD3MMOMTJD8+N0+ZO9&#13;
Ofx6Nh6UIw==</Y>
                </DSAKeyValue>
            </KeyValue>
        </KeyInfo>       
    </Signature>

Here declare the position in my XML with the following tag:

      <xs:element ref="Signature"/>

And here my definition of the signature element:

  <xs:element name="Signature" type="SignatureType"/>
  <xs:complexType name="SignatureType">
    <xs:sequence>
      <xs:any namespace="http://www.w3.org/2000/09/xmldsig#"
        minOccurs="0" maxOccurs="unbounded"/>
      <xs:element ref="SignedInfo"/>
      <xs:element ref="SignatureValue"/>
      <xs:element ref="KeyInfo" minOccurs="0"/>
      <xs:element ref="Object" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
    <xs:attribute name="Id" type="xs:ID"/>
  </xs:complexType>```
2 Answers

@Michaek Kay Yeah good idea. So i imported the file:

  <xs:import namespace="http://www.w3.org/2000/09/xmldsig#" schemaLocation="http://www.w3.org/TR/xmldsig-core/xmldsig-core-schema.xsd"/>

and also declared "dsig" as namespace for the Signature type:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:dsig="http://www.w3.org/2000/09/xmldsig#"
  elementFormDefault="qualified"
>

And i added the namespace to my Signature element:

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

But now the element cannot be found: <"org.xml.sax.SAXParseException; lineNumber: 51; columnNumber: 41; src-resolve: Cannot resolve the name 'dsig:Signature' to a(n) 'element declaration' component.

I also downloaded that xsd file and put it into the same directory where my xsd is located. If i klick the the "dsig:Signature" element in my development environment, the file will be opened and the Signature element is visible. So i think the file can be accessed.

Since the Signature element is in its own namespace http://www.w3.org/2000/09/xmldsig#, it needs to be declared in a schema document that has that as its target namespace.

It appears that such a schema document already exists: see https://www.w3.org/TR/xmldsig-core1/#sec-xsdSchema, so all you need to do is import it with xs:import, rather than trying to redefine it yourself.

Related