简体   繁体   English

XML Schema:具有属性的元素和具有限制的文本

[英]XML Schema: element with attributes and text with restrictions

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 我是XML Schema的初学者,我正在尝试解决(在我看来)相当简单的问题:我想匹配表单中的标记

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

ie a tag with both text and and attribute. 即具有文本和属性的标签。 Basically, I know how this can be done with the extension facility. 基本上,我知道如何使用extension工具完成此操作。 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. 当我使用扩展时,似乎我不能对文本使用限制。

Use <xs:restriction> instead of extension. 使用<xs:restriction>而不是扩展名。 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: 但是现在我很清醒,甚至在那个州,这是我设法提出的最好的:

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

Oh my gawd. 哦,我的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 attribute value to an integer in range [0, 10], inclusive. 以上将元素option的内容限制为最大长度为10的字符串,属性value [0,10]范围内的整数,包括端点。

Yeah, that sure isn't too verbose... 是的,那肯定不是太冗长......

I have encountered similar problem, and I'd rather define 2 simpleTypes. 我遇到过类似的问题,我宁愿定义2个simpleTypes。 One for the attribute, the other for the content. 一个用于属性,另一个用于内容。

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:simpleType name="restrictedLengthString">
        <xs:restriction base="xs:string">
            <xs:maxLength value="10"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="boundedInteger">
        <xs:restriction base="xs:integer">
            <xs:minInclusive value="0" />
            <xs:maxInclusive value="10" />
        </xs:restriction>
    </xs:simpleType>

    <xs:element name="foo">
        <xs:complexType>
            <xs:simpleContent> 
                <xs:extension base="restrictedLengthString">
                    <xs:attribute name="bar" type="boundedInteger"/>
                </xs:extension>
            </xs:simpleContent>
        </xs:complexType>
    </xs:element>

</xs:schema>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM