gpt4 book ai didi

xml - 元素中的命名空间发现 XSD 无效内容

转载 作者:行者123 更新时间:2023-12-04 16:55:39 27 4
gpt4 key购买 nike

我想根据 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>```

最佳答案

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

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

我将命名空间添加到我的 Signature 元素中:
      <xs:element ref="dsig:Signature"/>

但是现在找不到元素: <"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”元素,文件将被打开并且签名元素是可见的。所以我认为可以访问该文件。

关于xml - 元素中的命名空间发现 XSD 无效内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58334368/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com