簡體   English   中英

XSD。 用無序元素集擴展類型

[英]xsd. Extend a type with unordered elements set

我的.xsd文件中具有以下元素結構:

<xs:complexType name="the_rootest" abstract="true">
 <xs:sequence>
     <xs:element name="info" type="complex_type_here"/>
 </xs:sequence>
     <xs:attribute name="Name" type="cpo:string"/>
</xs:complexType>

<xs:complexType name="object" abstract="true">
<xs:complexContent>
    <xs:extension base="the_rootest">
        <xs:sequence>
            <xs:element name="is_working" type="cpo:bool"/>
            <xs:element name="has_errors" type="cpo:bool"/>
            <xs:element name="state" type ="cpo:string"/>
        </xs:sequence>
    </xs:extension>
</xs:complexContent>
</xs:complexType>

<xs:complexType name="extended_object">
<xs:complexContent>
    <xs:extension base="object">
        <xs:sequence>
            <xs:element name="is_zero" type="cpo:bool"/>
            <xs:element name="complex" type="another_complex_type_here" />
        </xs:sequence>
    </xs:extension>
</xs:complexContent>
</xs:complexType>

<xs:complexType name="one_more_extended">
<xs:complexContent>
    <xs:extension base="object">
        <xs:sequence>
        </xs:sequence>
    </xs:extension>
</xs:complexContent>
</xs:complexType>

我在xml文件中具有“ extended_object”類型的對象,該文件具有以下字段順序

info
Name
is_working
has_errors
is_zero
complex
state

如您所見,“狀態”字段位於對象元素的末尾。 問題是我應該如何糾正我的架構以使該對象有效? 謝謝。

在內容模型中允許無序元素的通常方法是使用xs:all

但是, xs:all無法擴展。

因此,如果您希望創建一個新的元素,其內容模型類似於現有元素,但可能具有其他元素,這些元素的順序可以不同於現有元素,則唯一的選擇就是創建一個獨立於原始元素的新內容模型。 (至少可以肯定地共享子元素的類型定義。)

XSD的設計不是完美的正交和一致。 這是看似合理的期望無法實現的領域。 從好的方面來看,無序性在實踐中往往被高估,並且在一個聲明中記錄與另一個依賴關系的注釋是不理想的,但可行的。

如果使用XML Schema 1.1,則可以像這樣使用xs:all擴展名(我修改了模式,使其可以在標准類型下使用):

<?xml version="1.0" encoding="utf-8"?>
<xs:schema
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
  vc:minVersion="1.1">
    <xs:complexType name="the_rootest" abstract="true">
        <xs:all>
            <xs:element name="info" type="xs:string"/>
        </xs:all>
        <xs:attribute name="Name" type="xs:string"/>
    </xs:complexType>

    <xs:complexType name="object" abstract="true">
        <xs:complexContent>
            <xs:extension base="the_rootest">
                <xs:all>
                    <xs:element name="is_working" type="xs:boolean"/>
                    <xs:element name="has_errors" type="xs:boolean"/>
                    <xs:element name="state" type ="xs:string"/>
                </xs:all>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>

    <xs:complexType name="extended_object">
        <xs:complexContent>
            <xs:extension base="object">
                <xs:all>
                    <xs:element name="is_zero" type="xs:boolean"/>
                    <xs:element name="complex" type="xs:string" />
                </xs:all>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>

    <xs:complexType name="one_more_extended">
        <xs:complexContent>
            <xs:extension base="object">
                <xs:sequence>
                </xs:sequence>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>

    <xs:element name="element" type="extended_object"/>
</xs:schema>

此XML文檔已成功驗證:

<?xml version="1.0" encoding="UTF-8"?>
<element xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="test.xsd"
 >
    <info>test</info>
    <is_working>true</is_working>
    <has_errors>true</has_errors>
    <is_zero>true</is_zero>
    <complex>true</complex>
    <state>true</state>
</element>

暫無
暫無

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

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