-->

Max occurs of element which depend on value of oth

2019-07-28 23:44发布

问题:

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.