代替グループ

XML Schema Part 0: Primer:

XML Schema には特定の要素を他の要素に差し替える代替グループという機構を持つ.具体的には,ヘッド要素と呼ばれる特定の要素に代替可能な特別な要素グループに他の要素を割り当てることができる.(ただし,ヘッド要素と代替可能な要素はグローバル要素に限る)

以下に代替グループの例を示します:

<complexType name="GH6Usage">
  <sequence>
    <element ref="ipo:gh6head"/>
    <element name="gh6head2" type="string" />
    <element name="city"   type="string"/>
  </sequence>
</complexType>

<element name="gh6head" type="string" abstract="true"/>
<element name="gh6sub1" type="string" substitutionGroup="ipo:gh6head" />
<element name="gh6sub2" type="string" substitutionGroup="ipo:gh6head" />
<element name="gh6head2" type="string" abstract="true"/>
<element name="gh6sub3" type="string" substitutionGroup="ipo:gh6head2" />
<element name="gh6sub4" type="string" substitutionGroup="ipo:gh6head2" />

ここには ipo:gh6headipo:gh6head2 という二つの代替グループがありますが,GH6Usage で使われているのは ipo:gh6head のみです.二つ目の要素 gh6head2ipo:gh6head2 と同じ名前が付けられていますが,GH6Usage のローカル要素です.以下が scalaxb が生成するコードです:

case class GH6Usage(gh6head: rt.DataRecord[Any],
  gh6head2: String,
  city: String)
 
object GH6Usage extends rt.ElemNameParser[GH6Usage] {
  val targetNamespace: Option[String] = Some("http://www.example.com/IPO")
 
  def parser(node: scala.xml.Node): Parser[GH6Usage] =
    (((rt.ElemName(targetNamespace, "gh6sub2")) ^^ 
      (x => rt.DataRecord(x.namespace, Some(x.name), x.node.text))) | 
    ((rt.ElemName(targetNamespace, "gh6sub1")) ^^ 
      (x => rt.DataRecord(x.namespace, Some(x.name), x.node.text)))) ~ 
      (rt.ElemName(targetNamespace, "gh6head2")) ~ 
      (rt.ElemName(targetNamespace, "city")) ^^
        { case p1 ~ 
      p2 ~ 
      p3 => GH6Usage(p1,
      p2.text,
      p3.text) }
 
  def toXML(__obj: GH6Usage, __namespace: Option[String], __elementLabel: Option[String]): scala.xml.NodeSeq = {
    var __scope: scala.xml.NamespaceBinding = scala.xml.TopScope
    __scope = scala.xml.NamespaceBinding("xsi", "http://www.w3.org/2001/XMLSchema-instance", __scope)
    __scope = scala.xml.NamespaceBinding("ipo", "http://www.example.com/IPO", __scope)
    __scope = scala.xml.NamespaceBinding(null, "http://www.example.com/IPO", __scope)
    val node = toXML(__obj, __namespace, __elementLabel, __scope)
    node match {
      case elem: scala.xml.Elem =>
        elem % new scala.xml.PrefixedAttribute(__scope.getPrefix(rt.Helper.XSI_URL),
          "type",
          "ipo:GH6Usage", elem.attributes)
      case _ => node
    }
  }
 
  def toXML(__obj: GH6Usage, __namespace: Option[String], __elementLabel: Option[String], __scope: scala.xml.NamespaceBinding): scala.xml.NodeSeq = {
    var attribute: scala.xml.MetaData  = scala.xml.Null
 
    scala.xml.Elem(rt.Helper.getPrefix(__namespace, __scope).orNull,
      __elementLabel getOrElse { error("missing element label.") },
      attribute, __scope,
      Seq.concat(rt.DataRecord.toXML(__obj.gh6head, targetNamespace, __obj.gh6head.key, __scope),
        scala.xml.Elem(rt.Helper.getPrefix(None, __scope).orNull, "gh6head2", scala.xml.Null, __scope, scala.xml.Text(__obj.gh6head2.toString)),
        scala.xml.Elem(rt.Helper.getPrefix(None, __scope).orNull, "city", scala.xml.Null, __scope, scala.xml.Text(__obj.city.toString))): _*)
  }
 
 
}