Add attributes to a simpletype or restriction to a complextype in Xml Schema

删除回忆录丶 提交于 2019-12-03 01:34:24

问题


The problem is as follows:

I have the following XML snippet:

<time format="minutes">11:60</time>

The problem is that I can't add both the attribute and the restriction at the same time. The attribute format can only have the values minutes, hours and seconds. The time has the restrictionpattern \d{2}:\d{2}

<xs:element name="time" type="timeType"/>
...
<xs:simpleType name="formatType">
<xs:restriction base="xs:string">
    <xs:enumeration value="minutes"/>
    <xs:enumeration value="hours"/>
    <xs:enumeration value="seconds"/>
</xs:restriction>
</xs:simpleType>
<xs:complexType name="timeType">
    <xs:attribute name="format">
        <xs:simpleType>
            <xs:restriction base="formatType"/>
        </xs:simpleType>
    </xs:attribute>
</xs:complexType>

If I make a complex type of timeType, I can add an attribute, but not the restriction, and if I make a simple type, I can add the restriction but not the attribute. Is there any way to get around this problem. This is not a very strange restriction, or is it?


回答1:


To add attributes you have to derive by extension, to add facets you have to derive by restriction. Therefore this has to be done in two steps for the element's child content. The attribute can be defined inline:

<xsd:simpleType name="timeValueType">
  <xsd:restriction base="xsd:token">
    <xsd:pattern value="\d{2}:\d{2}"/>
  </xsd:restriction>
</xsd:simpleType>

<xsd:complexType name="timeType">
  <xsd:simpleContent>
    <xsd:extension base="timeValueType">
      <xsd:attribute name="format">
        <xsd:simpleType>
          <xsd:restriction base="xsd:token">
            <xsd:enumeration value="seconds"/>
            <xsd:enumeration value="minutes"/>
            <xsd:enumeration value="hours"/>
          </xsd:restriction>
        </xsd:simpleType>
      </xsd:attribute>
    </xsd:extension>
  </xsd:simpleContent>
</xsd:complexType>



回答2:


I'd like to propose my example with more detailed explanation what actually require for mixing up inherited type with restrictions while adding attribute.

this is the place you define your inherited type (in my case it's xs:string based one with field lenght 1024 restriction applied). you can't have this as a standard type for your field as you are going to add an "attribute" to your field too.

  <xs:simpleType name="string1024Type">
    <xs:restriction base="xs:string">
      <xs:maxLength value="1024"/>
    </xs:restriction>
  </xs:simpleType>

this is the place where your element is defined based on your private type (in my case it's "string1024Type") and necessary attribute appended:

<xs:element maxOccurs="unbounded" name="event">
  <xs:complexType>
    <xs:simpleContent>
      <xs:extension base="string1024Type">
        <xs:attribute default="list" name="node" type="xs:string"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
</xs:element>

both blocks could be in completely separate places of your schema. the important point is only following once again - you can't mix inheritance and attributing in the same element definition.



来源:https://stackoverflow.com/questions/626319/add-attributes-to-a-simpletype-or-restriction-to-a-complextype-in-xml-schema

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!