简体   繁体   English

XML模式验证错误

[英]XML Schema validation error

I have my xml file as 我有我的xml文件

 <?xml version="1.0" encoding="ISO-8859-1"?>
 <Info>
 <Pan>123</Pan>
 <Name>qwe</Name>
 <Email>qwe</Email>
 <City>qwe</City>
 <State>qwe</State>
 <AssessmentYear>2012</AssessmentYear>
 <MobileNo>1234</MobileNo>
 <Income-Salary>1234</Income-Salary>
 <Income-Other>1234</Income-Other>
 <TotalAmount>122</TotalAmount>
<Signature> 
<SignedInfo>
 <CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
 <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"/><Transform Algorithm="http://www.w3.org/TR/1999/REC-xslt-19991116"><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text"/>
<xsl:template match="/">
Pan : <xsl:copy-of select="//Pan"/>

MobileNo : <xsl:copy-of select="//MobileNo"/>

TotalAmount : <xsl:copy-of select="//TotalAmount"/>
</xsl:template>
</xsl:stylesheet></Transform></Transforms><DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/><DigestValue>FgbIIimTLIbd0Zpvq1zDrZse6aJg5bAE1/Q58cEkEvk=</DigestValue></Reference>
 </SignedInfo>
<SignatureValue>dy4QDco5NhXResncu0tUG5ylujDn9siIQSHjuX5HxH2gs70LpsO3KDWNvDXjpgkIySYfzJ/FdC6C
trkSySWRjhObqI8cbcP5VU/nL8pP21+3CO+gF1k884aeX3felpRy0FBBMTYBknQTunWCHvpHk927
ZHGvm6Hiej7iBKr3e1k=</SignatureValue>
</Signature>
</Info>

My Schema files for this xml file is as follows( c1.xsd ) 此xml文件的我的Schema文件如下( c1.xsd

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
<xs:import namespace="http://www.w3.org/2000/09/xmldsig#" schemaLocation="C:\\xml\\c2.xsd"/>
<xs:element name="Info">
  <xs:complexType>

  <xs:sequence>
      <xs:element name="Pan" type="xs:string"/>
      <xs:element name="Name" type="xs:string"/>
      <xs:element name="Email" type="xs:string"/>
      <xs:element name="City" type="xs:string"/>
      <xs:element name="State" type="xs:string"/>
      <xs:element name="AssessmentYear" type="xs:gYear"/>
      <xs:element name="MobileNo" type="xs:unsignedLong"/>
      <xs:element name="Income-Salary" type="xs:unsignedLong"/>
      <xs:element name="Income-Other" type="xs:unsignedLong"/>
      <xs:element name="TotalAmount" type="xs:unsignedLong"/>
      <xs:element ref="ds:Signature"/>
 </xs:sequence>
  </xs:complexType>
</xs:element>
</xs:schema>

c2.xsd(Edited) c2.xsd(编辑)

 <?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.w3.org/2000/09/xmldsig#">
<xs:element name="Signature">
     <xs:complexType>
      <xs:sequence>
        <xs:element name="SignedInfo">
         <xs:complexType>
          <xs:sequence>
             <xs:element name="CanonicalizationMethod">
              <xs:complexType>
               <xs:attribute name="Algorithm" type="xs:string" use="required"/>
              </xs:complexType>
             </xs:element>
             <xs:element name="SignatureMethod">
               <xs:complexType>
                 <xs:attribute name="Algorithm" type="xs:string" use="required"/>
               </xs:complexType>
             </xs:element>                
            <xs:element name="Reference">
             <xs:complexType>
               <xs:attribute name="URI" type="xs:string"/>
               <xs:all>
               <xs:element name="Transforms">
                 <xs:complexType>
                   <xs:sequence>
                     <xs:element name="Transform" type="xs:string">
                       <xs:complexType>
                         <xs:attribute name="Algorithm" type="xs:string" use="required"/>
                       </xs:complexType>
                      </xs:element>
                   </xs:sequence>
                 </xs:complexType>
               </xs:element>
               <xs:element name="DigestMethod">
                 <xs:complexType>
                   <xs:attribute name="Algorithm" type="xs:string" use="required"/>
                 </xs:complexType>
               </xs:element>
               <xs:element name="DigestValue" type="xs:hexBinary"/>
               </xs:all>
             </xs:complexType>
            </xs:element>
            </xs:sequence>
         </xs:complexType> 
        </xs:element> 
       <xs:element name="SignatureValue" type="xs:string"/>
      </xs:sequence>
     </xs:complexType>
</xs:element>
</xs:schema>

When I validate my xml file i get the following Exception 当我验证xml文件时,出现以下异常

org.xml.sax.SAXParseException; systemId: file:/C://xml//c2.xsd; lineNumber: 22;
columnNumber: 37; s4s-elt-invalid-content.1: The content of '#AnonType_Reference
SignedInfoSignature' is invalid.  Element 'element' is invalid, misplaced, or oc
curs too often.

I cannot figure out where am I going wrong? 我不知道我要去哪里错了? Can anyone tell where exactly is the mistake in my code? 谁能告诉我代码中的错误在哪里?
Error after Edit 1 编辑1后出现错误

org.xml.sax.SAXParseException; systemId: file:/C://xml//c2.xsd; lineNumber: 22;
columnNumber: 15; s4s-elt-invalid-content.1: The content of '#AnonType_Reference
SignedInfoSignature' is invalid.  Element 'all' is invalid, misplaced, or occurs
 too often.

At c2.xsd, line 22 在c2.xsd,第22行

         <xs:complexType>
           <xs:attribute name="URI" type="xs:string"/>
           <xs:element name="Transforms">

xs:element cannot appear as a direct child of xs:complexType . xs:element不能作为xs:complexType的直接子代出现。 It must be inside xs:all , xs:choice or xs:sequence . 它必须在xs:allxs:choicexs:sequence

I tried out your file and managed to get it to validate against notepad++ doing a couple of things: 我尝试了您的文件,并设法使它针对notepad ++进行了验证:

  1. Changed the xsd to use a reference to the imported signature xsd without any c:\\reference. 将xsd更改为使用对导入的签名xsd的引用,而没有任何c:\\ reference。 To do this I just copied that imported file to the same location as the xsd I was doing the importing from 为此,我只是将导入的文件复制到与从中进行导入的xsd相同的位置
  2. Changed the c2.xsd where you specify the ds to a different attribute namely dsig 将您在其中指定ds的c2.xsd更改为另一个属性dsig
  3. Downloaded the w3c version of the signature xsd from w3 Signature xsd and used that in the import (Step 1). w3 Signature xsd下载了签名xsd的w3c版本,并将其用于导入(步骤1)。
  4. Changed your output xml to include the xmlns="http://www.w3.org/2000/09/xmldsig#" line when specifying the signature. 指定签名时,将输出xml更改为包括xmlns =“ http://www.w3.org/2000/09/xmldsig#”行。

Note the xsd and xml I succeeded in validating in notepad++ below. 请注意我在下面的notepad ++中成功验证的xsd和xml。

XSD: XSD:

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:dsig="http://www.w3.org/2000/09/xmldsig#" attributeFormDefault="unqualified">
<xs:import namespace="http://www.w3.org/2000/09/xmldsig#" schemaLocation="xmldsig-core-schema.xsd"/>
<xs:element name="Info">
  <xs:complexType>
<xs:sequence>
      <xs:element name="Pan" type="xs:string"/>
      <xs:element name="Name" type="xs:string"/>
      <xs:element name="Email" type="xs:string"/>
      <xs:element name="City" type="xs:string"/>
      <xs:element name="State" type="xs:string"/>
      <xs:element name="AssessmentYear" type="xs:gYear"/>
      <xs:element name="MobileNo" type="xs:unsignedLong"/>
      <xs:element name="Income-Salary" type="xs:unsignedLong"/>
      <xs:element name="Income-Other" type="xs:unsignedLong"/>
      <xs:element name="TotalAmount" type="xs:unsignedLong"/>
      <xs:element ref="dsig:Signature" minOccurs="0" maxOccurs="1" />
</xs:sequence>
  </xs:complexType>
</xs:element>
</xs:schema>

And the XML: 和XML:

<?xml version="1.0" ?>
 <Info xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" >
 <Pan>123</Pan>
 <Name>qwe</Name>
 <Email>qwe</Email>
 <City>qwe</City>
 <State>qwe</State>
 <AssessmentYear>2012</AssessmentYear>
 <MobileNo>1234</MobileNo>
 <Income-Salary>1234</Income-Salary>
 <Income-Other>1234</Income-Other>
 <TotalAmount>122</TotalAmount>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#"> 
<SignedInfo>
 <CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
 <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"/>
 <Transform Algorithm="http://www.w3.org/TR/1999/REC-xslt-19991116">
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
        <xsl:output method="text"/>
        <xsl:template match="/">
Pan : <xsl:copy-of select="//Pan"/>

MobileNo : <xsl:copy-of select="//MobileNo"/>

TotalAmount : <xsl:copy-of select="//TotalAmount"/>
        </xsl:template>
    </xsl:stylesheet>
</Transform>
</Transforms>
<DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
<DigestValue>FgbIIimTLIbd0Zpvq1zDrZse6aJg5bAE1/Q58cEkEvk=</DigestValue>
</Reference>
 </SignedInfo>
<SignatureValue>dy4QDco5NhXResncu0tUG5ylujDn9siIQSHjuX5HxH2gs70LpsO3KDWNvDXjpgkIySYfzJ/FdC6C
trkSySWRjhObqI8cbcP5VU/nL8pP21+3CO+gF1k884aeX3felpRy0FBBMTYBknQTunWCHvpHk927
ZHGvm6Hiej7iBKr3e1k=</SignatureValue>
</Signature>
</Info>

Your c2.xsd remains invalid, look on ComplexType definition: 您的c2.xsd仍然无效,请查看ComplexType定义:

<complexType
  id=ID
  name=NCName
  abstract=true|false
  mixed=true|false
  block=(#all|list of (extension|restriction))
  final=(#all|list of (extension|restriction))
  any attributes
 >

 (annotation?,(simpleContent|complexContent|((group|all|
 choice|sequence)?,((attribute|attributeGroup)*,anyAttribute?))))

</complexType>

attribute must be defined after group, all, choice or sequence element, so change order in <xs:element name="Reference"> in order to define first <xs:all> and then <xs:attribute> : 属性必须在组,全部,选择或序列元素之后定义,因此在<xs:element name="Reference">中更改顺序,以便首先定义<xs:all>然后定义<xs:attribute>

      <xs:element name="Reference">
         <xs:complexType>
           <xs:all>
           <xs:element name="Transforms">
             <xs:complexType>
               <xs:sequence>
                 <xs:element name="Transform" type="xs:string">
                   <xs:complexType>
                     <xs:attribute name="Algorithm" type="xs:string" use="required"/>
                   </xs:complexType>
                  </xs:element>
               </xs:sequence>
             </xs:complexType>
           </xs:element>
           <xs:element name="DigestMethod">
             <xs:complexType>
               <xs:attribute name="Algorithm" type="xs:string" use="required"/>
             </xs:complexType>
           </xs:element>
           <xs:element name="DigestValue" type="xs:hexBinary"/>
           </xs:all>
           <xs:attribute name="URI" type="xs:string"/>
         </xs:complexType>
        </xs:element>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM