【问题标题】:XSD: Element with namespace is not expectedXSD:不期望具有命名空间的元素
【发布时间】:2015-04-28 11:56:50
【问题描述】:

我有一个简单的 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 中的问题?

【问题讨论】:

    标签: xml validation xsd schema


    【解决方案1】:

    因为你在你的架构中声明

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

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

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

    【讨论】:

    • 我试过了,但这也会报错:schema.xsd:8: element element: Schemas parser error : Element '{http://www.w3.org/2001/XMLSchema}element', attribute 'name': 'ds:Signature' is not a valid value of the atomic type 'xs:NCName'. WXS schema schema.xsd failed to compile
    • 哦,我的错。应该使用的不是name,而是ref。我更新了答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-04
    • 2019-01-14
    • 1970-01-01
    相关资源
    最近更新 更多