Chapter 15

Named groups

Binding Test

Schema

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

  <xsd:annotation>
    <xsd:documentation>
      This example illustrates named model groups and attribute groups.
    </xsd:documentation>
  </xsd:annotation>

  <xsd:element name="shirt" type="ShirtType"/>

  <xsd:complexType name="ShirtType">
    <xsd:sequence>
      <xsd:group ref="ProductPropertyGroup" minOccurs="0"/>
      <xsd:element name="size" type="SizeType"/>
    </xsd:sequence>
    <xsd:attributeGroup ref="IdentifierGroup"/>
    <xsd:attribute name="effDate" type="xsd:date"/>
  </xsd:complexType>

  <xsd:group name="DescriptionGroup">
    <xsd:sequence>
      <xsd:element name="description" type="xsd:string"/>
      <xsd:element name="comment" type="xsd:string" minOccurs="0"/>
    </xsd:sequence>
  </xsd:group>

  <xsd:group name="ProductPropertyGroup">
    <xsd:sequence>
      <xsd:group ref="DescriptionGroup"/>
      <xsd:element name="number" type="xsd:integer"/>
      <xsd:element name="name" type="xsd:string"/>
    </xsd:sequence>
  </xsd:group>

  <xsd:attributeGroup name="IdentifierGroup">
    <xsd:attribute name="id" type="xsd:ID" use="required"/>
    <xsd:attribute name="version" type="xsd:decimal"/>
  </xsd:attributeGroup>

  <xsd:complexType name="SizeType">
    <xsd:simpleContent>
      <xsd:extension base="xsd:integer">
        <xsd:attribute name="system" type="xsd:token"/>
      </xsd:extension>
    </xsd:simpleContent>
  </xsd:complexType>

</xsd:schema>

XML Document

<shirt xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:noNamespaceSchemaLocation="chapter15.xsd"
       id="P557" version="100">
  <description>This is a great shirt.</description>
  <number>557</number>
  <name>Short-Sleeved Linen Blouse</name>
  <size system="US-DRESS">6</size>
</shirt>

xsData XML Document

<shirt id="P557" version="100">
  <description>This is a great shirt.</description>
  <number>557</number>
  <name>Short-Sleeved Linen Blouse</name>
  <size system="US-DRESS">6</size>
</shirt>

xsData JSON

{
    "description": "This is a great shirt.",
    "comment": null,
    "number": 557,
    "name": "Short-Sleeved Linen Blouse",
    "size": {
        "value": 6,
        "system": "US-DRESS"
    },
    "id": "P557",
    "version": "100",
    "eff_date": null
}

Example 15-1 Named model group with local element declarations

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:group name="DescriptionGroup">
    <xs:sequence>
      <xs:element name="description" type="xs:string" />
      <xs:element name="comment" type="xs:string" minOccurs="0" />
    </xs:sequence>
  </xs:group>
  <xs:complexType name="DescriptionGroup">
    <xs:group ref="DescriptionGroup" />
  </xs:complexType>
</xs:schema>
from dataclasses import dataclass, field
from typing import Optional


@dataclass
class DescriptionGroup:
    """
    :ivar description:
    :ivar comment:
    """
    description: Optional[str] = field(
        default=None,
        metadata=dict(
            type="Element",
            namespace="",
            required=True
        )
    )
    comment: Optional[str] = field(
        default=None,
        metadata=dict(
            type="Element",
            namespace=""
        )
    )

Example 15-2 Named model group with element references

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="description" type="xs:string" />
  <xs:element name="comment" type="xs:string" />
  <xs:group name="DescriptionGroup">
    <xs:sequence>
      <xs:element ref="description" />
      <xs:element ref="comment" minOccurs="0" />
    </xs:sequence>
  </xs:group>
  <xs:complexType name="DescriptionGroup">
    <xs:group ref="DescriptionGroup" />
  </xs:complexType>
</xs:schema>
from dataclasses import dataclass, field
from typing import Optional


@dataclass
class Comment:
    """
    :ivar value:
    """
    class Meta:
        name = "comment"

    value: Optional[str] = field(
        default=None,
        metadata=dict(
            required=True
        )
    )


@dataclass
class Description:
    """
    :ivar value:
    """
    class Meta:
        name = "description"

    value: Optional[str] = field(
        default=None,
        metadata=dict(
            required=True
        )
    )


@dataclass
class DescriptionGroup:
    """
    :ivar description:
    :ivar comment:
    """
    description: Optional[Description] = field(
        default=None,
        metadata=dict(
            type="Element",
            required=True
        )
    )
    comment: Optional[Comment] = field(
        default=None,
        metadata=dict(
            type="Element"
        )
    )

