Java 用xsl进行Xml验证

Java 用xsl进行Xml验证,java,xml,validation,xslt,schematron,Java,Xml,Validation,Xslt,Schematron,几个小时以来,我一直在尝试通过xslt验证xml。我有以下用于xml验证的xsl表单。每次尝试验证xml时,我都会收到下面的警告,xml中的空currencyid属性将被忽略,xml验证(尽管它不是)。有人知道为什么它会被忽略和验证吗 <xsl:variable name="CurrencyCodeList" select="',AED,AFN,ALL,AMD,ANG,AOA,ARS,AUD,AWG,AZN,BAM,BBD,BDT,BGN,BHD,BIF,BM

几个小时以来,我一直在尝试通过xslt验证xml。我有以下用于xml验证的xsl表单。每次尝试验证xml时,我都会收到下面的警告,xml中的空currencyid属性将被忽略,xml验证(尽管它不是)。有人知道为什么它会被忽略和验证吗

  <xsl:variable name="CurrencyCodeList"
             select="',AED,AFN,ALL,AMD,ANG,AOA,ARS,AUD,AWG,AZN,BAM,BBD,BDT,BGN,BHD,BIF,BMD,BND,BOB,BOV,BRL,BSD,BTN,BWP,BYR,BZD,CAD,CDF,CHE,CHF,CHW,CLF,CLP,CNY,COP,COU,CRC,CUC,CUP,CVE,CZK,DJF,DKK,DOP,DZD,EEK,EGP,ERN,ETB,EUR,FJD,FKP,GBP,GEL,GHS,GIP,GMD,GNF,GTQ,GWP,GYD,HKD,HNL,HRK,HTG,HUF,IDR,ILS,INR,IQD,IRR,ISK,JMD,JOD,JPY,KES,KGS,KHR,KMF,KPW,KRW,KWD,KYD,KZT,LAK,LBP,LKR,LRD,LSL,LTL,LVL,LYD,MAD,MAD,MDL,MGA,MKD,MMK,MNT,MOP,MRO,MUR,MVR,MWK,MXN,MXV,MYR,MZN,NAD,NGN,NIO,NOK,NPR,NZD,OMR,PAB,PEN,PGK,PHP,PKR,PLN,PYG,QAR,RON,RSD,RUB,RWF,SAR,SBD,SCR,SDG,SEK,SGD,SHP,SLL,SOS,SRD,STD,SVC,SYP,SZL,THB,TJS,TMT,TND,TOP,TRY,TTD,TWD,TZS,UAH,UGX,USD,USN,USS,UYI,UYU,UZS,VEF,VND,VUV,WST,XAF,XAG,XAU,XBA,XBB,XBC,XBD,XCD,XDR,XFU,XOF,XPD,XPF,XPF,XPF,XPT,XTS,XXX,YER,ZAR,ZMK,ZWL,'"/>


<xsl:template match="//@currencyID" priority="1008" mode="M0">
      <svrl:fired-rule xmlns:svrl="http://purl.oclc.org/dsdl/svrl" context="//@currencyID"/>

        <!--ASSERT -->
  <xsl:choose>
     <xsl:when test="contains($CurrencyCodeList, concat(',',.,','))"/>
     <xsl:otherwise>
        <svrl:failed-assert xmlns:svrl="http://purl.oclc.org/dsdl/svrl"
                            test="contains($CurrencyCodeList, concat(',',.,','))">
           <xsl:attribute name="location">
              <xsl:apply-templates select="." mode="schematron-select-full-path"/>
           </xsl:attribute>
           <svrl:text>Geçersiz currencyID niteliği : '<xsl:text/>
              <xsl:value-of select="."/>
              <xsl:text/>'. Geçerli değerler için kod listesine bakınız.</svrl:text>
        </svrl:failed-assert>
     </xsl:otherwise>
  </xsl:choose>
  <xsl:apply-templates select="*|comment()|processing-instruction()" mode="M0"/>
编辑:

实际上,我将schematron转换为xslt,以便在xslt中测试它。给出了用于验证的schematron。所以实际上我必须通过给定的schematron文件、样本xml和java代码进行验证。主schematron和其他文件有更多的规则和模式。但是为了便于测试,我删除了它们中的大部分。除了元素中的属性(例如currencyId属性),所有内容都已成功验证。im使用UgliSch(Ugli Schematron Validator)进行Schematron验证

