对 xml 模式中的元素同时具有属性和限制 [英] Having both an attribute and a restriction on an element in xml schema

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

问题描述

我正在尝试编写一个 xml 模式来验证这段 xml:

I'm trying to write a xml schema that will validate this piece of xml:

<date isodate="2007-03-14">14 march 2007</date>

属性 isodate 应将其类型设置为 xs:date 并且内容应最多 50 个字符长.

The attribute isodate should have it's type set to xs:date and the content should be max 50 characters long.

我想知道是否可以在一个块中编写 xml 模式定义,可能是这样的:

I wonder if it's possible to write the xml schema definition in one block, something like this maybe:

<xs:element name="date" minOccurs="0" maxOccurs="1">  
  <xs:complexType>  
    <xs:simpleContent>  
      <xs:restriction base="xs:string">  
        <xs:minLength value="0"/>  
        <xs:maxLength value="50"/>  
      </xs:restriction>  
      <xs:attribute name="isodate" type="xs:date" use="required"/>  
    </xs:simpleContent>  
  </xs:complexType>  
</xs:element>

上面的代码不起作用,我真的不明白为什么.我发现的唯一解决方法是将限制部分分解为单独的类型,并像这样链接:

The code above doesn't work, and I can't really figure out why. Only workaround I have found is to break out the restriction part into a separate type, and link that like this:

<xs:simpleType name="reviewDate">  
    <xs:restriction base="xs:string">  
        <xs:minLength value="0"/>  
        <xs:maxLength value="50"/>  
    </xs:restriction>  
</xs:simpleType>

<xs:element name="date" minOccurs="0" maxOccurs="1">  
    <xs:complexType>  
        <xs:simpleContent>  
            <xs:extension base="reviewDate">  
                <xs:attribute name="isodate" type="xs:date" use="required"/>  
            </xs:extension>  
        </xs:simpleContent>  
    </xs:complexType>  
</xs:element>

我的问题是如何在一个块中编写定义,以便架构更具可读性,并且不会引用架构其他部分中的类型.

The question I have is how to write the definition in one block so that the schema is a bit more readable, and doesn't reference types in other parts of the schema.

推荐答案

不能将限制和扩展合并到一个 XSD 块中.您对ReviewDate"简单类型的解决方案是我所知道的最佳解决方案.

You cannot merge both a restriction and an extension into one block of XSD. The solution that you have with the "ReviewDate" simple type is the best solution I know of.

马克

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

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