Example 15-4 Equivalent content model without a named model group reference

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:complexType name="PurchaseOrderType">
    <xs:sequence>
      <xs:sequence minOccurs="0">
        <xs:element name="description" type="xs:string" />
        <xs:element name="comment" type="xs:string" minOccurs="0" />
      </xs:sequence>
    </xs:sequence>
  </xs:complexType>
</xs:schema>
from dataclasses import dataclass, field
from typing import Optional


@dataclass
class PurchaseOrderType:
    """
    :ivar description:
    :ivar comment:
    """
    description: Optional[str] = field(
        default=None,
        metadata=dict(
            type="Element",
            namespace=""
        )
    )
    comment: Optional[str] = field(
        default=None,
        metadata=dict(
            type="Element",
            namespace=""
        )
    )

Example 15-5 Group reference at the top level of the content model

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:complexType name="DescriptionType">
    <xs:group ref="DescriptionGroup" />
    <xs:attribute name="lang" type="xs:string" />
  </xs:complexType>
  <xs:group name="DescriptionGroup">
    <xs:sequence>
      <xs:element name="description" type="xs:string" />
      <xs:element name="comment" type="xs:string" minOccurs="0" />
    </xs:sequence>
  </xs:group>
</xs:schema>
from dataclasses import dataclass, field
from typing import Optional


@dataclass
class DescriptionType:
    """
    :ivar description:
    :ivar comment:
    :ivar lang:
    """
    description: Optional[str] = field(
        default=None,
        metadata=dict(
            type="Element",
            namespace="",
            required=True
        )
    )
    comment: Optional[str] = field(
        default=None,
        metadata=dict(
            type="Element",
            namespace=""
        )
    )
    lang: Optional[str] = field(
        default=None,
        metadata=dict(
            type="Attribute"
        )
    )

Example 15-6 Group with an all model group

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:group name="DescriptionGroup">
    <xs:all>
      <xs:element name="description" type="xs:string" />
      <xs:element name="comment" type="xs:string" minOccurs="0" />
    </xs:all>
  </xs:group>
  <xs:complexType name="DescriptionGroup">
    <xs:group ref="DescriptionGroup" />
  </xs:complexType>
</xs:schema>
from dataclasses import dataclass, field
from typing import Optional


@dataclass
class DescriptionGroup:
    """
    :ivar description:
    :ivar comment:
    """
    description: Optional[str] = field(
        default=None,
        metadata=dict(
            type="Element",
            namespace="",
            required=True
        )
    )
    comment: Optional[str] = field(
        default=None,
        metadata=dict(
            type="Element",
            namespace=""
        )
    )

Example 15-7 Group reference from a group

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:group name="ProductPropertyGroup">
    <xs:sequence>
      <xs:group ref="DescriptionGroup" />
      <xs:element name="number" type="xs:integer" />
      <xs:element name="name" type="xs:string" />
    </xs:sequence>
  </xs:group>
  <xs:group name="DescriptionGroup">
    <xs:sequence>
      <xs:element name="description" type="xs:string" />
      <xs:element name="comment" type="xs:string" minOccurs="0" />
    </xs:sequence>
  </xs:group>
  <xs:complexType name="DescriptionGroup">
    <xs:group ref="DescriptionGroup" />
  </xs:complexType>
</xs:schema>
from dataclasses import dataclass, field
from typing import Optional


@dataclass
class DescriptionGroup:
    """
    :ivar description:
    :ivar comment:
    """
    description: Optional[str] = field(
        default=None,
        metadata=dict(
            type="Element",
            namespace="",
            required=True
        )
    )
    comment: Optional[str] = field(
        default=None,
        metadata=dict(
            type="Element",
            namespace=""
        )
    )

Example 15-8 Attribute group with local attribute declarations

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:attributeGroup name="IdentifierGroup">
    <xs:attribute name="id" type="xs:ID" use="required" />
    <xs:attribute name="version" type="xs:decimal" />
  </xs:attributeGroup>
  <xs:complexType name="IdentifierGroup">
    <xs:attributeGroup ref="IdentifierGroup" />
  </xs:complexType>
</xs:schema>
from decimal import Decimal
from dataclasses import dataclass, field
from typing import Optional


@dataclass
class IdentifierGroup:
    """
    :ivar id:
    :ivar version:
    """
    id: Optional[str] = field(
        default=None,
        metadata=dict(
            type="Attribute",
            required=True
        )
    )
    version: Optional[Decimal] = field(
        default=None,
        metadata=dict(
            type="Attribute"
        )
    )

