如何跳过 XSD 模式中特定字段的验证

How to skip validation on specific fields in XSD schema

我有一个架构 XSD 文件,Main.xsd 和 Child.xsd。

Main.xsd:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ns1="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" targetNamespace="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xsd:import namespace="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" schemaLocation="Child.xsd"/>
    <xsd:element name="Invoice">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:any namespace="##any" processContents="skip"/>
                <xsd:element ref="cbc:UBLVersionID" minOccurs="0"/>
                <xsd:element ref="cbc:CustomizationID"/>
                <xsd:element ref="cbc:ProfileID"/>
                <xsd:any namespace="##any" processContents="skip" maxOccurs="unbounded"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

Child.xsd:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xsd:element name="UBLVersionID" type="cbc:UBLVersionIDType"/>
    <xsd:element name="CustomizationID" type="cbc:CustomizationIDType"/>
    <xsd:element name="ProfileID" type="cbc:ProfileIDType"/>
    <xsd:complexType name="UBLVersionIDType">
        <xsd:simpleContent>
            <xsd:extension base="xsd:string">
                <xsd:anyAttribute namespace="##any" processContents="skip"/>
            </xsd:extension>
        </xsd:simpleContent>
    </xsd:complexType>
    <xsd:simpleType name="CustomizationIDType">
        <xsd:restriction base="xsd:string">
            <xsd:enumeration value="urn:cen.eu:en16931:2017#compliant#urn:fdc:nen.nl:nlcius:v1.0"/>
        </xsd:restriction>
    </xsd:simpleType>
    <xsd:simpleType name="ProfileIDType">
        <xsd:restriction base="xsd:string">
            <xsd:enumeration value="urn:fdc:peppol.eu:2017:poacc:billing:01:1.0"/>
        </xsd:restriction>
    </xsd:simpleType>
</xsd:schema>

正如您在 Main.xsd 中看到的,我们正在检查可选的 UBLVersionID,CustomizationID 和 ProfileID,两者都是必需的。 在 UBLVersionID 之前和 ProfileID 之后的 xsd:any 语句,跳过任何元素或字段,因为我们只需要检查在 Main.xsd 中定义的字段。 Child.xsd 定义了字段、规则等...

我的 XSD 可以很好地处理我拥有的所有 2 个 XML:A.xml、B.xml:

A.XML: 有 UBLExtensions 和 UBLVersionID

<Invoice xmlns:ns2="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <ext:UBLExtensions xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2">
        <ext:UBLExtension>
            <ext:ExtensionContent>
                <seef:SEEFExtensionWrapper xmlns:seef="urn:www.energie-efactuur.nl:profile:invoice:ver1.0">
                    <seef:UtilityConsumptionPoint>
                        <cbc:ID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">871685920003767063</cbc:ID>
                        <seef:Address>
                            <cac:Address xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2">
                                <cbc:StreetName xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">Zuider IJdijk 46</cbc:StreetName>
                                <cbc:CityName xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">AMSTERDAM</cbc:CityName>
                                <cbc:PostalZone xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">1095 KN</cbc:PostalZone>
                                <cac:Country>
                                    <cbc:IdentificationCode xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">NL</cbc:IdentificationCode>
                                </cac:Country>
                            </cac:Address>
                        </seef:Address>
                    </seef:UtilityConsumptionPoint>
                </seef:SEEFExtensionWrapper>
            </ext:ExtensionContent>
        </ext:UBLExtension>
    </ext:UBLExtensions>
    <cbc:UBLVersionID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">2.1</cbc:UBLVersionID>
    <cbc:CustomizationID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">urn:cen.eu:en16931:2017#compliant#urn:fdc:nen.nl:nlcius:v1.0</cbc:CustomizationID>
    <cbc:ProfileID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">urn:fdc:peppol.eu:2017:poacc:billing:01:1.0</cbc:ProfileID>
    <cbc:ID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">41014619807</cbc:ID>
    <cbc:IssueDate xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">2021-11-10</cbc:IssueDate>
    <cbc:DueDate xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">2021-11-24</cbc:DueDate>
    <cbc:InvoiceTypeCode xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">380</cbc:InvoiceTypeCode>
    <cbc:Note xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">Zuider IJdijk 46, 1095 KN AMSTERDAM; Voor de algemene voorwaarden, zie: https://www.liander.nl/algemene-voorwaarden.</cbc:Note>
    <cbc:DocumentCurrencyCode xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">EUR</cbc:DocumentCurrencyCode>
    <cbc:BuyerReference xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">28224654</cbc:BuyerReference>
