gpt4 book ai didi

XML 架构 : element with attributes and text with restrictions

转载 作者:数据小太阳 更新时间:2023-10-29 02:37:12 24 4
gpt4 key购买 nike

我是 XML Schema 的初学者,我正在尝试解决一个(在我看来)相当简单的问题:我想匹配表单中的标签

<foo bar="123">some text</foo>

即具有文本和属性的标签。基本上,我知道如何使用 extension 工具完成此操作。这似乎相当不直观,但有效。这是基本的习语:

<xs:element name="option">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="value" type="xs:string">
</xs:attribute>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>

不过,我也想对text和attribute进行限制!文本不应超过一定长度,属性应为一定范围内的整数。我怎样才能做到这一点?貌似我在使用扩展时不能对文本使用限制。

最佳答案

使用 <xs:restriction>而不是扩展。您可能希望单独声明简单类型并在其他构造中引用它们。

编辑:抱歉占用我的时间。昨天去参加了一些事件,结果一如既往地在我国的交通中你无法到达任何地方,我迟到了以至于只是尖叫和诅咒地转身。我整个晚上都喝醉了。

但现在我很清醒,即使在那种状态下,这也是我设法想出的最好办法:

<xs:element name="option">
<xs:complexType>
<xs:simpleContent>
<xs:restriction base="optionType">
<xs:maxLength value="10" />
</xs:restriction>
</xs:simpleContent>
</xs:complexType>
</xs:element>

<xs:complexType name="optionType">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="value">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="0" />
<xs:maxInclusive value="10" />
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:extension>
</xs:simpleContent>
</xs:complexType>

我的天哪。所以显然你应该限制扩展。以上将限制元素 option的内容为最大长度为 10 且属性为 value 的字符串为 [0, 10] 范围内的整数。

是的,这肯定不会太冗长......

关于XML 架构 : element with attributes and text with restrictions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7918999/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com