在XML模式中定义具有限制的属性 [英] Defining an attribute with restrictions in XML schema

查看:99
本文介绍了在XML模式中定义具有限制的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,这是官方的:我很胖.我已经编程多年了,但是之前从未定义过XML模式,这使我无所适从.尽管看了数十个示例,但我只是无法在元素上定义强制属性的语法正确,而限制条件是该属性必须是1到100之间的数字.

我什至无法得到以下更简单的示例来工作,该示例仅声明该属性必须为正整数.我收到一条错误消息,指出未声明MyType2或它不是简单类型.

Well, it''s official: I''m thick. I have been programming years, but have never had to define an XML schema before and it is driving me up the wall. Despite looking at tens of examples, I just can''t get the syntax right for defining an mandatory attribute on an element with the restriction that the attribute must be a number between 1 and 100.

I can''t even get the following simpler example to work, which simply states the attribute must be a positive integer. I get an error saying that MyType2 is not declared or is not a simple type.

<!-- Define a type to hold a single artwork. -->
    <xs:complexType name="ArtworkType">
        <xs:sequence>
            <xs:element name="title" type="xs:string"/>
        </xs:sequence>
        <xs:attribute name="ID" type="MyType2" use="required"/>
    </xs:complexType>

    <!-- Define attributes. -->
    <xs:attribute name="MyType2" type="xs:positiveInteger"/>



我生气之前请帮忙.

帕特里克



Please help before I go mad.

Kind wishes, Patrick

推荐答案

这应该对您有所帮助
This should help you on your way
<xs:schema id="Alpha" xmlns:xs="#unknown">
    targetNamespace="http://tempuri.org/Alpha.xsd"
    elementFormDefault="qualified"
    xmlns="http://tempuri.org/Alpha.xsd"
    xmlns:mstns="http://tempuri.org/Alpha.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
  <xs:element name="Artwork">
    <xs:complextype>
      <xs:attribute name="ID" type="ArtworkType" />
    </xs:complextype>
  </xs:element>

  <xs:simpletype name="ArtworkType">
    <xs:restriction base="xs:integer">
      <xs:mininclusive value="0" />
      <xs:maxinclusive value="100" />
    </xs:restriction>
  </xs:simpletype>
</xs:schema>


太棒了!谢谢.

-帕特里克
That''s brilliant! Thank you.

- Patrick


这篇关于在XML模式中定义具有限制的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