</Invoice>

B.XML:具有 UBLVersionID 而不是 UBLExtensions:

<Invoice xmlns:ns2="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <cbc:UBLVersionID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">2.1</cbc:UBLVersionID>
    <cbc:CustomizationID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">urn:cen.eu:en16931:2017#compliant#urn:fdc:nen.nl:nlcius:v1.0</cbc:CustomizationID>
    <cbc:ProfileID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">urn:fdc:peppol.eu:2017:poacc:billing:01:1.0</cbc:ProfileID>
    <cbc:ID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">41014619807</cbc:ID>
    <cbc:IssueDate xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">2021-11-10</cbc:IssueDate>
    <cbc:DueDate xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">2021-11-24</cbc:DueDate>
    <cbc:InvoiceTypeCode xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">380</cbc:InvoiceTypeCode>
    <cbc:Note xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">Zuider IJdijk 46, 1095 KN AMSTERDAM; Voor de algemene voorwaarden, zie: https://www.liander.nl/algemene-voorwaarden.</cbc:Note>
    <cbc:DocumentCurrencyCode xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">EUR</cbc:DocumentCurrencyCode>
    <cbc:BuyerReference xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">28224654</cbc:BuyerReference>
</Invoice>

我的 C.XML 正在产生验证错误,因为它没有 UBLExtensions 和 UBLVersionID 段。可以看到,schema中的xsd:any和UBLVersionID都是可选的。

C.XML:

<Invoice xmlns:ns2="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <cbc:CustomizationID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">urn:cen.eu:en16931:2017#compliant#urn:fdc:nen.nl:nlcius:v1.0</cbc:CustomizationID>
    <cbc:ProfileID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">urn:fdc:peppol.eu:2017:poacc:billing:01:1.0</cbc:ProfileID>
    <cbc:ID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">41014619807</cbc:ID>
    <cbc:IssueDate xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">2021-11-10</cbc:IssueDate>
    <cbc:DueDate xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">2021-11-24</cbc:DueDate>
    <cbc:InvoiceTypeCode xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">380</cbc:InvoiceTypeCode>
    <cbc:Note xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">Zuider IJdijk 46, 1095 KN AMSTERDAM; Voor de algemene voorwaarden, zie: https://www.liander.nl/algemene-voorwaarden.</cbc:Note>
    <cbc:DocumentCurrencyCode xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">EUR</cbc:DocumentCurrencyCode>
    <cbc:BuyerReference xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">28224654</cbc:BuyerReference>
</Invoice>

我尝试了很多,我应该在我的架构中更改什么,以便它适用于我的所有 3 个 xml。请帮忙。

错误信息是

The element 'Invoice' in namespace 'urn:oasis:names:specification:ubl:schema:xsd:Invoice-2' has invalid child element 'ProfileID' in namespace 'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2'. List of possible elements expected: 'UBLVersionID, CustomizationID' in namespace 'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2'.

这是在告诉您要么需要 UBLVersionID,要么需要 CustomizationID,因为 UBLVersionID 是可选的。哪个当然会让你感到奇怪,因为它确实存在。

如果您像下面一样再次添加 CustomizationID,它会真正生效。

<Invoice xmlns:ns2="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <cbc:CustomizationID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">urn:cen.eu:en16931:2017#compliant#urn:fdc:nen.nl:nlcius:v1.0</cbc:CustomizationID>
    <cbc:CustomizationID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">urn:cen.eu:en16931:2017#compliant#urn:fdc:nen.nl:nlcius:v1.0</cbc:CustomizationID>
    <cbc:ProfileID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">urn:fdc:peppol.eu:2017:poacc:billing:01:1.0</cbc:ProfileID>
    <cbc:ID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">41014619807</cbc:ID>
    <cbc:IssueDate xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">2021-11-10</cbc:IssueDate>
    <cbc:DueDate xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">2021-11-24</cbc:DueDate>
    <cbc:InvoiceTypeCode xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">380</cbc:InvoiceTypeCode>
    <cbc:Note xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">Zuider IJdijk 46, 1095 KN AMSTERDAM; Voor de algemene voorwaarden, zie: https://www.liander.nl/algemene-voorwaarden.</cbc:Note>
    <cbc:DocumentCurrencyCode xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">EUR</cbc:DocumentCurrencyCode>
    <cbc:BuyerReference xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">28224654</cbc:BuyerReference>
</Invoice>

