Chapter 07: Attribute declarations¶
Example 7-1 Global attribute declarations¶
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://datypic.com/prod" targetNamespace="http://datypic.com/prod">
<xs:attribute name="system" type="xs:string" />
<xs:attribute name="dim" type="xs:integer" />
<xs:complexType name="SizeType">
<xs:attribute ref="system" use="required" />
<xs:attribute ref="dim" />
</xs:complexType>
</xs:schema>
from dataclasses import dataclass, field
from typing import Optional
@dataclass
class SizeType:
"""
:ivar system:
:ivar dim:
"""
system: Optional[str] = field(
default=None,
metadata=dict(
name="system",
type="Attribute",
required=True
)
)
dim: Optional[int] = field(
default=None,
metadata=dict(
name="dim",
type="Attribute"
)
)
Example 7-2 Local attribute declarations¶
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://datypic.com/prod" targetNamespace="http://datypic.com/prod">
<xs:complexType name="SizeType">
<xs:attribute name="system" type="xs:string" use="required" />
<xs:attribute name="dim" type="xs:integer" />
</xs:complexType>
</xs:schema>
from dataclasses import dataclass, field
from typing import Optional
@dataclass
class SizeType:
"""
:ivar system:
:ivar dim:
"""
system: Optional[str] = field(
default=None,
metadata=dict(
name="system",
type="Attribute",
required=True
)
)
dim: Optional[int] = field(
default=None,
metadata=dict(
name="dim",
type="Attribute"
)
)
Example 7-3 Declaring the types of attributes¶
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:attribute name="color" type="xs:string" />
<xs:attribute name="dim" type="xs:integer" />
<xs:attribute name="system">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="US-DRESS" />
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="anything" />
<xs:complexType name="attributes">
<xs:attribute ref="anything" />
<xs:attribute ref="color" />
<xs:attribute ref="dim" />
<xs:attribute ref="system" />
</xs:complexType>
</xs:schema>
from enum import Enum
from dataclasses import dataclass, field
from typing import Optional
class System(Enum):
"""
:cvar US_DRESS:
"""
US_DRESS = "US-DRESS"
@dataclass
class Attributes:
"""
:ivar anything:
:ivar color:
:ivar dim:
:ivar system:
"""
class Meta:
name = "attributes"
anything: Optional[str] = field(
default=None,
metadata=dict(
name="anything",
type="Attribute"
)
)
color: Optional[str] = field(
default=None,
metadata=dict(
name="color",
type="Attribute"
)
)
dim: Optional[int] = field(
default=None,
metadata=dict(
name="dim",
type="Attribute"
)
)
system: Optional[System] = field(
default=None,
metadata=dict(
name="system",
type="Attribute"
)
)
Example 7-4 Qualified and unqualified attribute names¶
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://datypic.com/prod" targetNamespace="http://datypic.com/prod">
<xs:attribute name="global" type="xs:string" />
<xs:element name="size" type="SizeType" />
<xs:complexType name="SizeType">
<xs:attribute ref="global" />
<xs:attribute name="unqual" form="unqualified" />
<xs:attribute name="qual" form="qualified" />
<xs:attribute name="unspec" />
</xs:complexType>
</xs:schema>
from dataclasses import dataclass, field
from typing import Optional
@dataclass
class SizeType:
"""
:ivar global_value:
:ivar unqual:
:ivar qual:
:ivar unspec:
"""
global_value: Optional[str] = field(
default=None,
metadata=dict(
name="global",
type="Attribute"
)
)
unqual: Optional[str] = field(
default=None,
metadata=dict(
name="unqual",
type="Attribute"
)
)
qual: Optional[str] = field(
default=None,
metadata=dict(
name="qual",
type="Attribute",
namespace="http://datypic.com/prod"
)
)
unspec: Optional[str] = field(
default=None,
metadata=dict(
name="unspec",
type="Attribute"
)
)
@dataclass
class Size(SizeType):
class Meta:
name = "size"
namespace = "http://datypic.com/prod"
Example 7-5 Declaring a default value for an attribute¶
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="size">
<xs:complexType>
<xs:attribute name="dim" type="xs:integer" default="1" />
</xs:complexType>
</xs:element>
</xs:schema>
from dataclasses import dataclass, field
@dataclass
class Size:
"""
:ivar dim:
"""
class Meta:
name = "size"
dim: int = field(
default=1,
metadata=dict(
name="dim",
type="Attribute"
)
)
Example 7-7 Declaring an inheritable attribute¶
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="chapter" type="ChapterType" />
<xs:complexType name="ChapterType">
<xs:sequence>
<xs:element name="p" type="ParaType" maxOccurs="unbounded" />
</xs:sequence>
<xs:attribute name="language" type="xs:language" inheritable="true" />
</xs:complexType>
<xs:complexType name="ParaType">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="language" type="xs:language" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:schema>
from dataclasses import dataclass, field
from typing import List, Optional
@dataclass
class ParaType:
"""
:ivar value:
:ivar language:
"""
value: Optional[str] = field(
default=None,
metadata=dict(
name="value",
type="Extension"
)
)
language: Optional[str] = field(
default=None,
metadata=dict(
name="language",
type="Attribute"
)
)
@dataclass
class ChapterType:
"""
:ivar p:
:ivar language:
"""
p: List[ParaType] = field(
default_factory=list,
metadata=dict(
name="p",
type="Element",
namespace="",
min_occurs=1,
max_occurs=9223372036854775807
)
)
language: Optional[str] = field(
default=None,
metadata=dict(
name="language",
type="Attribute"
)
)
@dataclass
class Chapter(ChapterType):
class Meta:
name = "chapter"
Samples Source
Definitive XML Schema by Priscilla Walmsley (c) 2012 Prentice Hall PTR