XML 模式:具有属性的元素和具有限制的文本
我是 XML Schema 的初学者,我正在尝试解决一个(在我看来)相当简单的问题:我想匹配表单中的标签,
<foo bar="123">some text</foo>
即具有文本和属性的标签。基本上,我知道如何使用扩展
工具来完成此操作。这看起来相当不直观,但却有效。这是基本的习惯用法:
<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>
但是,我也想对文本和属性施加限制!文本不应超过一定长度,属性应为一定范围内的整数。我怎样才能做到这一点?当我使用扩展时,似乎无法对文本使用限制。
I'm a beginner with XML Schema and I'm trying to solve a (in my opinion) rather simple problem: I want to match a tag in the form
<foo bar="123">some text</foo>
i.e. a tag with both text and and attribute. Basically, I know how this can be done with the extension
facility. It seems rather unintuitive, but works. This is the basic idiom:
<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>
However, I also want to impose restrictions on text and attribute! The text shouldn't exceed a certain length and the attribute should be in an integer in a certain range. How can I achieve that? It seems that I cannot use restrictions for the text when I use an extension.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
而不是扩展名。您可能需要单独声明简单类型并在其他构造中引用它们。编辑:抱歉占用我的时间。昨天去参加了一些活动,一如既往,事实证明,在我国的交通中你无法到达任何地方,而且我迟到了,只是尖叫着咒骂着回头。我整个晚上都喝醉了。
但现在我清醒了,即使在那种状态下,这也是我能想到的最好的办法:
天哪。所以显然你应该对延期进行限制。上面的代码将元素
option
的内容限制为最大长度为 10 的字符串,并将属性value
限制为 [0, 10](含)范围内的整数。是的,这确实不是太冗长......
Use
<xs:restriction>
instead of extension. You may want to declare the simple types separately and refer to them in other constructs.EDIT: apologies for taking my time. Went to some event yesterday, as always it turned out you can't get anywhere in my country's traffic and I arrived late to the point of simply turning back screaming and cursing. I spent the evening getting drunk instead.
But now I'm sober and even in that state this is the best I managed to come up with:
Oh my gawd. So apparently you're supposed to make a restriction of an extension. The above will restrict element
option
's content to a string of max length 10 and attributevalue
to an integer in range [0, 10], inclusive.Yeah, that sure isn't too verbose...
我遇到过类似的问题,我宁愿定义2个simpleTypes。一个用于属性,另一个用于内容。
I have encountered similar problem, and I'd rather define 2 simpleTypes. One for the attribute, the other for the content.