MainSchematron.xml:

<?xml version="1.0" encoding="UTF-8"?>
<sch:schema xmlns="http://purl.oclc.org/dsdl/schematron"
            xmlns:sch="http://purl.oclc.org/dsdl/schematron"
            xmlns:sh="http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader"
            xmlns:ef="http://www.efatura.gov.tr/envelope-namespace">

    <sch:include href="UBL-TR_Codelist.xml#codes"/>
    <sch:include href="UBL-TR_Common_Schematron.xml#abstracts"/>

    <sch:ns prefix="cbc" uri="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" />

    <sch:rule context="//cbc:CurrencyCode">
        <sch:extends rule="GeneralCurrencyCodeCheck"/>
    </sch:rule>
    <sch:rule context="//@currencyID">
        <sch:extends rule="GeneralCurrencyIDCheck"/>
    </sch:rule>


</sch:schema>
<sch:schema xmlns:sch="http://purl.oclc.org/dsdl/schematron"
            xmlns="http://purl.oclc.org/dsdl/schematron">

    <sch:pattern name="AbstractRules" id="abstracts">
        <sch:p>Pattern for storing abstract rules</sch:p>

        <!-- Rule to validate currencyID Genel -->
        <sch:rule abstract="true" id="GeneralCurrencyIDCheck">
            <sch:assert test="contains($CurrencyCodeList, concat(',',.,','))">Geçersiz currencyID niteliği : '<sch:value-of select="."/>'. Geçerli değerler için kod listesine bakınız.</sch:assert>
        </sch:rule>


    </sch:pattern>
</sch:schema>

,你能不能提供样本数据,以便有人能重现这个问题?@Rao i编辑并改进了上面的问题。我将感谢任何帮助!这些警告是因为schematron转换成的xsl有一个
格式的语句,即使在使用属性的模板下也会显示该语句。这只是一个警告,说明该语句没有任何用途,也不需要担心(如果您有任何处理属性的规则,您就会得到它)。我正在尝试测试您的模式,但您没有包含抽象规则GeneralCurrencyCodeCheck,因此无法正确构建。
<sch:schema xmlns:sch="http://purl.oclc.org/dsdl/schematron"
            xmlns="http://purl.oclc.org/dsdl/schematron">

    <sch:pattern name="AbstractRules" id="abstracts">
        <sch:p>Pattern for storing abstract rules</sch:p>

        <!-- Rule to validate currencyID Genel -->
        <sch:rule abstract="true" id="GeneralCurrencyIDCheck">
            <sch:assert test="contains($CurrencyCodeList, concat(',',.,','))">Geçersiz currencyID niteliği : '<sch:value-of select="."/>'. Geçerli değerler için kod listesine bakınız.</sch:assert>
        </sch:rule>


    </sch:pattern>
</sch:schema>
<sch:schema xmlns:sch="http://purl.oclc.org/dsdl/schematron"
            xmlns="http://purl.oclc.org/dsdl/schematron">

    <sch:pattern name="CodeList" id="codes">

        <sch:let name="CurrencyCodeList" value="',AED,AFN,ALL,AMD,ANG,AOA,ARS,AUD,AWG,AZN,BAM,BBD,BDT,BGN,BHD,BIF,BMD,BND,BOB,BOV,BRL,BSD,BTN,BWP,BYR,BZD,CAD,CDF,CHE,CHF,CHW,CLF,CLP,CNY,COP,COU,CRC,CUC,CUP,CVE,CZK,DJF,DKK,DOP,DZD,EEK,EGP,ERN,ETB,EUR,FJD,FKP,GBP,GEL,GHS,GIP,GMD,GNF,GTQ,GWP,GYD,HKD,HNL,HRK,HTG,HUF,IDR,ILS,INR,IQD,IRR,ISK,JMD,JOD,JPY,KES,KGS,KHR,KMF,KPW,KRW,KWD,KYD,KZT,LAK,LBP,LKR,LRD,LSL,LTL,LVL,LYD,MAD,MAD,MDL,MGA,MKD,MMK,MNT,MOP,MRO,MUR,MVR,MWK,MXN,MXV,MYR,MZN,NAD,NGN,NIO,NOK,NPR,NZD,OMR,PAB,PEN,PGK,PHP,PKR,PLN,PYG,QAR,RON,RSD,RUB,RWF,SAR,SBD,SCR,SDG,SEK,SGD,SHP,SLL,SOS,SRD,STD,SVC,SYP,SZL,THB,TJS,TMT,TND,TOP,TRY,TTD,TWD,TZS,UAH,UGX,USD,USN,USS,UYI,UYU,UZS,VEF,VND,VUV,WST,XAF,XAG,XAU,XBA,XBB,XBC,XBD,XCD,XDR,XFU,XOF,XPD,XPF,XPF,XPF,XPT,XTS,XXX,YER,ZAR,ZMK,ZWL,'"/>

        </sch:pattern>

