« テスト | Main | テストです。 »

19 March 2008

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 の和訳ってなにげに少ないんですね。。。


参考

TrackBack

TrackBack URL for this entry:
http://bb.lekumo.jp/t/trackback/517129/31023107

Listed below are links to weblogs that reference xs:choice:

Comments

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!

It's great that you are getting ideas from this piece of writing as well as from our argument made here.

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.

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!

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

Post a comment

Access Ranking

Powered by Six Apart
Sponsored links