使用 XML Schema 依赖于其他元素的值的元素的最大出现次数 [英] Max occurs of element which depend on value of other element using XML Schema

查看:28
本文介绍了使用 XML Schema 依赖于其他元素的值的元素的最大出现次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要有关 XML 架构的帮助.我想写一些条件,其中元素的最大值取决于其他元素的值

I would need help with XML Schema. I would like to write some condition where max occurs of element depend on value of other element

我的 XML:

<databaza>
<dvd>
    <id>01</id>
    <type>DVD-R</type>
    <number_of_movies>2</number_of_movies>
    <movie>
        <movie_id>1</movie_id>
        <movie_name>X-man</movie_name>
        <number_of_characters>2</number_of_characters>
        <character>
            <character_id>1</character_id>
            <character_name>Andy Dufresne</character_name>
            <main_character>YES</main_character>
            <performer>Tim Robbins</performer>      
        </character>
        <character>
            <character_id>2</character_id>
            <character_name>Rede</character_name>
            <main_character>YES</main_character>
            <performer>Morgan Freeman</performer>       
        </character>
    </movie>
    <movie>
        <movie_id>2</movie_id>
        <movie_name>Forrest Gump</movie_name>
        <number_of_characters>4</number_of_characters>
        <character>
            <character_id>1</character_id>
            <character_name>Forrest Gump</character_name>
            <main_character>YES</main_character>
            <performer>Tom Hanks</performer>        
        </character>
        <character>
            <character_id>2</character_id>
            <character_name>Jenny Curran</character_name>
            <main_character>YES</main_character>
            <performer>Robin Wright</performer>     
        </character>
        <character>
            <character_id>3</character_id>
            <character_name>Bubba</character_name>
            <main_character>YES</main_character>
            <performer>Mykelti Williamson</performer>       
        </character>
        <character>
            <character_id>4</character_id>
            <character_name>Dan Taylor</character_name>
            <main_character>YES</main_character>
            <performer>Gary Sinise</performer>      
        </character>
    </movie>
</dvd>
</databaza>

我需要一些条件,说明 的数量取决于 ,然后 的数量取决于 取决于

I need some condition which say that the number of <movie> depends on <number_of_movies> and then that the number of <character> depends on <number_of_characters>

我该如何编写条件,说明 <character_id> 从 1 到最大 以及 < 的相同条件;movie_id>?

And how can I write the condition which say that the <character_id> going from 1 to max <number_of_characters> and the same condition for <movie_id>?

推荐答案

主要建议: 不要明确跟踪电影数量或角色数量,这样您就不必保留数量与相关元素的计数一致.让它含蓄.如果需要,XML 的使用者可以导出计数,如果不需要,则无需设置.

Primary recommendation: Do not explicitly track the number of movies or number of characters, then you'll not have to keep the number consistent with the count of the associated element. Let it be implicit. Consumers of the XML can derive the count if needed and won't be burdened with setting it if not needed.

如果出于某种原因,您绝对必须有一个明确的计数,那么请意识到您不能在 XSD 1.0 中强制执行这样的约束.您将需要 XSD 1.1 中的断言:

If for some reason you absolutely must have an explicit count, then realize that you cannot enforce such a constraint in XSD 1.0. You'll need assertions in XSD 1.1:

这里是如何让 XSD 强制 movie 计数等于 number_of_movies 元素给定的值:

Here is how to have the XSD enforce that the movie count equal the value given by the number_of_movies element:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="unqualified" 
  elementFormDefault="qualified" 
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
  vc:minVersion="1.1">

  <xs:element name="dvd">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="number_of_movies" type="xs:integer"/>
        <xs:element name="movie" maxOccurs="unbounded" />
      </xs:sequence>
      <xs:assert test="count(movie) = number_of_movies"/>
    </xs:complexType>
  </xs:element>
 </xs:schema>

那么这个 XML 将是有效的,

Then this XML will be valid,

<?xml version="1.0" encoding="UTF-8"?>
<dvd>
    <number_of_movies>1</number_of_movies>
    <movie/>
</dvd>

并且此 XML 将无效,

and this XML will be invalid,

<?xml version="1.0" encoding="UTF-8"?>
<dvd>
    <number_of_movies>1</number_of_movies>
    <movie/>
    <movie/>
</dvd>

根据要求.

这篇关于使用 XML Schema 依赖于其他元素的值的元素的最大出现次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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