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

发布于 2024-12-12 06:16:08 字数 641 浏览 0 评论 0原文

我是 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 技术交流群。

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

发布评论

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

评论(2

灯角 2024-12-19 06:16:08

使用 而不是扩展名。您可能需要单独声明简单类型并在其他构造中引用它们。

编辑:抱歉占用我的时间。昨天去参加了一些活动,一如既往,事实证明,在我国的交通中你无法到达任何地方,而且我迟到了,只是尖叫着咒骂着回头。我整个晚上都喝醉了。

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

<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](含)范围内的整数。

是的,这确实不是太冗长......

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:

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

Yeah, that sure isn't too verbose...

心凉 2024-12-19 06:16:08

我遇到过类似的问题,我宁愿定义2个simpleTypes。一个用于属性,另一个用于内容。

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

I have encountered similar problem, and I'd rather define 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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文