2012-05-28 94 views
-1

我有我的XML文件作爲XML Schema驗證錯誤

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

這個xml文件我的架構文件如下(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(編輯)

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

當我驗證我的xml f ile我得到以下異常

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. 

我不知道我哪裏錯了?任何人都可以告訴我代碼中的錯誤究竟在哪裏?
錯誤編輯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. 

回答

2

在c2.xsd,行後22

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

xs:element不能顯示爲xs:complexType直接孩子。它必須位於xs:all,xs:choicexs:sequence之內。

+0

現在檢查我的c2.xsd。我試過,但仍然顯示錯誤。我在我的問題中貼上了新的錯誤 – suraj

1

我嘗試了你的文件,並設法得到它來驗證記事本++做了幾件事情:

  1. 改變了XSD用來導入簽名XSD的引用,沒有任何C:\參考。要做到這一點,我只是複製了導入的文件相同的位置,因爲我是做從
  2. 進口改爲在您指定DS到不同的屬性c2.xsd的XSD即DSIG
  3. 下載的W3C的版本簽名xsd從w3 Signature xsd並用於導入(步驟1)。
  4. 在指定簽名時,將輸出xml更改爲包含xmlns =「http://www.w3.org/2000/09/xmldsig#」行。

請注意xsd和xml,我在下面的記事本++中成功進行了驗證。

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> 

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

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

屬性必須組之後進行定義,所有,選擇或順序元素,所以在<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>