2012-05-09 62 views
1

我確認一個XML文檔來驗證這樣的總和(很抱歉的標準的例子):XSLT驗證錯誤

<book> 
    <name>Book 1</name> 
    <price>25.30</price> 
</book> 
<book> 
    <name>Book 2</name> 
    <price>20.77</price> 
</book> 
<sum>46.07</sum> 

予加載XML和驗證模式,併產生一個xml作爲輸出。該輸出包含信息有關哪些錯誤發生:

var xml = new XmlDocument(); 
xml.Load(@"c:\test.xml") 

var myXslTrans = new XslCompiledTransform(); 
myXslTrans.Load(@"c:\ValidationSchema.xml";); 
var xmlDocOut = new XmlDocument(); 

    using (XmlWriter xmlWriter = xmlDocOut.CreateNavigator().AppendChild()) 
    { 
     myXslTrans.Transform(xml, null, xmlWriter); 
    } 

出於某種原因,當我有十進制值我的驗證總是失敗。

我試圖與

CultureInfo.InvariantCulture 

我也改變了整個線程與這種文化運行加載這兩個文件。這可以與編碼有關嗎?


編輯:

爲了進一步闡明這一點:

,如果我的代碼運行初期,但一些大型服務器作業完成後,似乎有些全局設置的驗證成功某種改變了一些文化,編碼或某種東西,但我無法找出問題出在哪裏或爲什麼。

我驗證對這一模式的電子發票: http://vefa.difi.no/formatvalidering/resources/validation-xslt/NORWAY-UBL-T10.xsl

這裏是我的測試發票:

