在 Xml 架构中向简单类型添加属性或向复杂类型添加限制

发布于 2024-07-14 07:57:22 字数 903 浏览 6 评论 0原文

问题如下:

我有以下 XML 片段:

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

问题是我无法同时添加属性和限制。 属性格式只能具有值分钟、小时和秒。 时间有限制模式 \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>

如果我制作一个复杂类型的 timeType,我可以添加一个属性,但不能添加限制,如果我制作一个简单的type,我可以添加限制,但不能添加属性。 有什么办法可以解决这个问题吗? 这并不是一个很奇怪的限制,不是吗?

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 restriction pattern \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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心房敞 2024-07-21 07:57:22

要添加属性,您必须通过扩展派生,要添加方面,您必须通过限制派生。 因此,对于元素的子内容,必须分两步完成。 该属性可以内联定义:

<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>

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>
夜巴黎 2024-07-21 07:57:22

我想提出我的示例,并更详细地解释在添加属性时将继承类型与限制混合实际需要什么。

这是您定义继承类型的地方(在我的例子中,它是基于 xs:string 的类型,并应用了字段长度 1024 限制)。 您不能将其作为字段的标准类型,因为您也要向字段添加“属性”。

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

这是根据您的私有类型(在我的例子中为“string1024Type”)和附加的必要属性定义元素的位置:

<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>

两个块可以位于模式的完全不同的位置。 重要的一点只是再次强调 - 您不能在同一元素定义中混合继承和属性。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文