</sch:schema>
<sh:StandardBusinessDocument xsi:schemaLocation="http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader PackageProxy_1_2.xsd" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" xmlns:ef="http://www.efatura.gov.tr/package-namespace" xmlns:oa="http://www.openapplications.org/oagis/9" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:sh="http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:ns9="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:ns11="urn:oasis:names:specification:ubl:schema:xsd:ApplicationResponse-2" xmlns:ns3="http://www.hr-xml.org/3" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <sh:StandardBusinessDocumentHeader>
        <sh:HeaderVersion>1.0</sh:HeaderVersion>
        <sh:Sender>
            <sh:Identifier>urn:mail:defaultgb@xxx.com.tr</sh:Identifier>
            <sh:ContactInformation>
                <sh:Contact>xxx Kurumsal Bilgi Sistemleri A.Ş</sh:Contact>
                <sh:ContactTypeIdentifier>UNVAN</sh:ContactTypeIdentifier>
            </sh:ContactInformation>
            <sh:ContactInformation>
                <sh:Contact>8110120507</sh:Contact>
                <sh:ContactTypeIdentifier>VKN_TCKN</sh:ContactTypeIdentifier>
            </sh:ContactInformation>
        </sh:Sender>
        <sh:Receiver>
            <sh:Identifier>urn:mail:defaultpk@xxx.com.tr</sh:Identifier>
            <sh:ContactInformation>
                <sh:Contact>KAKAR KURUMSAL BİLGİSİSTEMLERİ LTD.ŞTİ. Test Kullanıcısı</sh:Contact>
                <sh:ContactTypeIdentifier>UNVAN</sh:ContactTypeIdentifier>
            </sh:ContactInformation>
            <sh:ContactInformation>
                <sh:Contact>4545552073</sh:Contact>
                <sh:ContactTypeIdentifier>VKN_TCKN</sh:ContactTypeIdentifier>
            </sh:ContactInformation>
        </sh:Receiver>
        <sh:DocumentIdentification>
            <sh:Standard>UBL-TR</sh:Standard>
            <sh:TypeVersion>1.2</sh:TypeVersion>
            <sh:InstanceIdentifier>bb583542-a81a-4b45-87d6-e90596101a41</sh:InstanceIdentifier>
            <sh:Type>SENDERENVELOPE</sh:Type>
            <sh:MultipleType>false</sh:MultipleType>
            <sh:CreationDateAndTime>2016-01-06T16:27:25.759+02:00</sh:CreationDateAndTime>
        </sh:DocumentIdentification>
    </sh:StandardBusinessDocumentHeader>
    <ef:Package>
        <Elements>
            <ElementType>INVOICE</ElementType>
            <ElementCount>1</ElementCount>
            <ElementList>
                <ns9:Invoice>
                    <ext:UBLExtensions>
                        <ext:UBLExtension>
                            <ext:ExtensionContent>
                            ...
                            </ext:ExtensionContent>
                        </ext:UBLExtension>
                    </ext:UBLExtensions>
                    <cbc:UBLVersionID>2.1</cbc:UBLVersionID>
                    <cbc:CustomizationID>TR1.2</cbc:CustomizationID>
                    <cbc:ProfileID>TICARIFATURA</cbc:ProfileID>
                    <cbc:ID>PAZ2015000000012</cbc:ID>
                    <cbc:CopyIndicator>false</cbc:CopyIndicator>
                    <cbc:UUID>54b0dad2-e3a7-44ee-848a-cf7977000020</cbc:UUID>
                    <cbc:IssueDate>2016-01-06</cbc:IssueDate>
                    <cbc:InvoiceTypeCode>SATIS</cbc:InvoiceTypeCode>
                    <cbc:DocumentCurrencyCode>TRY</cbc:DocumentCurrencyCode>
                    <cbc:LineCountNumeric>0</cbc:LineCountNumeric>
                    <cac:Signature>
                        <cbc:ID schemeID="VKN_TCKN">8110120507</cbc:ID>
                        <cac:SignatoryParty>
                            <cbc:WebsiteURI>http://www.xxx.com.tr/</cbc:WebsiteURI>
                            <cac:PartyIdentification>
                                <cbc:ID schemeID="VKN">8110120507</cbc:ID>
                            </cac:PartyIdentification>
                            <cac:PartyName>
                                <cbc:Name>xxx Kurumsal Bilgi Sistemleri A.Ş</cbc:Name>
                            </cac:PartyName>
                            <cac:PostalAddress>
                                <cbc:StreetName>Besiktas Teknik Universitesi</cbc:StreetName>
                                <cbc:BuildingNumber>150/1G</cbc:BuildingNumber>
                                <cbc:CitySubdivisionName>Besıktas</cbc:CitySubdivisionName>
                                <cbc:CityName>Istanbul</cbc:CityName>
                                <cbc:PostalZone>06100</cbc:PostalZone>
                                <cac:Country>
                                    <cbc:Name>Turkiye</cbc:Name>
                                </cac:Country>
                            </cac:PostalAddress>
                        </cac:SignatoryParty>
                        <cac:DigitalSignatureAttachment>
                            <cac:ExternalReference>
                                <cbc:URI>#Signature</cbc:URI>
                            </cac:ExternalReference>
                        </cac:DigitalSignatureAttachment>
                    </cac:Signature>
                    <cac:AccountingSupplierParty>
                        <cac:Party>
                            <cac:PartyIdentification>
                                <cbc:ID schemeID="VKN">7221130507</cbc:ID>
                            </cac:PartyIdentification>
                            <cac:PartyName>
                                <cbc:Name>KAKAR KURUMSAL  LTD.ŞTİ.</cbc:Name>
                            </cac:PartyName>
                            <cac:PostalAddress>
                                <cbc:Room/>
                                <cbc:BuildingName/>
                                <cbc:BuildingNumber/>
                                <cbc:CitySubdivisionName>besiktas</cbc:CitySubdivisionName>
                                <cbc:CityName>istanbul</cbc:CityName>
                                <cbc:PostalZone/>
                                <cac:Country>
                                    <cbc:Name>ALMANYA</cbc:Name>
                                </cac:Country>
                            </cac:PostalAddress>
                            <cac:Contact>
                                <cbc:Telephone/>
                                <cbc:Telefax/>
                                <cbc:ElectronicMail/>
                            </cac:Contact>
                        </cac:Party>
                    </cac:AccountingSupplierParty>
                    <cac:AccountingCustomerParty>
                        <cac:Party>
                            <cbc:WebsiteURI/>
                            <cac:PartyIdentification>
                                <cbc:ID schemeID="VKN">2535552073</cbc:ID>
                            </cac:PartyIdentification>
                            <cac:PartyName>
                                <cbc:Name>KAKAR LTD.ŞTİ. Test Kullanıcısı</cbc:Name>
                            </cac:PartyName>
                            <cac:PostalAddress>
                                <cbc:ID/>
                                <cbc:Postbox/>
                                <cbc:Room/>
                                <cbc:StreetName/>
                                <cbc:BlockName/>
                                <cbc:BuildingName/>
                                <cbc:BuildingNumber/>
                                <cbc:CitySubdivisionName>besiktas</cbc:CitySubdivisionName>
                                <cbc:CityName>istanbul</cbc:CityName>
                                <cbc:PostalZone/>
                                <cbc:Region/>
                                <cbc:District/>
                                <cac:Country>
                                    <cbc:Name>TÜRKİYE</cbc:Name>
                                </cac:Country>
                            </cac:PostalAddress>
                            <cac:Contact>
                                <cbc:Telephone/>
                                <cbc:Telefax/>
                                <cbc:ElectronicMail/>
                            </cac:Contact>
                            <cac:Person>
                                <cbc:FirstName/>
                                <cbc:FamilyName/>
                            </cac:Person>
                        </cac:Party>
                    </cac:AccountingCustomerParty>
                    <cac:TaxTotal>
                        <cbc:TaxAmount currencyID="TRY">2.16</cbc:TaxAmount>
                        <cac:TaxSubtotal>
                            <cbc:TaxableAmount currencyID="asdasdasdasd">0</cbc:TaxableAmount>
                            <cbc:TaxAmount currencyID="TRY">2.16</cbc:TaxAmount>
                            <cbc:CalculationSequenceNumeric>0</cbc:CalculationSequenceNumeric>
                            <cbc:Percent>18</cbc:Percent>
                            <cac:TaxCategory>
                                <cac:TaxScheme>
                                    <cbc:Name>KDV</cbc:Name>
                                    <cbc:TaxTypeCode>0015</cbc:TaxTypeCode>
                                </cac:TaxScheme>
                            </cac:TaxCategory>
                        </cac:TaxSubtotal>
                    </cac:TaxTotal>
                    <cac:LegalMonetaryTotal>
                        <cbc:LineExtensionAmount currencyID="TRY">12</cbc:LineExtensionAmount>
                        <cbc:TaxExclusiveAmount currencyID="TRY">12</cbc:TaxExclusiveAmount>
                        <cbc:TaxInclusiveAmount currencyID="TRY">14.16</cbc:TaxInclusiveAmount>
                        <cbc:AllowanceTotalAmount currencyID="TRY">0</cbc:AllowanceTotalAmount>
                        <cbc:PayableAmount currencyID="TRY">14.16</cbc:PayableAmount>
                    </cac:LegalMonetaryTotal>
                    <cac:InvoiceLine>
                        <cbc:ID>1</cbc:ID>
                        <cbc:InvoicedQuantity unitCode="NIU">1</cbc:InvoicedQuantity>
                        <cbc:LineExtensionAmount currencyID="">12</cbc:LineExtensionAmount>
                        <cac:AllowanceCharge>
                            <cbc:ChargeIndicator>false</cbc:ChargeIndicator>
                            <cbc:MultiplierFactorNumeric>0</cbc:MultiplierFactorNumeric>
                            <cbc:Amount currencyID="">0</cbc:Amount>
                            <cbc:BaseAmount currencyID="">0</cbc:BaseAmount>
                        </cac:AllowanceCharge>
                        <cac:TaxTotal>
                            <cbc:TaxAmount currencyID="">2.16</cbc:TaxAmount>
                            <cac:TaxSubtotal>
                                <cbc:TaxableAmount currencyID="">0</cbc:TaxableAmount>
                                <cbc:TaxAmount currencyID="">2.16</cbc:TaxAmount>
                                <cbc:Percent>18</cbc:Percent>
                                <cac:TaxCategory>
                                    <cac:TaxScheme>
                                        <cbc:Name>KDV</cbc:Name>
                                        <cbc:TaxTypeCode>0015</cbc:TaxTypeCode>
                                    </cac:TaxScheme>
                                </cac:TaxCategory>
                            </cac:TaxSubtotal>
                        </cac:TaxTotal>
                        <cac:Item>
                            <cbc:Name>asdasd</cbc:Name>
                        </cac:Item>
                        <cac:Price>
                            <cbc:PriceAmount currencyID="TRY">12</cbc:PriceAmount>
                        </cac:Price>
                    </cac:InvoiceLine>
                </ns9:Invoice>
            </ElementList>
        </Elements>
    </ef:Package>
</sh:StandardBusinessDocument>
  try (InputStream ubl = getClass().getResourceAsStream("/schematrons/UBL-TR_Main_Schematron.xml");) {

            SchemaFactory schemaFactory = SchemaFactory.newInstance(XmlSchemaNsUris.SCHEMATRON_NS_URI);
            Schema schema = schemaFactory.newSchema(new StreamSource(ubl));

            Validator validator = schema.newValidator();

            validator.setErrorHandler(validationErrorHandler);

            validator.validate(new StringSource(new String(binary,"UTF-8")));
        } catch (Exception e) {
            e.printStackTrace();
        }