<?xml version="1.0" encoding="utf-8"?> 
<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:ccts="urn:un:unece:uncefact:documentation:2" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" xmlns:qdt="urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2" xmlns:udt="urn:un:unece:uncefact:data:specification:UnqualifiedDataTypesSchemaModule:2" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2 UBL-Invoice-2.0.xsd"> 
    <cbc:UBLVersionID>2.0</cbc:UBLVersionID> 
    <cbc:CustomizationID>urn:www.cenbii.eu:transaction:biicoretrdm010:ver1.0</cbc:CustomizationID> 
    <cbc:ProfileID>urn:www.cenbii.eu:profile:bii05:ver1.0</cbc:ProfileID> 
    <cbc:ID>30300109</cbc:ID> 
    <cbc:IssueDate>2012-05-09</cbc:IssueDate> 
    <cbc:InvoiceTypeCode listID="UN/ECE 1001 Subset" listAgencyID="6">380</cbc:InvoiceTypeCode> 
    <cbc:Note languageID="no">Invoice</cbc:Note> 
    <cbc:DocumentCurrencyCode listID="ISO 4217" listAgencyID="5">NOK</cbc:DocumentCurrencyCode> 
    <cbc:AccountingCost> 
    </cbc:AccountingCost> 
    <cac:OrderReference> 
    <cbc:ID>1234</cbc:ID> 
    </cac:OrderReference> 
    <cac:ContractDocumentReference> 
    <cbc:ID> 
    </cbc:ID> 
    </cac:ContractDocumentReference> 
    <cac:AdditionalDocumentReference> 
    <cbc:ID>1 - 30300109 Faktura</cbc:ID> 
    <cbc:DocumentType>Commercial invoice</cbc:DocumentType> 
    <cac:Attachment> 
     <cbc:EmbeddedDocumentBinaryObject mimeCode="application/pdf">123123123123</cbc:EmbeddedDocumentBinaryObject> 
    </cac:Attachment> 
    </cac:AdditionalDocumentReference> 
    <cac:AccountingSupplierParty> 
    <cac:Party> 
     <cac:PartyName> 
     <cbc:Name>Demo</cbc:Name> 
     </cac:PartyName> 
     <cac:PostalAddress> 
     <cbc:StreetName>Postbox 3</cbc:StreetName> 
     <cbc:CityName>OSLO</cbc:CityName> 
     <cbc:PostalZone>0631</cbc:PostalZone> 
     <cac:Country> 
      <cbc:IdentificationCode listID="ISO 3166" listAgencyID="5">NO</cbc:IdentificationCode> 
     </cac:Country> 
     </cac:PostalAddress> 
     <cac:PartyTaxScheme> 
     <cbc:CompanyID>NO910667831MVA</cbc:CompanyID> 
     <cac:TaxScheme> 
      <cbc:ID>VAT</cbc:ID> 
     </cac:TaxScheme> 
     </cac:PartyTaxScheme> 
     <cac:PartyLegalEntity> 
     <cbc:CompanyID schemeID="CVR" schemeAgencyID="ZZZ">910667831</cbc:CompanyID> 
     </cac:PartyLegalEntity> 
     <cac:Contact> 
     <cbc:ID>Per</cbc:ID> 
     </cac:Contact> 
    </cac:Party> 
    </cac:AccountingSupplierParty> 
    <cac:AccountingCustomerParty> 
    <cac:Party> 
     <cac:PartyIdentification> 
     <cbc:ID>2072</cbc:ID> 
     </cac:PartyIdentification> 
     <cac:PartyName> 
     <cbc:Name>Wenzel</cbc:Name> 
     </cac:PartyName> 
     <cac:PostalAddress> 
     <cbc:StreetName>Road 4</cbc:StreetName> 
     <cbc:CityName>OSLO</cbc:CityName> 
     <cbc:PostalZone>0404</cbc:PostalZone> 
     <cac:Country> 
      <cbc:IdentificationCode listID="ISO 3166" listAgencyID="5">NO</cbc:IdentificationCode> 
     </cac:Country> 
     </cac:PostalAddress> 
     <cac:PartyLegalEntity> 
     <cbc:CompanyID schemeAgencyID="ZZZ" schemeID="ZZZ">986532951</cbc:CompanyID> 
     </cac:PartyLegalEntity> 
     <cac:Contact> 
     <cbc:ID>ww</cbc:ID> 
     </cac:Contact> 
    </cac:Party> 
    </cac:AccountingCustomerParty> 
    <cac:Delivery> 
    <cbc:ActualDeliveryDate>2012-05-09</cbc:ActualDeliveryDate> 
    <cac:DeliveryLocation> 
     <cac:Address> 
     <cbc:StreetName>Road 123</cbc:StreetName> 
     <cbc:CityName>OSLO</cbc:CityName> 
     <cbc:PostalZone>0404</cbc:PostalZone> 
     <cac:Country> 
      <cbc:IdentificationCode>NO</cbc:IdentificationCode> 
     </cac:Country> 
     </cac:Address> 
    </cac:DeliveryLocation> 
    </cac:Delivery> 
    <cac:PaymentMeans> 
    <cbc:PaymentMeansCode listID="UN/ECE 4461">31</cbc:PaymentMeansCode> 
    <cbc:PaymentDueDate>2012-06-08</cbc:PaymentDueDate> 
    <cbc:PaymentID>1020720303001099</cbc:PaymentID> 
    <cac:PayeeFinancialAccount> 
     <cbc:ID schemeID="BBAN">50841051308</cbc:ID> 
    </cac:PayeeFinancialAccount> 
    </cac:PaymentMeans> 
    <cac:AllowanceCharge> 
    <cbc:ChargeIndicator>false</cbc:ChargeIndicator> 
    <cbc:AllowanceChargeReason>rebate</cbc:AllowanceChargeReason> 
    <cbc:Amount currencyID="NOK">4664.21</cbc:Amount> 
    <cac:TaxCategory> 
     <cbc:ID schemeID="UN/ECE 5305" schemeAgencyID="6">S</cbc:ID> 
     <cbc:Percent>25</cbc:Percent> 
     <cac:TaxScheme> 
     <cbc:ID schemeID="UN/ECE 5153" schemeAgencyID="6">VAT</cbc:ID> 
     </cac:TaxScheme> 
    </cac:TaxCategory> 
    </cac:AllowanceCharge> 
    <cac:AllowanceCharge> 
    <cbc:ChargeIndicator>true</cbc:ChargeIndicator> 
    <cbc:AllowanceChargeReason>delivery</cbc:AllowanceChargeReason> 
    <cbc:Amount currencyID="NOK">2938.45</cbc:Amount> 
    <cac:TaxCategory> 
     <cbc:ID schemeID="UN/ECE 5305" schemeAgencyID="6">S</cbc:ID> 
     <cbc:Percent>24.000</cbc:Percent> 
     <cac:TaxScheme> 
     <cbc:ID schemeID="UN/ECE 5153" schemeAgencyID="6">VAT</cbc:ID> 
     </cac:TaxScheme> 
    </cac:TaxCategory> 
    </cac:AllowanceCharge> 
    <cac:TaxTotal> 
    <cbc:TaxAmount currencyID="NOK">11229.08</cbc:TaxAmount> 
    <cac:TaxSubtotal> 
     <cbc:TaxableAmount currencyID="NOK">44916.30</cbc:TaxableAmount> 
     <cbc:TaxAmount currencyID="NOK">11229.08</cbc:TaxAmount> 
     <cac:TaxCategory> 
     <cbc:ID schemeID="UN/ECE 5305" schemeAgencyID="6">S</cbc:ID> 
     <cbc:Percent>25</cbc:Percent> 
     <cac:TaxScheme> 
      <cbc:ID schemeID="UN/ECE 5153" schemeAgencyID="6">VAT</cbc:ID> 
     </cac:TaxScheme> 
     </cac:TaxCategory> 
    </cac:TaxSubtotal> 
    </cac:TaxTotal> 
    <cac:LegalMonetaryTotal> 
    <cbc:LineExtensionAmount currencyID="NOK">46642.06</cbc:LineExtensionAmount> 
    <cbc:TaxExclusiveAmount currencyID="NOK">44916.30</cbc:TaxExclusiveAmount> 
    <cbc:TaxInclusiveAmount currencyID="NOK">56145.38</cbc:TaxInclusiveAmount> 
    <cbc:AllowanceTotalAmount currencyID="NOK">4664.21</cbc:AllowanceTotalAmount> 
    <cbc:ChargeTotalAmount currencyID="NOK">2938.45</cbc:ChargeTotalAmount> 
    <!--<cbc:PrepaidAmount currencyID=""></cbc:PrepaidAmount> 
    <cbc:PayableRoundingAmount currencyID=""></cbc:PayableRoundingAmount>--> 
    <cbc:PayableAmount currencyID="NOK">56145.38</cbc:PayableAmount> 
    </cac:LegalMonetaryTotal> 
    <cac:InvoiceLine> 
    <cbc:ID>1</cbc:ID> 
    <cbc:InvoicedQuantity unitCode="EA">1.00</cbc:InvoicedQuantity> 
    <cbc:LineExtensionAmount currencyID="NOK">12000.00</cbc:LineExtensionAmount> 
    <cac:OrderLineReference> 
     <cbc:LineID>1</cbc:LineID> 
    </cac:OrderLineReference> 
    <cac:TaxTotal> 
     <cbc:TaxAmount currencyID="NOK">2700.00</cbc:TaxAmount> 
    </cac:TaxTotal> 
    <cac:Item> 
     <cbc:Name>Skrivebord</cbc:Name> 
     <cac:SellersItemIdentification> 
     <cbc:ID>13000</cbc:ID> 
     </cac:SellersItemIdentification> 
     <cac:ClassifiedTaxCategory> 
     <cbc:ID schemeID="UN/ECE 5305" schemeAgencyID="6">S</cbc:ID> 
     <cbc:Percent>25.00</cbc:Percent> 
     <cac:TaxScheme> 
      <cbc:ID schemeID="UN/ECE 5153" schemeAgencyID="6">VAT</cbc:ID> 
     </cac:TaxScheme> 
     </cac:ClassifiedTaxCategory> 
    </cac:Item> 
    <cac:Price> 
     <cbc:PriceAmount currencyID="NOK">12000</cbc:PriceAmount> 
    </cac:Price> 
    </cac:InvoiceLine> 
    <cac:InvoiceLine> 
    <cbc:ID>2</cbc:ID> 
    <cbc:InvoicedQuantity unitCode="EA">2.00</cbc:InvoicedQuantity> 
    <cbc:LineExtensionAmount currencyID="NOK">30000.00</cbc:LineExtensionAmount> 
    <cac:OrderLineReference> 
     <cbc:LineID>2</cbc:LineID> 
    </cac:OrderLineReference> 
    <cac:TaxTotal> 
     <cbc:TaxAmount currencyID="NOK">6750.00</cbc:TaxAmount> 
    </cac:TaxTotal> 
    <cac:Item> 
     <cbc:Name>Kontorstol</cbc:Name> 
     <cac:SellersItemIdentification> 
     <cbc:ID>13002</cbc:ID> 
     </cac:SellersItemIdentification> 
     <cac:ClassifiedTaxCategory> 
     <cbc:ID schemeID="UN/ECE 5305" schemeAgencyID="6">S</cbc:ID> 
     <cbc:Percent>25.00</cbc:Percent> 
     <cac:TaxScheme> 
      <cbc:ID schemeID="UN/ECE 5153" schemeAgencyID="6">VAT</cbc:ID> 
     </cac:TaxScheme> 
     </cac:ClassifiedTaxCategory> 
    </cac:Item> 
    <cac:Price> 
     <cbc:PriceAmount currencyID="NOK">15000</cbc:PriceAmount> 
    </cac:Price> 
    </cac:InvoiceLine> 
    <cac:InvoiceLine> 
    <cbc:ID>3</cbc:ID> 
    <cbc:InvoicedQuantity unitCode="MTR">14.50</cbc:InvoicedQuantity> 
    <cbc:LineExtensionAmount currencyID="NOK">3465.50</cbc:LineExtensionAmount> 
    <cac:OrderLineReference> 
     <cbc:LineID>3</cbc:LineID> 
    </cac:OrderLineReference> 
    <cac:TaxTotal> 
     <cbc:TaxAmount currencyID="NOK">779.74</cbc:TaxAmount> 
    </cac:TaxTotal> 
    <cac:Item> 
     <cbc:Name>Persienne</cbc:Name> 
     <cac:SellersItemIdentification> 
     <cbc:ID>13006</cbc:ID> 
     </cac:SellersItemIdentification> 
     <cac:ClassifiedTaxCategory> 
     <cbc:ID schemeID="UN/ECE 5305" schemeAgencyID="6">S</cbc:ID> 
     <cbc:Percent>25.00</cbc:Percent> 
     <cac:TaxScheme> 
      <cbc:ID schemeID="UN/ECE 5153" schemeAgencyID="6">VAT</cbc:ID> 
     </cac:TaxScheme> 
     </cac:ClassifiedTaxCategory> 
    </cac:Item> 
    <cac:Price> 
     <cbc:PriceAmount currencyID="NOK">239</cbc:PriceAmount> 
    </cac:Price> 
    </cac:InvoiceLine> 
    <cac:InvoiceLine> 
    <cbc:ID>4</cbc:ID> 
    <cbc:InvoicedQuantity unitCode="KGM">62.75</cbc:InvoicedQuantity> 
    <cbc:LineExtensionAmount currencyID="NOK">1176.56</cbc:LineExtensionAmount> 
    <cac:OrderLineReference> 
     <cbc:LineID>4</cbc:LineID> 
    </cac:OrderLineReference> 
    <cac:TaxTotal> 
     <cbc:TaxAmount currencyID="NOK">264.73</cbc:TaxAmount> 
    </cac:TaxTotal> 
    <cac:Item> 
     <cbc:Name>Kaffe</cbc:Name> 
     <cac:SellersItemIdentification> 
     <cbc:ID>13005</cbc:ID> 
     </cac:SellersItemIdentification> 
     <cac:ClassifiedTaxCategory> 
     <cbc:ID schemeID="UN/ECE 5305" schemeAgencyID="6">S</cbc:ID> 
     <cbc:Percent>25.00</cbc:Percent> 
     <cac:TaxScheme> 
      <cbc:ID schemeID="UN/ECE 5153" schemeAgencyID="6">VAT</cbc:ID> 
     </cac:TaxScheme> 
     </cac:ClassifiedTaxCategory> 
    </cac:Item> 
    <cac:Price> 
     <cbc:PriceAmount currencyID="NOK">18.750</cbc:PriceAmount> 
    </cac:Price> 
    </cac:InvoiceLine> 
