xs:choice
今、XML Schema について少し学んでいるのですが、xs:choice について疑問があって調べました。
xs:choice というのは以下の例の場合、foo, bar, baz のいずれか一つを選択しなければなりません。
<xs:choice> <xs:element name="foo" /> <xs:element name="bar" /> <xs:element name="baz" /></xs:choice>
xs:choice には 2 つの属性があります。
- minOccurs (デフォルトは1)
- maxOccurs (デフォルトは1)
なので、<xs:choice minOccurs="1" maxOccurs="1"> ということになるわけです。でも省略されているから、<xs:choice> は必ず一つを選択するということです。
で、<xs:choice minOccurs="0"> とした場合は 1 つ含まれるかもしれないということになるので、選択してもいいし選択しなくてもよくなります。(具体的にはmaxOccurs="1"なので、0か1ということです。)
<xs:choice minOccurs="0"> <xs:element name="foo" /> <xs:element name="bar" /> <xs:element name="baz" /></xs:choice>
foo, bar, baz のうちの1つあるかないかってことですね。
で、当初悩んでいたのが、次の例です。
<xs:choice minOccurs="0" maxOccurs="unbounded"> <xs:element name="foo" /> <xs:element name="bar" /> <xs:element name="baz" /></xs:choice>
unbounded は無制限の意味になります。ではこのときfoo, bar, baz はいくつ選ぶことができるか?
foo, bar, baz の 3 つでも OK ですし、それこそ foo, foo, bar, bar, bar, baz でも OK らしいです。もう choice の意味がないんじゃないか?と思う程。むしろ、xs:choice をやめて以下のようにしてもいいんじゃないか?と思ったり。(おそらく xs:choice にする意味がどこかにあるからこそ、xs:choice を使っているのだと思いますが。。。)
<xs:element name="foo" minOccurs="0" maxOccurs="unbounded" /> <xs:element name="bar" minOccurs="0" maxOccurs="unbounded" /> <xs:element name="baz" minOccurs="0" maxOccurs="unbounded" />
ま、私は今のところ XML Schema を書くのではなく読む側なので、<xs:choice minOccurs="0" maxOccurs="unbounded"> の意味さえ分かればそれで OK なのですが、それにしても XML Schema は自由すぎて不自由な感じがしました。
ちなみに RelaxNG で書くと以下になります。
( foo | bar | baz )*
ところで、W3C の XML Schema の和訳ってなにげに少ないんですね。。。
参考
Spot on with this write-up, I really feel this web site needs much more attention. I'll probably be back again to read through more, thanks for the info!
Posted by: Humberto | 09 June 2014 at 08:42 AM
It's great that you are getting ideas from this piece of writing as well as from our argument made here.
Posted by: genuine leather hidden magnetic | 14 November 2014 at 04:12 AM
I constantly emailed this blog post page to all my associates, for the reason that if like to read it after that my contacts will too.
Posted by: precision stock without winco | 30 November 2014 at 05:08 AM
Hey there! I could have sworn I've been to this website before but after checking through some of the post I realized it's new to me. Nonetheless, I'm definitely happy I found it and I'll be book-marking and checking back often!
Posted by: mens wool aran sweater | 10 December 2014 at 03:24 AM
Great web site you've got here.. It's hard to find good quality writing like yours nowadays. I honestly appreciate individuals like you! Take care!!
Posted by: ms870 metro pcs | 18 December 2014 at 09:49 PM