XSD: 不应包含带有命名空间的元素

XSD: Element with namespace is not expected

我有一个简单的 XSD 架构:

<?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>

和 XML 文件,我要验证:

<?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>

我使用 xmllint,但出现以下错误:

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

我对 XML 命名空间做错了什么?问题出在 XSD 还是 XML?

自从您在架构中声明后

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

它希望有一个 <Signature> 未绑定到任何名称空间(不同于导入模式中声明的名称空间)。 似乎您想插入 "http://www.w3.org/2000/09/xmldsig#" 命名空间中定义的 <Signature>,因此您应该在架构中使用以下内容:

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