XSD <anyAttribute> Element
- Previous Page XSD <any>
- Next Page XSD Element Substitution
Das <anyAttribute>-Element ermöglicht es uns, XML-Dokumente durch Attributen zu erweitern, die nicht im Schema definiert sind!
Das <anyAttribute>-Element
Das <anyAttribute>-Element ermöglicht es uns, XML-Dokumente durch Attributen zu erweitern, die nicht im Schema definiert sind!
Das folgende Beispiel ist ein Auszug aus einer XML-Schema-Datei mit dem Namen "family.xsd". Es zeigt eine Deklaration für das "person"-Element. Durch die Verwendung des <anyAttribute>-Elements können wir dem "person"-Element eine beliebige Anzahl von Attributen hinzufügen:
<xs:element name="person"> <xs:complexType> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence> <xs:anyAttribute/> </xs:complexType> </xs:element>
Jetzt hoffen wir, dass wir die "gender"-Eigenschaft verwenden können, um das "person"-Element zu erweitern. In diesem Fall können wir das so tun, auch wenn der Autor des Schemas nie eine "gender"-Eigenschaft deklariert hat.
Sehen Sie sich diese Schema-Datei an, benannt "attribute.xsd":
<?xml version="1.0" encoding="ISO-8859-1"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.codew3c.com" xmlns="http://www.codew3c.com" elementFormDefault="qualified"> <xs:attribute name="gender"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="male|female"/> </xs:restriction> </xs:simpleType> </xs:attribute> </xs:schema>
Diese XML (benannt "Myfamily.xml") verwendet Bestandteile aus verschiedenen Schemata, "family.xsd" und "attribute.xsd":
<?xml version="1.0" encoding="ISO-8859-1"?> <persons xmlns="http://www.microsoft.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:SchemaLocation="http://www.microsoft.com family.xsd http://www.codew3c.com attribute.xsd"> <person gender="female"> <firstname>Jane</firstname> <lastname>Smith</lastname> </person> <person gender="male"> <firstname>David</firstname> <lastname>Smith</lastname> </person> </persons>
The above XML file is valid because the schema "family.xsd" allows us to add attributes to the "person" element.
Both <any> and <anyAttribute> can be used to create extensible documents! They enable documents to include additional elements that are not declared in the main XML schema.
- Previous Page XSD <any>
- Next Page XSD Element Substitution