这表明匹配 CustomizationID 的 <xsd:any namespace="##any" processContents="skip" />

您需要实际定义其他字段,但作为可选字段。

Main.xml

<?xml version="1.0" encoding="utf-16"?>
<xs:schema xmlns:ns2="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:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:b="http://schemas.microsoft.com/BizTalk/2003" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" xmlns:seef="urn:www.energie-efactuur.nl:profile:invoice:ver1.0" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:import schemaLocation=".\A1.xsd" namespace="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" />
  <xs:import schemaLocation=".\A3.xsd" namespace="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" />
  <xs:annotation>
    <xs:appinfo>
      <references xmlns="http://schemas.microsoft.com/BizTalk/2003">
        <reference targetNamespace="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" />
        <reference targetNamespace="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" />
        <reference targetNamespace="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" />
        <reference targetNamespace="urn:www.energie-efactuur.nl:profile:invoice:ver1.0" />
      </references>
    </xs:appinfo>
  </xs:annotation>
  <xs:element name="Invoice">
    <xs:complexType>
      <xs:sequence>
        <xs:element minOccurs="0" ref="ext:UBLExtensions" />
        <xs:element minOccurs="0" ref="cbc:UBLVersionID" />
        <xs:element ref="cbc:CustomizationID" />
        <xs:element ref="cbc:ProfileID" />
        <xs:element ref="cbc:ID" />
        <xs:element ref="cbc:IssueDate" />
        <xs:element ref="cbc:DueDate" />
        <xs:element ref="cbc:InvoiceTypeCode" />
        <xs:element ref="cbc:Note" />
        <xs:element ref="cbc:DocumentCurrencyCode" />
        <xs:element ref="cbc:BuyerReference" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Child1.xml

<?xml version="1.0" encoding="utf-16"?>
<xs:schema xmlns:tns="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:import schemaLocation=".\Child2.xsd" namespace="urn:www.energie-efactuur.nl:profile:invoice:ver1.0" />
  <xs:element name="UBLExtensions">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="UBLExtension">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="ExtensionContent">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element xmlns:q1="urn:www.energie-efactuur.nl:profile:invoice:ver1.0" ref="q1:SEEFExtensionWrapper" />
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Child2.xml

<?xml version="1.0" encoding="utf-16"?>
<xs:schema xmlns:tns="urn:www.energie-efactuur.nl:profile:invoice:ver1.0" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="urn:www.energie-efactuur.nl:profile:invoice:ver1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:import schemaLocation=".\Child3.xsd" namespace="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" />
  <xs:import schemaLocation=".\Child4.xsd" namespace="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" />
  <xs:element name="SEEFExtensionWrapper">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="UtilityConsumptionPoint">
          <xs:complexType>
            <xs:sequence>
              <xs:element xmlns:q1="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" ref="q1:ID" />
              <xs:element name="Address">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element xmlns:q2="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" ref="q2:Address" />
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Child3.xml

<?xml version="1.0" encoding="utf-16"?>
<xs:schema xmlns:tns="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="ID" type="xs:unsignedLong" />
  <xs:element name="StreetName" type="xs:string" />
  <xs:element name="CityName" type="xs:string" />
  <xs:element name="PostalZone" type="xs:string" />
  <xs:element name="IdentificationCode" type="xs:string" />
  <xs:element name="UBLVersionID" type="xs:decimal" />
  <xs:element name="CustomizationID" type="xs:string" />
  <xs:element name="ProfileID" type="xs:string" />
  <xs:element name="IssueDate" type="xs:date" />
  <xs:element name="DueDate" type="xs:date" />
  <xs:element name="InvoiceTypeCode" type="xs:unsignedShort" />
  <xs:element name="Note" type="xs:string" />
  <xs:element name="DocumentCurrencyCode" type="xs:string" />
  <xs:element name="BuyerReference" type="xs:unsignedInt" />
</xs:schema>

Child4.xml

<?xml version="1.0" encoding="utf-16"?>
<xs:schema xmlns:tns="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:import schemaLocation=".\Child3.xsd" namespace="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" />
  <xs:element name="Address">
    <xs:complexType>
      <xs:sequence>
        <xs:element xmlns:q1="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" ref="q1:StreetName" />
        <xs:element xmlns:q2="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" ref="q2:CityName" />
        <xs:element xmlns:q3="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" ref="q3:PostalZone" />
        <xs:element name="Country">
          <xs:complexType>
            <xs:sequence>
              <xs:element xmlns:q4="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" ref="q4:IdentificationCode" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>