</Invoice> 
+0

你能提供您的驗證模式?背景:您是否設置了正確的數據類型? –

+1

在帖子中提供了更多信息:) – soberga

+0

cbc:金額是如何定義的? –

回答

1

似乎有一個基本的VB過程,弄亂了我的十進制數。我添加了一個新的測試,其中我用另一個值(cac:InvoiceLine/cbc:LineExtensionAmount)減去了一個值(cbc:LineExtensionAmount)。它給了我0.00000000000001的差異。 我的解決方案是將Xml驗證作爲獨立的exe文件運行,直到我可以在出現問題的位置進行excactly本地化。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Xml.Xsl; 
using System.Xml; 

namespace test.Transform 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 

      // Arguments 
      // Stylesheet 
      // XML-out 
      // outpath-xml-path 

      if (args.Length != 3) 
      { 
       Console.WriteLine("parameters Stylesheet XML-out outpath-xml-path"); 
       return; 
      } 

      XslCompiledTransform xslt = new XslCompiledTransform(); 
      xslt.Load(args[0]); 

      var xml = new XmlDocument(); 
      xml.Load(args[1]); 

      var xmlDocOut = new XmlDocument(); 

      using (XmlWriter xmlWriter = xmlDocOut.CreateNavigator().AppendChild()) 
      { 
       xslt.Transform(xml, null, xmlWriter); 
      } 

      xmlDocOut.Save(args[2]); 
     } 
    } 
} 

