XSD 包含导入的 xsd 时的 Delphi XE2 XML 数据绑定向导错误

Posted

技术标签:

【中文标题】XSD 包含导入的 xsd 时的 Delphi XE2 XML 数据绑定向导错误【英文标题】:Delphi XE2 XML Data Binding Wizard bug when XSD contains IMPORTED xsd's 【发布时间】:2021-12-23 06:13:31 【问题描述】:

当 XSD 包含其他导入的 XSD 时,引用导入的 XSD 的 XML 条目不包括命名空间前缀。

这是具有多个导入的 xsd 的开始:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:inv="http://www.aade.gr/myDATA/invoice/v1.0"
xmlns:icls="https://www.aade.gr/myDATA/incomeClassificaton/v1.0" 
xmlns:ecls="https://www.aade.gr/myDATA/expensesClassificaton/v1.0" 
targetNamespace="http://www.aade.gr/myDATA/invoice/v1.0"
elementFormDefault="qualified" 
version="1.0.3">
<xs:import namespace="https://www.aade.gr/myDATA/incomeClassificaton/v1.0" 
schemaLocation="incomeClassification-v1.0.3.xsd"/>
<xs:import namespace="https://www.aade.gr/myDATA/expensesClassificaton/v1.0" 
schemaLocation="expensesClassification-v1.0.3.xsd"/>

生成的示例 XML 如下所示:

<?xml version="1.0"?>
<!--XML document created with Delphi XE2 by Three Plus Ltd-->
<InvoicesDoc xmlns="http://www.aade.gr/myDATA/invoice/v1.0" 
xmlns:icls="https://www.aade.gr/myDATA/incomeClassificaton/v1.0" 
xmlns:ecls="https://www.aade.gr/myDATA/expensesClassificaton/v1.0">
<invoice>
<uid>1</uid>
<mark>240254</mark>
<issuer>
  <vatNumber>084110672</vatNumber>
  <country>GR</country>
  <branch>0</branch>
</issuer>
<counterpart>
  <vatNumber>052354924</vatNumber>
  <country>GR</country>
  <branch>0</branch>
  <address>
    <postalCode></postalCode>
    <city>ΑΓΙΑ ΒΑΡΒΑΡΑ</city>
  </address>
</counterpart>
<invoiceHeader>
  <series>Β</series>
  <aa>59</aa>
  <issueDate>2021-01-07</issueDate>
  <invoiceType>1.1</invoiceType>
  <currency>EUR</currency>
</invoiceHeader>
<paymentMethods>
  <paymentMethodDetails>
    <type>3</type>
    <amount>254.68</amount>
  </paymentMethodDetails>
</paymentMethods>
<invoiceDetails>
  <lineNumber>1</lineNumber>
  <netValue>205.39</netValue>
  <vatCategory>1</vatCategory>
  <vatAmount>49.29</vatAmount>
  <incomeClassification>
    <classificationType>E3_561_001</classificationType>
    <classificationCategory>category1_1</classificationCategory>
    <amount>205.39</amount>
  </incomeClassification>
</invoiceDetails>
<invoiceSummary>
  <totalNetValue>205.39</totalNetValue>
  <totalVatAmount>49.29</totalVatAmount>
  <totalWithheldAmount>0.00</totalWithheldAmount>
  <totalFeesAmount>0.00</totalFeesAmount>
  <totalStampDutyAmount>0.00</totalStampDutyAmount>
  <totalOtherTaxesAmount>0.00</totalOtherTaxesAmount>
  <totalDeductionsAmount>0.00</totalDeductionsAmount>
  <totalGrossValue>254.68</totalGrossValue>
  <incomeClassification>
    <classificationType>E3_561_001</classificationType>
    <classificationCategory>category1_1</classificationCategory>
    <amount>205.39</amount>
  </incomeClassification>
</invoiceSummary>
</invoice>
</InvoicesDoc>

此 XML 未通过验证,因为收入分类条目应包含前缀“icls”,如下所示:

<?xml version="1.0"?>
<!--XML document created with Delphi XE2 by Three Plus Ltd-->
<InvoicesDoc xmlns="http://www.aade.gr/myDATA/invoice/v1.0" 
xmlns:icls="https://www.aade.gr/myDATA/incomeClassificaton/v1.0" 
xmlns:ecls="https://www.aade.gr/myDATA/expensesClassificaton/v1.0">
<invoice>
<uid>1</uid>
<mark>241108</mark>
<issuer>
  <vatNumber>084110672</vatNumber>
  <country>GR</country>
  <branch>0</branch>
</issuer>
<counterpart>
  <vatNumber>130197574</vatNumber>
  <country>GR</country>
  <branch>0</branch>
  <address>
    <postalCode>12135</postalCode>
    <city>ΠΕΡΙΣΤΕΡΙ</city>
  </address>
</counterpart>
<invoiceHeader>
  <series>Β</series>
  <aa>274</aa>
  <issueDate>2021-01-16</issueDate>
  <invoiceType>1.1</invoiceType>
  <currency>EUR</currency>
</invoiceHeader>
<paymentMethods>
  <paymentMethodDetails>
    <type>5</type>
    <amount>45.50</amount>
  </paymentMethodDetails>
</paymentMethods>
<invoiceDetails>
  <lineNumber>1</lineNumber>
  <netValue>36.70</netValue>
  <vatCategory>1</vatCategory>
  <vatAmount>8.80</vatAmount>
  <incomeClassification>
    <icls:classificationType>E3_561_001</icls:classificationType>
    <icls:classificationCategory>category1_1</icls:classificationCategory>
    <icls:amount>36.70</icls:amount>
  </incomeClassification>
</invoiceDetails>
<invoiceSummary>
  <totalNetValue>36.70</totalNetValue>
  <totalVatAmount>8.80</totalVatAmount>
  <totalWithheldAmount>0.00</totalWithheldAmount>
  <totalFeesAmount>0.00</totalFeesAmount>
  <totalStampDutyAmount>0.00</totalStampDutyAmount>
  <totalOtherTaxesAmount>0.00</totalOtherTaxesAmount>
  <totalDeductionsAmount>0.00</totalDeductionsAmount>
  <totalGrossValue>45.50</totalGrossValue>
  <incomeClassification>
    <icls:classificationType>E3_561_001</icls:classificationType>
    <icls:classificationCategory>category1_1</icls:classificationCategory>
    <icls:amount>36.70</icls:amount>
  </incomeClassification>
</invoiceSummary>
</invoice>
</InvoicesDoc>

有没有办法更改向导创建的单元以生成带有前缀的正确 XML?

【问题讨论】:

【参考方案1】:

我终于找到了手动添加前缀的解决方法:

首先为所有三个条目声明变量(这里只显示第一个):

nodeClassificationType:IXMLNode;

然后替换代码:

IncomeClassificationType.ClassificationType:='xxxxxx'

与:

nodeClassificationType:=
  IncomeClassificationType.AddChild('icls:classificationType');
nodeClassificationType.Text:='xxxxx'

效果很好!!

【讨论】:

以上是关于XSD 包含导入的 xsd 时的 Delphi XE2 XML 数据绑定向导错误的主要内容,如果未能解决你的问题,请参考以下文章

针对包含 xsd:import without location 的 XSD 验证 XML

使用C#导入使用XSD验证XML

如何将外部xsd导入另一个xsd?

XSD 到 XML 的导入

将 xsd 导入 wsdl

如何将 XSD 类型导入根架构?