Example 15-9 Attribute group with attribute references

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:attribute name="id" type="xs:ID" />
  <xs:attribute name="version" type="xs:decimal" />
  <xs:attributeGroup name="IdentifierGroup">
    <xs:attribute ref="id" use="required" />
    <xs:attribute ref="version" />
  </xs:attributeGroup>
  <xs:complexType name="IdentifierGroup">
    <xs:attributeGroup ref="IdentifierGroup" />
  </xs:complexType>
</xs:schema>
from decimal import Decimal
from dataclasses import dataclass, field
from typing import Optional


@dataclass
class IdentifierGroup:
    """
    :ivar id:
    :ivar version:
    """
    id: Optional[str] = field(
        default=None,
        metadata=dict(
            type="Attribute",
            required=True
        )
    )
    version: Optional[Decimal] = field(
        default=None,
        metadata=dict(
            type="Attribute"
        )
    )

Example 15-10 Attribute group with a wildcard

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:attributeGroup name="IdentifierGroup">
    <xs:attribute name="id" type="xs:ID" use="required" />
    <xs:attribute name="version" type="xs:decimal" />
    <xs:anyAttribute namespace="##other" />
  </xs:attributeGroup>
  <xs:complexType name="IdentifierGroup">
    <xs:attributeGroup ref="IdentifierGroup" />
  </xs:complexType>
</xs:schema>
from decimal import Decimal
from dataclasses import dataclass, field
from lxml.etree import QName
from typing import Dict, Optional


@dataclass
class IdentifierGroup:
    """
    :ivar id:
    :ivar version:
    :ivar other_attributes:
    """
    id: Optional[str] = field(
        default=None,
        metadata=dict(
            type="Attribute",
            required=True
        )
    )
    version: Optional[Decimal] = field(
        default=None,
        metadata=dict(
            type="Attribute"
        )
    )
    other_attributes: Dict[QName, str] = field(
        default_factory=dict,
        metadata=dict(
            type="Attributes",
            namespace="##other"
        )
    )

Example 15-11 Referencing an attribute group from a complex type definition

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:include schemaLocation="example1508.xsd" />
  <xs:complexType name="ProductType">
    <xs:sequence />
    <xs:attributeGroup ref="IdentifierGroup" />
    <xs:attribute name="effDate" type="xs:date" />
  </xs:complexType>
</xs:schema>
from decimal import Decimal
from dataclasses import dataclass, field
from typing import Optional


@dataclass
class ProductType:
    """
    :ivar id:
    :ivar version:
    :ivar eff_date:
    """
    id: Optional[str] = field(
        default=None,
        metadata=dict(
            type="Attribute",
            required=True
        )
    )
    version: Optional[Decimal] = field(
        default=None,
        metadata=dict(
            type="Attribute"
        )
    )
    eff_date: Optional[str] = field(
        default=None,
        metadata=dict(
            name="effDate",
            type="Attribute"
        )
    )

Example 15-12 Equivalent complex type without an attribute group

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:complexType name="ProductType">
    <xs:attribute name="id" type="xs:ID" use="required" />
    <xs:attribute name="version" type="xs:decimal" />
    <xs:attribute name="effDate" type="xs:date" />
  </xs:complexType>
</xs:schema>
from decimal import Decimal
from dataclasses import dataclass, field
from typing import Optional


@dataclass
class ProductType:
    """
    :ivar id:
    :ivar version:
    :ivar eff_date:
    """
    id: Optional[str] = field(
        default=None,
        metadata=dict(
            type="Attribute",
            required=True
        )
    )
    version: Optional[Decimal] = field(
        default=None,
        metadata=dict(
            type="Attribute"
        )
    )
    eff_date: Optional[str] = field(
        default=None,
        metadata=dict(
            name="effDate",
            type="Attribute"
        )
    )

Example 15-14 Attribute group referencing an attribute group

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:attributeGroup name="HeaderGroup">
    <xs:attributeGroup ref="IdentifierGroup" />
  </xs:attributeGroup>
  <xs:attributeGroup name="IdentifierGroup">
    <xs:attribute name="id" type="xs:ID" use="required" />
    <xs:attribute name="version" type="xs:decimal" />
  </xs:attributeGroup>
  <xs:complexType name="HeaderGroup">
    <xs:attributeGroup ref="HeaderGroup" />
  </xs:complexType>
</xs:schema>
from decimal import Decimal
from dataclasses import dataclass, field
from typing import Optional