第一個參數需要ValidationSchemaPath,第二個Xml路徑,最後一個輸出路徑。

這樣稱呼它:

public static XmlDocument TestRun2(string xsltPath, string docPath, IReport _report) 
    { 

     var tempFile = Path.Combine(Path.GetTempPath(), Path.GetTempFileName()); 
     var x = Process.GetCurrentProcess().MainModule; 

     Process p = new Process(); 
     p.StartInfo.FileName = Path.Combine(Path.GetDirectoryName(x.FileName), "Transform.exe"); 
     _report.API.WriteLog(p.StartInfo.FileName); 
     p.StartInfo.Arguments = string.Format("\"{0}\" \"{1}\" \"{2}\"", xsltPath, docPath, tempFile); 
     p.StartInfo.UseShellExecute = false; 
     p.StartInfo.RedirectStandardOutput = true; 
     p.Start(); 

     string output = p.StandardOutput.ReadToEnd(); 
     p.WaitForExit(); 

     _report.API.WriteLog("Output from calling Transform.exe"); 
     _report.API.WriteLog(output); 

     if (!File.Exists(tempFile)) 
     { 
      return null; 
     } 

     var xd = new XmlDocument(); 
     xd.Load(tempFile); 

     return xd; 

    } 

} 
0

確定這並不完全回答你的問題但它可能解決你的問題

