【问题标题】:Extend XSD schema to allow a new attribute in one XSD standard element type扩展 XSD 模式以允许一种 XSD 标准元素类型中的新属性
【发布时间】:2021-12-24 00:16:51
【问题描述】:

我需要在 XSD 文件中添加不同类型的文档。我希望能够在文档元素中添加一个名为“type”的属性,如下所示:

<xs:annotation>

   <xs:documentation type="doc">
       My documentation....
   </xs:documentation>

   <xs:documentation type="example">
       My first example...
   </xs:documentation>

   <xs:documentation type="example">
       My second example...
   </xs:documentation>

   <xs:documentation type="tip">
        My tip.
   </xs:documentation>

</xs:annotation>

我可以更改 XSD 中所需的所有内容以使这成为可能。我该怎么做?

【问题讨论】:

    标签: xml xsd xsd-validation xsd-1.1


    【解决方案1】:

    除了在type 属性上使用命名空间的@MartinHonnen's idea 之外,还有其他一些可能性:

    1. 使用子type 元素:

      <xs:documentation>
        <type>tip</type>
        My tip.
      </xs:documentation>
      
    2. 使用按类型命名的包装元素,通常比使用type 属性更可取:

      <xs:documentation>
        <tip>
          My tip.
        <tip>
      </xs:documentation>
      

    当然,这些添加的元素本身可以位于您自己设计的命名空间中。

    【讨论】:

      【解决方案2】:

      架构的架构已经允许这样做,但仅适用于自定义命名空间中的属性,例如

      <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
          xmlns:my="http://example.com/my-annotation-types"
          xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" elementFormDefault="qualified"
          vc:minVersion="1.1">
          
          <xs:element name="foo" type="xs:string">
              <xs:annotation>
                  <xs:documentation my:type="doc"></xs:documentation>
              </xs:annotation>
          </xs:element>
          
      </xs:schema>
      

      在大多数地方都允许作为 schema 声明、使用和扩展 xs:annotated 的架构:

      <xs:complexType name="openAttrs">
          <xs:annotation>
            <xs:documentation>
             This type is extended by almost all schema types
             to allow attributes from other namespaces to be
             added to user schemas.
           </xs:documentation>
          </xs:annotation>
          <xs:complexContent>
            <xs:restriction base="xs:anyType">
              <xs:anyAttribute namespace="##other" processContents="lax"/>
            </xs:restriction>
          </xs:complexContent>
        </xs:complexType>
        <xs:complexType name="annotated">
          <xs:annotation>
            <xs:documentation>
             This type is extended by all types which allow annotation
             other than &lt;schema> itself
           </xs:documentation>
          </xs:annotation>
          <xs:complexContent>
            <xs:extension base="xs:openAttrs">
              <xs:sequence>
                <xs:element ref="xs:annotation" minOccurs="0"/>
              </xs:sequence>
              <xs:attribute name="id" type="xs:ID"/>
            </xs:extension>
          </xs:complexContent>
        </xs:complexType>
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-29
      • 1970-01-01
      • 2012-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多