@dataclass
class HeaderGroup:
    """
    :ivar id:
    :ivar version:
    """
    id: Optional[str] = field(
        default=None,
        metadata=dict(
            type="Attribute",
            required=True
        )
    )
    version: Optional[Decimal] = field(
        default=None,
        metadata=dict(
            type="Attribute"
        )
    )

Example 15-15 Default attribute group

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:prod="http://datypic.com/prod" targetNamespace="http://datypic.com/prod" defaultAttributes="prod:IdentifierGroup">
  <xs:attributeGroup name="IdentifierGroup">
    <xs:attribute name="id" type="xs:ID" use="required" />
    <xs:attribute name="version" type="xs:decimal" />
  </xs:attributeGroup>
  <xs:complexType name="ProductType">
    <xs:sequence />
    <xs:attribute name="dept" type="xs:string" />
  </xs:complexType>
  <xs:complexType name="CatalogType" defaultAttributesApply="false">
    <xs:sequence />
    <xs:attribute name="catalogNumber" type="xs:integer" />
  </xs:complexType>
</xs:schema>
from decimal import Decimal
from dataclasses import dataclass, field
from typing import Optional

__NAMESPACE__ = "http://datypic.com/prod"


@dataclass
class CatalogType:
    """
    :ivar catalog_number:
    """
    catalog_number: Optional[int] = field(
        default=None,
        metadata=dict(
            name="catalogNumber",
            type="Attribute"
        )
    )


@dataclass
class ProductType:
    """
    :ivar id:
    :ivar version:
    :ivar dept:
    """
    id: Optional[str] = field(
        default=None,
        metadata=dict(
            type="Attribute",
            required=True
        )
    )
    version: Optional[Decimal] = field(
        default=None,
        metadata=dict(
            type="Attribute"
        )
    )
    dept: Optional[str] = field(
        default=None,
        metadata=dict(
            type="Attribute"
        )
    )

Example 15-16 Named groups with a target namespace

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" xmlns="http://datypic.com/prod" targetNamespace="http://datypic.com/prod">
  <xs:group name="DescriptionGroup">
    <xs:sequence>
      <xs:element name="description" type="xs:string" />
      <xs:element name="comment" type="xs:string" minOccurs="0" />
    </xs:sequence>
  </xs:group>
  <xs:attributeGroup name="IdentifierGroup">
    <xs:attribute name="id" type="xs:ID" use="required" />
    <xs:attribute name="version" type="xs:decimal" />
    <xs:anyAttribute namespace="##other" />
  </xs:attributeGroup>
  <xs:complexType name="PurchaseOrderType">
    <xs:sequence>
      <xs:group ref="DescriptionGroup" minOccurs="0" />
    </xs:sequence>
    <xs:attributeGroup ref="IdentifierGroup" />
  </xs:complexType>
</xs:schema>
from decimal import Decimal
from dataclasses import dataclass, field
from lxml.etree import QName
from typing import Dict, Optional

__NAMESPACE__ = "http://datypic.com/prod"


@dataclass
class PurchaseOrderType:
    """
    :ivar description:
    :ivar comment:
    :ivar id:
    :ivar version:
    :ivar other_attributes:
    """
    description: Optional[str] = field(
        default=None,
        metadata=dict(
            type="Element",
            namespace="http://datypic.com/prod"
        )
    )
    comment: Optional[str] = field(
        default=None,
        metadata=dict(
            type="Element",
            namespace="http://datypic.com/prod"
        )
    )
    id: Optional[str] = field(
        default=None,
        metadata=dict(
            type="Attribute",
            required=True
        )
    )
    version: Optional[Decimal] = field(
        default=None,
        metadata=dict(
            type="Attribute"
        )
    )
    other_attributes: Dict[QName, str] = field(
        default_factory=dict,
        metadata=dict(
            type="Attributes",
            namespace="##other"
        )
    )

Example 15-17 Named groups across namespaces

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" xmlns="http://datypic.com/ord" xmlns:prod="http://datypic.com/prod" targetNamespace="http://datypic.com/ord">
  <xs:import namespace="http://datypic.com/prod" schemaLocation="example15172.xsd" />
  <xs:complexType name="PurchaseOrderType">
    <xs:sequence>
      <xs:group ref="prod:DescriptionGroup" minOccurs="0" />
    </xs:sequence>
    <xs:attributeGroup ref="prod:IdentifierGroup" />
  </xs:complexType>
</xs:schema>
from decimal import Decimal
from dataclasses import dataclass, field
from lxml.etree import QName
from typing import Dict, Optional