假設XML實際上包含了根元素:

var xml = 
    @"<root> 
    <book> 
     <name>Book 1</name> 
     <price>25.30</price> 
    </book> 
    <book> 
     <name>Book 2</name> 
     <price>20.77</price> 
    </book> 
    <sum>46.07</sum> 
    </root>"; 

然後你可以使用LINQ到XML做你需要的一切簡單:

var doc = XDocument.Parse(xml); 

var sum = doc.Descendants("book").Sum(e => Convert.ToDecimal(e.Element("price").Value)); 
var total = Convert.ToDecimal(doc.Root.Element("sum").Value); 

Console.WriteLine("sum: [{0}] total: [{1}] matched: [{2}]", sum, total, sum == total); 
+1

但是這不會**驗證**他的XML,這是他正在嘗試做的! :) –

+0

當然我明白這一點。但是有人可以繞過整個社會。同意你對re問題的評論:看到驗證模式。 – yamen

+1

謝謝,但恐怕我不能對此做一個解決方法;) – soberga

0

你已經鏈接到XSLT代碼是使用Schematron生成的。如果(a)您告訴我們錯誤是什麼,並且(b)您向我們展示了Schematron模式,而不是它生成的XSLT代碼,那麼可以更容易地告訴您爲什麼會出現錯誤。代碼中有超過90個「價格」事件...

另外,我看不到(a)您的問題中的書籍XML和發票XML如何相互關聯,或者(b)你所擁有的線索會讓你認爲這與文化或編碼有關 - 表面上看來這是一個相當不可思議的理論。

+1

在NORWAY-UBL-T10中有一個叫做[BIIRULE-T10-R011]的規則。這是失敗的測試之一。它將總LineExtensionAmount測試爲所有InvoiceLine/LineExtensionAmount的總和 – soberga