substitution group
XML Schema provides a mechanism, called substitution groups, that allows elements to be substituted for other elements. More specifically, elements can be assigned to a special group of elements that are said to be substitutable for a particular named element called the head element. (Note that the head element as well as the substitutable elementsmust be declared as global elements.)
The following demonstrates substitution group:
<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" />
There are two substitution groups ipo:gh6head
and ipo:gh6head2
, but only ipo:gh6head
is used in GH6Usage
. The second element gh6head2
is named the same as ipo:gh6head2
, but it is a local element of GH6Usage
. Here's what scalaxb generates:
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))): _*) } }