如何将签名元素定义添加到XSD

时间:2017-10-10 10:18:10

标签: xml xsd

我们的一个XML将来会有一个签名块。

我不知道在哪里将它的定义放入XSD文件中。

我尝试将以下内容放在最后(在关闭<xs:schem>之前):

<element name="Signature" type="ds:SignatureType"/>
<complexType name="SignatureType">
    <sequence> 
        <element ref="ds:SignedInfo"/> 
        <element ref="ds:SignatureValue"/> 
    </sequence>  
    <attribute name="Id" type="ID" use="optional"/>
</complexType>

<element name="SignatureValue" type="ds:SignatureValueType"/> 
<complexType name="SignatureValueType">
    <simpleContent>
        <extension base="base64Binary">
            <attribute name="Id" type="ID" use="optional"/>
        </extension>
    </simpleContent>
</complexType>

<element name="SignedInfo" type="ds:SignedInfoType"/> 
<complexType name="SignedInfoType">
    <sequence> 
        <element ref="ds:CanonicalizationMethod"/>
        <element ref="ds:SignatureMethod"/> 
        <element ref="ds:Reference" maxOccurs="unbounded"/> 
    </sequence>  
    <attribute name="Id" type="ID" use="optional"/> 
</complexType>

<element name="CanonicalizationMethod" type="ds:CanonicalizationMethodType"/> 
<complexType name="CanonicalizationMethodType" mixed="true">
    <sequence>
        <any namespace="##any" minOccurs="0" maxOccurs="unbounded"/>
        <!-- (0,unbounded) elements from (1,1) namespace -->
    </sequence>
    <attribute name="Algorithm" type="anyURI" use="required"/> 
</complexType>

<element name="SignatureMethod" type="ds:SignatureMethodType"/>
<complexType name="SignatureMethodType" mixed="true">
    <sequence>
        <element name="HMACOutputLength" minOccurs="0" 
            type="ds:HMACOutputLengthType"/>
            <any namespace="##other" minOccurs="0" maxOccurs="unbounded"/>
        <!-- (0,unbounded) elements from (1,1) external namespace -->
        </sequence>
    <attribute name="Algorithm" type="anyURI" use="required"/> 
</complexType>

<element name="Reference" type="ds:ReferenceType"/>
<complexType name="ReferenceType">
    <sequence> 
        <element ref="ds:Transforms" minOccurs="0"/> 
        <element ref="ds:DigestMethod"/> 
        <element ref="ds:DigestValue"/> 
    </sequence>
    <attribute name="Id" type="ID" use="optional"/> 
    <attribute name="URI" type="anyURI" use="optional"/> 
    <attribute name="Type" type="anyURI" use="optional"/> 
</complexType>

<element name="Transforms" type="ds:TransformsType"/>
<complexType name="TransformsType">
    <sequence>
        <element ref="ds:Transform" maxOccurs="unbounded"/>  
    </sequence>
</complexType>

<element name="Transform" type="ds:TransformType"/>
<complexType name="TransformType" mixed="true">
    <choice minOccurs="0" maxOccurs="unbounded"> 
        <any namespace="##other" processContents="lax"/>
        <!-- (1,1) elements from (0,unbounded) namespaces -->
        <element name="XPath" type="string"/> 
    </choice>
    <attribute name="Algorithm" type="anyURI" use="required"/> 
</complexType>

<element name="DigestMethod" type="ds:DigestMethodType"/>
<complexType name="DigestMethodType" mixed="true"> 
    <sequence>
        <any namespace="##other" processContents="lax" 
             minOccurs="0" maxOccurs="unbounded"/>
    </sequence>    
    <attribute name="Algorithm" type="anyURI" use="required"/> 
</complexType>

<element name="DigestValue" type="ds:DigestValueType"/>
    <simpleType name="DigestValueType">
    <restriction base="base64Binary"/>
</simpleType>
架构标签中的

我有以下内容:

<xs:schema xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
           xmlns:xs="http://www.w3.org/2001/XMLSchema" 
           xmlns:ds="http://www.w3.org/2000/09/xmldsig#">

W3C检查器告诉我命名空间存在一些问题。但由于我对XSD缺乏了解,我在这里迷失了

XML中的Signature部分如下所示(在关闭根节点之前)

<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>[the digest value]</DigestValue>
  </Reference>
</SignedInfo>
<SignatureValue>[the generated signature]</SignatureValue>
</Signature>

我需要对XSD进行哪些更改才能使带有签名块的XML对XSD验证为真?

1 个答案:

答案 0 :(得分:0)

我创建了一个有效的XSD,将&#34; xs&#34; -namespace添加到签名定义中的所有元素和类型,并从所有元素和类型中删除&#34; ds&#34; -namespace。

我复制&amp;粘贴了https://www.w3.org/TR/xmldsig-core/xmldsig-core-schema.xsd

的整个定义