簡體   English   中英

如何驗證名稱相同但類型不同的多個元素

[英]How to validate multiple elements with same name but with different types

我正在嘗試針對具有不同類型的重復元素的 XSD 驗證 XML。 但無法讓它發揮作用。

XML

<notifications xmlns="http://soap.sforce.com/2005/09/outbound">
    <Notification>
        <sObject xmlns:sf="urn:sobject.soap.sforce.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Tracking">
            <sf:Id>a2L1g000000OzM7EAK</sf:Id>
        </sObject>
        <sObject xmlns:sf="urn:sobject.soap.sforce.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Log">
            <sf:Id>a1t1g000001wMQrAAM</sf:Id>
        </sObject>
    </Notification>
</notifications>

正如您在上面看到的, sObject元素出現了兩次。 以下是我想出的 XSD。

主 XSD

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:ob="http://soap.sforce.com/2005/09/outbound" xmlns:sf="urn:sobject.soap.sforce.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://soap.sforce.com/2005/09/outbound" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:import schemaLocation="schema1.xsd" namespace="urn:sobject.soap.sforce.com" />
  <xs:element name="notifications">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Notification">
         <xs:complexType>
            <xs:sequence>
              <xs:element name="sObject" maxOccurs="unbounded" type="ob:Tracking" />
              <!--xs:element name="sObject" type="ob:Log"/-->
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:complexType name="Tracking">
    <xs:sequence>
        <xs:element ref="sf:Id" />
    </xs:sequence>
    <xs:anyAttribute processContents="skip"/>
  </xs:complexType>
  <xs:complexType name="Log">
    <xs:sequence>
        <xs:element ref="sf:Id" />
    </xs:sequence>
    <xs:anyAttribute processContents="skip"/>
  </xs:complexType>
</xs:schema>

依賴模式1.xsd

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="urn:sobject.soap.sforce.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Id" type="xs:string" />
</xs:schema>

注意:我無法更改 XML 結構,因為它來自外部系統,這是一個最小的可重現樣本,實際的 XML 非常復雜。

訣竅是將基類型sObjectType定義為LogTracking的超類型。 我將其定義為抽象類型,因為實際上讓元素使用它是沒有意義的。

這似乎對我有用:

通知.xsd

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    targetNamespace="http://soap.sforce.com/2005/09/outbound"
    attributeFormDefault="unqualified" elementFormDefault="qualified"

    xmlns:not="http://soap.sforce.com/2005/09/outbound" 
    xmlns:sobj="urn:sobject.soap.sforce.com">

    <xs:import schemaLocation="schema1.xsd" namespace="urn:sobject.soap.sforce.com" />

    <xs:element name="notifications">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Notification">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="sObject" maxOccurs="unbounded"
                                type="not:sObjectAbstractType" />
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:complexType name="sObjectAbstractType" abstract="true" />

    <xs:complexType name="Tracking">
        <xs:complexContent>
            <xs:extension base="not:sObjectAbstractType">
                <xs:sequence>
                    <xs:element ref="sobj:Id" />
                </xs:sequence>
                <xs:anyAttribute processContents="skip" />
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>

    <xs:complexType name="Log">
        <xs:complexContent>
            <xs:extension base="not:sObjectAbstractType">
                <xs:sequence>
                    <xs:element ref="sobj:Id" />
                </xs:sequence>
                <xs:anyAttribute processContents="skip" />
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>

</xs:schema>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM