首页 > 解决方案 > 通配符 ##any 错误 xml 架构验证

问题描述

根据 C# 中的架构验证 XSD 时出现上述错误。Schema 查找列出的标签,其他标签是可选的。下面是我的 XSD 文件。xml 文件需要如何修改,以确保它具有以下 XSD 中列出的标签并忽略额外的标签。它正在使用 XML 文本阅读器进行架构验证

在此处输入图像描述

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="InvoiceExport" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
  <xs:element name="InvoiceExport" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element name="Invoice">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Invoice_ID" type="xs:string" minOccurs="1" />             
              <xs:element name="BillingEntity" minOccurs="1" maxOccurs="unbounded">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="Name" type="xs:string" minOccurs="1" />                  
                    <xs:element name="VendorNumber" type="xs:string" minOccurs="1" />                                        
                    <xs:any processContents="lax" maxOccurs="unbounded"/>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
              <xs:element name="InvoiceBaseInfo" minOccurs="1" maxOccurs="unbounded">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="InvoiceDate" type="xs:string" minOccurs="1" />                  
                    <xs:element name="BillingEntityInvoiceNumber" type="xs:string" minOccurs="1" />                    
                    <xs:element name="ReceivedDate" type="xs:string" minOccurs="1" />                   
                    <xs:element name="AdjustedBilledAmount" type="xs:string" minOccurs="1" />                    
                    <xs:element name="UserFields" minOccurs="1" maxOccurs="unbounded">
                      <xs:complexType>
                        <xs:all>
                          <xs:element name="FirstApproverID" type="xs:string" minOccurs="1" />
                          <xs:element name="LastApproverID" type="xs:string" minOccurs="1" />
                          <xs:element name="LegalSpendCode" type="xs:string" minOccurs="0" />
                        </xs:all>
                      </xs:complexType>
                    </xs:element>
                     <xs:any processContents="lax" maxOccurs="unbounded"/>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
              <xs:element name="InvoiceMatter" minOccurs="1" maxOccurs="unbounded">
                <xs:complexType>
                  <xs:sequence>                       
                    <xs:element name="Priority_CD" type="xs:string" minOccurs="1" />                   
                    <xs:element name="InvoiceMatterDetail" minOccurs="0" maxOccurs="unbounded">
                      <xs:complexType>
                        <xs:sequence>                        
                          <xs:any processContents="lax" maxOccurs="unbounded"/>
                        </xs:sequence>
                      </xs:complexType>
                    </xs:element>
                    <xs:element name="InvoiceMatterAllocation" minOccurs="1" maxOccurs="unbounded">
                      <xs:complexType>
                        <xs:sequence>                          
                          <xs:element name="AllocationAmount" type="xs:string" minOccurs="1" />
                          <xs:element name="ChargeBackAccount1" type="xs:string" minOccurs="1" />
                          <xs:element name="ChargeBackAccount2" type="xs:string" minOccurs="1" />
                          <xs:element name="ChargeBackAccount3" type="xs:string" minOccurs="1" />
                          <xs:element name="UserFields" type="xs:string" minOccurs="0" />
                          <xs:any processContents="lax" maxOccurs="unbounded"/>
                        </xs:sequence>
                      </xs:complexType>
                    </xs:element>
                    <xs:element name="InvoiceMatterApproval" minOccurs="1" maxOccurs="unbounded">                   
                      <xs:complexType>
                        <xs:sequence>
                          <xs:element name="EmployeeName" type="xs:string" minOccurs="1" />                          
                          <xs:any processContents="lax" maxOccurs="unbounded"/>
                        </xs:sequence>
                      </xs:complexType>
                    </xs:element>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>         
              <xs:any processContents="lax" maxOccurs="unbounded"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>

标签: c#.netxmlxsdxml-validation

解决方案


问题是您的架构允许任意数量的称为 InvoiceMatter 的元素,后跟一个可以具有任何名称的元素(包括名称 InvoiceMatter);因此,如果找到 InvoiceMatter 元素,则它是否匹配特定元素粒子或通配符是不明确的。

XSD 1.1 通过说特定元素粒子总是优先解决这个问题,因此一种解决方案是简单地使用 XSD 1.1 处理器(例如 Saxon)。

如果您不能这样做,通常的 XSD 1.0 解决方案是使用 xs:any 通配符上的 targetNamespace 属性来通过命名空间(例如targetNamespace="##other")来限制它。但这是对您的设计的改变;如果您想允许同一名称空间中的元素,那么您遇到了问题。


推荐阅读