【问题标题】:Group, choice,all or sequence XSD分组、选择、全部或序列 XSD
【发布时间】:2014-11-22 05:47:15
【问题描述】:

我为元素“标签”定义了这种类型,标签可以是图像引用格式

<complexType name="Tag">
        <choice minOccurs="0" maxOccurs="unbounded">
            <element name="quote" type="rel:Quote" maxOccurs="1"
                minOccurs="0">
            </element>
            <element name="image" type="rel:Image" minOccurs="0"
                maxOccurs="1">
            </element>
            <element name="format" type="rel:Format" minOccurs="0"
                maxOccurs="1">
            </element>
        </choice>
</complexType>

我该如何使用例如

<rel:image></rel:image>

而不是

 <rel:tag><rel:image></rel:image></rel:tag>

【问题讨论】:

    标签: xml xsd


    【解决方案1】:

    XML Schema 有一个 substitition groups 结构,它允许一些元素替换一些其他元素。这可能就是你想要的。

    例如,您可以定义tag 类型的元素Tag

    <xs:element name="tag" type="Tag"/>
    

    并在某处使用它:

    <xs:complexType name="Tags">
        <xs:sequence>
            <xs:element ref="tag"/>
        </xs:sequence>
    </xs:complexType>
    

    现在您可以定义元素quoteimageformat 来替换tag

    <xs:element name="quote" type="Quote" substitutionGroup="tag"/>
    <xs:element name="image" type="Image" substitutionGroup="tag"/>
    <xs:element name="format" type="Format" substitutionGroup="tag"/>
    

    QuoteImageFormat 必须派生自 Tag 类型。

    现在在所有使用tag 元素的地方(每个引用),它都可以替换为quoteimageformat 元素。

    地理标记语言是使用此模式的架构示例,这里有一个链接到有些过时(但简洁)的版本2.1.2

    架构定义了一个抽象元素_Geometry

    <element name="_Geometry" type="gml:AbstractGeometryType" abstract="true"/>
    
    <complexType name="AbstractGeometryType" abstract="true">
        <annotation>
            <documentation>
        All geometry elements are derived from this abstract supertype; 
        a geometry element may have an identifying attribute (gid). 
        It may be associated with a spatial reference system.
      </documentation>
        </annotation>
        <complexContent>
            <restriction base="anyType">
                <attribute name="gid" type="ID" use="optional"/>
                <attribute name="srsName" type="anyURI" use="optional"/>
            </restriction>
        </complexContent>
    </complexType>
    

    然后定义一个元素Polygon可以替代_Geometry

    <element name="Polygon" type="gml:PolygonType" substitutionGroup="gml:_Geometry"/>
    
    <complexType name="PolygonType">
        <annotation>
            <documentation>
        A Polygon is defined by an outer boundary and zero or more inner 
        boundaries which are in turn defined by LinearRings.
      </documentation>
        </annotation>
        <complexContent>
            <extension base="gml:AbstractGeometryType">
                <sequence>
                    <element ref="gml:outerBoundaryIs"/>
                    <element ref="gml:innerBoundaryIs" minOccurs="0" maxOccurs="unbounded"/>
                </sequence>
            </extension>
        </complexContent>
    </complexType>
    

    所以现在您可以使用Polygon 和引用_Geometry 的其他元素:

    <complexType name="GeometryAssociationType">
        <annotation>
            <documentation>
        An instance of this type (e.g. a geometryMember) can either 
        enclose or point to a primitive geometry element. When serving 
        as a simple link that references a remote geometry instance, 
        the value of the gml:remoteSchema attribute can be used to 
        locate a schema fragment that constrains the target instance.
      </documentation>
        </annotation>
        <sequence minOccurs="0">
            <element ref="gml:_Geometry"/>
        </sequence>
        <attributeGroup ref="xlink:simpleAttrs"/>
        <attribute ref="gml:remoteSchema" use="optional"/>
        <!-- <attributeGroup ref="gml:AssociationAttributeGroup"/> -->
    </complexType>
    

    【讨论】:

      【解决方案2】:

      如果Tag 是元素tag 的类型,那么按照您定义它的方式,tag 元素可以包含quoteimageformat 子元素的序列。

      如果您希望使用image 代替tag,则应将image 元素定义为替换组的成员,并以tag 作为其头部。

      【讨论】:

        猜你喜欢
        • 2011-11-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-03
        • 1970-01-01
        • 2020-09-26
        相关资源
        最近更新 更多