__NAMESPACE__ = "http://datypic.com/ord"


@dataclass
class PurchaseOrderType:
    """
    :ivar description:
    :ivar comment:
    :ivar id:
    :ivar version:
    :ivar other_attributes:
    """
    description: Optional[str] = field(
        default=None,
        metadata=dict(
            type="Element",
            namespace="http://datypic.com/prod"
        )
    )
    comment: Optional[str] = field(
        default=None,
        metadata=dict(
            type="Element",
            namespace="http://datypic.com/prod"
        )
    )
    id: Optional[str] = field(
        default=None,
        metadata=dict(
            type="Attribute",
            required=True
        )
    )
    version: Optional[Decimal] = field(
        default=None,
        metadata=dict(
            type="Attribute"
        )
    )
    other_attributes: Dict[QName, str] = field(
        default_factory=dict,
        metadata=dict(
            type="Attributes",
            namespace="##other"
        )
    )

Example 15-17 Named groups across namespaces

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" xmlns="http://datypic.com/prod" targetNamespace="http://datypic.com/prod">
  <xs:group name="DescriptionGroup">
    <xs:sequence>
      <xs:element name="description" type="xs:string" />
      <xs:element name="comment" type="xs:string" minOccurs="0" />
    </xs:sequence>
  </xs:group>
  <xs:attributeGroup name="IdentifierGroup">
    <xs:attribute name="id" type="xs:ID" use="required" />
    <xs:attribute name="version" type="xs:decimal" />
    <xs:anyAttribute namespace="##other" />
  </xs:attributeGroup>
</xs:schema>
from decimal import Decimal
from dataclasses import dataclass, field
from lxml.etree import QName
from typing import Dict, Optional

__NAMESPACE__ = "http://datypic.com/prod"


@dataclass
class DescriptionGroup:
    """
    :ivar description:
    :ivar comment:
    """
    description: Optional[str] = field(
        default=None,
        metadata=dict(
            type="Element",
            namespace="http://datypic.com/prod",
            required=True
        )
    )
    comment: Optional[str] = field(
        default=None,
        metadata=dict(
            type="Element",
            namespace="http://datypic.com/prod"
        )
    )


@dataclass
class IdentifierGroup:
    """
    :ivar id:
    :ivar version:
    :ivar other_attributes:
    """
    id: Optional[str] = field(
        default=None,
        metadata=dict(
            type="Attribute",
            required=True
        )
    )
    version: Optional[Decimal] = field(
        default=None,
        metadata=dict(
            type="Attribute"
        )
    )
    other_attributes: Dict[QName, str] = field(
        default_factory=dict,
        metadata=dict(
            type="Attributes",
            namespace="##other"
        )
    )

Example 15-18 Reusing content model fragments through derivation

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:include schemaLocation="../chapter16/example1601.xsd" />
  <xs:complexType name="DescribedType">
    <xs:sequence>
      <xs:element name="description" type="xs:string" />
      <xs:element name="comment" type="xs:string" minOccurs="0" />
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="PurchaseOrderType">
    <xs:complexContent>
      <xs:extension base="DescribedType">
        <xs:sequence>
          <xs:element ref="items" />
        </xs:sequence>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
  <xs:complexType name="ItemsType">
    <xs:complexContent>
      <xs:extension base="DescribedType">
        <xs:sequence>
          <xs:element ref="product" maxOccurs="unbounded" />
        </xs:sequence>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
</xs:schema>
from dataclasses import dataclass, field
from typing import List, Optional
from tests.fixtures.defxmlschema.chapter16.example1601 import (
    Items,
    Product,
)


@dataclass
class DescribedType:
    """
    :ivar description:
    :ivar comment:
    """
    description: Optional[str] = field(
        default=None,
        metadata=dict(
            type="Element",
            namespace="",
            required=True
        )
    )
    comment: Optional[str] = field(
        default=None,
        metadata=dict(
            type="Element",
            namespace=""
        )
    )


@dataclass
class ItemsType(DescribedType):
    """
    :ivar product:
    """
    product: List[Product] = field(
        default_factory=list,
        metadata=dict(
            type="Element",
            min_occurs=1,
            max_occurs=9223372036854775807
        )
    )


@dataclass
class PurchaseOrderType(DescribedType):
    """
    :ivar items:
    """
    items: Optional[Items] = field(
        default=None,
        metadata=dict(
            type="Element",
            required=True
        )
    )

Samples Source

Definitive XML Schema by Priscilla Walmsley (c) 2012 Prentice Hall PTR