Consider the following schema:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="Root">
<xs:sequence>
<xs:element ref="Child" />
<xs:element name="Child2" type="Child" />
</xs:sequence>
<xs:attribute ref="Att" />
<xs:attribute name="Att2" type="Att" />
</xs:complexType>
<xs:complexType name="Child">
<xs:attribute ref="Att" />
</xs:complexType>
<xs:attribute name="Att" type="xs:integer" />
</xs:schema>
The ref
to "Child" on line 6 fails, while the type
on line 7 validates. For the attribute, the ref
succeeds while the type
fails. I'm trying to understand why.
My understanding of ref
was that it simply referred to another element and specified that you expect to see an instance of the referred type (with the name given in the definition) at that location. Obviously I'm wrong, so what does ref
actually mean?
ref. Optional. Refers to the name of another element. The ref attribute can include a namespace prefix. This attribute cannot be used if the parent element is the schema element.
XML Schema is commonly known as XML Schema Definition (XSD). It is used to describe and validate the structure and the content of XML data. XML schema defines the elements, attributes and data types. Schema element supports Namespaces.
Reference schema document (REF): A schema document that is intended to provide the authoritative definitions of broadly reusable schema components.
Reference the XSD schema in the XML document using XML schema instance attributes such as either xsi:schemaLocation or xsi:noNamespaceSchemaLocation. Add the XSD schema file to a schema cache and then connect that cache to the DOM document or SAX reader, prior to loading or parsing the XML document.
Using ref=".." you are "pasting" existing element/attribute defined on the other place. Using type=".." you are assigning some structure (defined in complextype/simpletype) to new element/attribute. Look at following:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tst="test" targetNamespace="test">
<xs:complexType name="Root">
<xs:sequence>
<xs:element ref="tst:Child" />
<xs:element name="Child2" type="tst:ChildType" />
</xs:sequence>
<xs:attribute ref="tst:AttRef" />
<xs:attribute name="Att2" type="tst:AttType" />
</xs:complexType>
<xs:complexType name="ChildType">
<xs:attribute ref="tst:AttRef" />
</xs:complexType>
<xs:element name="Child">
</xs:element>
<xs:simpleType name="AttType">
<xs:restriction base="xs:string">
<xs:maxLength value="10" />
</xs:restriction>
</xs:simpleType>
<xs:attribute name="AttRef" type="xs:integer" />
</xs:schema>
Within a content model, the xs:element
element may be either:
(The same name/ref alternation applies to attribute declarations and attribute references, and there is a similar dichotomy between inline type definitions and references to named types.)
In your example, there is no top-level declaration for an element named Child, so the ref
attribute fails. I hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With