Max occurs of element which depend on value of other element using XML Schema

◇◆丶佛笑我妖孽 提交于 2019-12-11 13:36:29

问题


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

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

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


回答1:


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.

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 1.1

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>

Then this XML will be valid,

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

and this XML will be invalid,

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

as requested.



来源:https://stackoverflow.com/questions/37642086/max-occurs-of-element-which-depend-on-value-of-other-element-using-xml-schema

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!