【问题标题】:XSD invalid content was found by namespace in element元素中的命名空间发现 XSD 无效内容
【发布时间】:2020-02-08 13:48:05
【问题描述】:

我想根据 XSD 架构验证签名的 XML 文件。问题是签名 XML 元素包含命名空间属性,并且 XSD 架构无法识别命名空间。所以我在验证过程中总是遇到这个错误:

<"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.

这是我要解析的 xml 的 XML 部分:

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

在这里使用以下标记在我的 XML 中声明位置:

      <xs:element ref="Signature"/>

这里是我对签名元素的定义:

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

【问题讨论】:

    标签: xml xsd namespaces signature


    【解决方案1】:

    @迈克尔·凯 是的好主意。所以我导入了文件:

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

    并且还将“dsig”声明为签名类型的命名空间:

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

    我将命名空间添加到我的签名元素中:

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

    但是现在找不到元素:&lt;"org.xml.sax.SAXParseException; lineNumber: 51; columnNumber: 41; src-resolve: Cannot resolve the name 'dsig:Signature' to a(n) 'element declaration' component.

    我还下载了该 xsd 文件并将其放入我的 xsd 所在的同一目录中。如果我在我的开发环境中单击“dsig:Signature”元素,文件将被打开并且 Signature 元素可见。所以我认为该文件可以访问。

    【讨论】:

      【解决方案2】:

      由于 Signature 元素位于其自己的命名空间 http://www.w3.org/2000/09/xmldsig# 中,因此需要在以它为目标命名空间的架构文档中声明它。

      这样的架构文档似乎已经存在:请参阅https://www.w3.org/TR/xmldsig-core1/#sec-xsdSchema,因此您只需使用xs:import 将其导入,而不是尝试自己重新定义。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-11-04
        • 1970-01-01
        • 2019-11-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多