Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between ref and type in an XML schema?

Tags:

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?

like image 654
Nigel Hawkins Avatar asked Jul 02 '13 09:07

Nigel Hawkins


People also ask

What does REF mean in XML?

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.

What is XML Schema type?

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.

What is schema reference?

Reference schema document (REF): A schema document that is intended to provide the authoritative definitions of broadly reusable schema components.

How do you reference an XML Schema?

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.


2 Answers

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> 
like image 99
Jirka Š. Avatar answered Oct 08 '22 23:10

Jirka Š.


Within a content model, the xs:element element may be either:

  1. An element declaration (giving the name and type of the element) or
  2. A reference to a top-level element declaration (giving the name of the element as a way of identifying it, and deferring to the actual declaration for the type).

(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.

like image 42
C. M. Sperberg-McQueen Avatar answered Oct 08 '22 23:10

C. M. Sperberg-McQueen