[Xml-compile] xs:choice question

Mark Overmeer solutions at overmeer.net
Thu Sep 12 13:43:55 GMT 2013


* DEIGNAN Ciaran (C.DEIGNAN at rsd.com) [130912 12:40]:
> I'm not sure if this is a XML::Compile question or just proof that
> I need training on XML.
> 
> I have a schema that contains a section like this:
> <xs:element minOccurs="0" name="contents">
>   <xs:complexType>
>     <xs:sequence>
>       <xs:element maxOccurs="unbounded" minOccurs="0" name="component">
>          <xs:complexType>
>            <xs:choice>
>              <xs:element minOccurs="0" name="csRef">
>                <xs:complexType>
>                  <xs:sequence>
>                    <xs:element name="csId" type="csIdType"/>
>                    <xs:element name="contentId" type="xs:string"/>
>                  </xs:sequence>
>                </xs:complexType>
>              </xs:element>
>            </xs:choice>

Am I the only one who doesn't like the Russian Doll?
http://www.oracle.com/technetwork/java/design-patterns-142138.html

> In perl I build a structure like this:
> 
>    contents => {
>      component => [
>        { csRef => { csId => 'fld', contentId => 'docid', },
>          title => 'PerlTest',
>          mimeType => 'text/plain',
>          fileExtension => 'txt',
>          lang => 'fr',
>        }, ], }
> 
> The above fragment will be converted to XML by
> XML::Compile::Schema. However if I don't specify the csRef, it refuses
> to convert to XML, and gives me the error
> 
> error: no match for required block `cho_csRef' at node/contents/component

Have your tried
   print $schema->template(PERL => "$yourns:contents");

> The schema does say   minOccurs="0", so I don't know why I have
> to provide it.

So, you can say:
   component => [
     { cho_csRef => []
       ... 
     }

> However the xs:choice does seem required, so I understand the error.

The choice has to be made, but the list may be empty.  That is
slightly different from <choice minOccurs="0"> in which case you
would not have had the complaint.

> Is the schema just bad ?

The schema is ok.

I hope it works ;-)  Choice with optional choices is aways a bit
confusing.  A <sequence> would have been more logical here.
-- 
Regards,
               MarkOv

------------------------------------------------------------------------
       Mark Overmeer MSc                                MARKOV Solutions
       Mark at Overmeer.net                          solutions at overmeer.net
http://Mark.Overmeer.net                   http://solutions.overmeer.net




More information about the Xml-compile mailing list