Tecnologías XML / XML Schema / Control de espacios de nombres / Esquemas para XML, XML Base y XLink

(Extracto de «XML Schema» de Eric van der Vlist)

XML Base, XLink y XML 1.0 definen vocabularios compuestos solo de atributos. Atributos como xml:base, xlink:href o xml:lang funcionan con elementos de cualquier vocabulario, transmitiéndoles significado, a veces también a sus descendientes.

Atributos XML

Permitiremos atributos XML, utilizando xml:lang para permitir descripciones en varios idiomas e indicar cada uno:

<?xml version="1.0"?>
<library xmlns="http://dyomedea.com/ns/library">
    <book id="b3810518883">
        <title>
            Auf den Hund gekommen
        </title>
        <description xml:lang="en">
            Its title says it all !
        </description>
        <description xml:lang="de">
            Der Titel sagt alles!!
        </description>
    </book>
</library>

Según la recomendación «Espacios de nombres en XML 1.0», W3C XML Schema considera xml:lang, xml:space, de XML 1.0, y xml:base, de la especificación XML Base, como pertenecientes al espacio de nombres de XML 1.0 identificado por la referencia URI "http://dyomedea.com/ns/library".

El espacio XML 1.0 es excepcional: no necesita declararse en las instancias. Su prefijo «xml» está reservado y tiene significado incluso para analizadores sin espacios de nombres. Otro prefijo sería posible en teoría, pero crearía confusión y debe desaconsejarse.

Declare el espacio XML, importe un esquema y referencie sus atributos, como en nuestro espacio people:

<?xml version="1.0"?>
<xs:schema targetNamespace="http://dyomedea.com/ns/library" elementFormDefault="qualified" attributeFormDefault="unqualified" xmlns:lib="http://dyomedea.com/ns/library" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:import namespace="http://www.w3.org/XML/1998/namespace" schemaLocation="xml.xsd"/>
    <xs:element name="description">
        <xs:complexType>
            <xs:simpleContent>
                <xs:restriction base="xs:string">
                    <xs:attribute ref="xml:lang"/>
                </xs:restriction>
            </xs:simpleContent>
        </xs:complexType>
    </xs:element>
    <xs:element name="library">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="lib:book"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="book">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="title" type="xs:string"/>
                <xs:element ref="lib:description" maxOccurs="unbounded"/>
            </xs:sequence>
            <xs:attribute name="id" type="xs:ID" use="required"/>
        </xs:complexType>
    </xs:element>
</xs:schema>

Aquí importamos un esquema local (xml.xsd) para XML. El W3C define su propio esquema para los atributos xml:lang, xml:space y xml:base.

<?xml version='1.0'?>
<!DOCTYPE xs:schema PUBLIC "-//W3C//DTD XMLSCHEMA 200102//EN" "XMLSchema.dtd">
<xs:schema targetNamespace="http://www.w3.org/XML/1998/namespace" xmlns:xs="http://www.w3.org/2001/XMLSchema" xml:lang="en">
    <xs:annotation>
        <xs:documentation>See http://www.w3.org/XML/1998/namespace.html and http://www.w3.org/TR/REC-xml for information about this namespace.</xs:documentation>
    </xs:annotation>
    <xs:annotation>
        <xs:documentation>This schema defines attributes and an attribute group
            suitable for use by
            schemas wishing to allow xml:base, xml:lang or xml:space attributes
            on elements they define.
            To enable this, such a schema must import this schema
            for the XML namespace, e.g. as follows:
            &lt;schema . . .>
            . . .
            &lt;import namespace="http://www.w3.org/XML/1998/namespace"
            schemaLocation="http://www.w3.org/2001/03/xml.xsd"/>
            Subsequently, qualified reference to any of the attributes
            or the group defined below will have the desired effect, e.g.
            &lt;type . . .>
            . . .
            &lt;attributeGroup ref="xml:specialAttrs"/>
            will define a type which will schema-validate an instance
            element with any of those attributes</xs:documentation>
    </xs:annotation>
    <xs:annotation>
        <xs:documentation>In keeping with the XML Schema WG's standard versioning
            policy, this schema document will persist at
            http://www.w3.org/2001/03/xml.xsd.
            At the date of issue it can also be found at
            http://www.w3.org/2001/xml.xsd.
            The schema document at that URI may however change in the future,
            in order to remain compatible with the latest version of XML Schema
            itself. In other words, if the XML Schema namespace changes, the version
            of this document at
            http://www.w3.org/2001/xml.xsd will change
            accordingly; the version at
            http://www.w3.org/2001/03/xml.xsd will not change.
        </xs:documentation>
    </xs:annotation>
    <xs:attribute name="lang" type="xs:language">
        <xs:annotation>
            <xs:documentation>In due course, we should install the relevant ISO 2- and 3-letter codes as the enumerated possible values . . .</xs:documentation>
        </xs:annotation>
    </xs:attribute>
    <xs:attribute name="space" default="preserve">
        <xs:simpleType>
            <xs:restriction base="xs:NCName">
                <xs:enumeration value="default"/>
                <xs:enumeration value="preserve"/>
            </xs:restriction>
        </xs:simpleType>
    </xs:attribute>
    <xs:attribute name="base" type="xs:anyURI">
        <xs:annotation>
            <xs:documentation>See http://www.w3.org/TR/xmlbase/ for information about this attribute.</xs:documentation>
        </xs:annotation>
    </xs:attribute>
    <xs:attributeGroup name="specialAttrs">
        <xs:attribute ref="xml:base"/>
        <xs:attribute ref="xml:lang"/>
        <xs:attribute ref="xml:space"/>
    </xs:attributeGroup>
</xs:schema> 

Elegir el esquema original del sitio del W3C o una copia local es controvertido. El acceso remoto garantiza la versión más reciente, pero puede desincronizar esquema y aplicaciones. Un servidor no disponible puede impedir el procesamiento y el acceso remoto puede permitir sustituciones maliciosas de esquemas.

Atributos XLink

Para ilustrar atributos XLink, sustituya la definición del autor bajo el elemento authors por una referencia a un autor descrito como person en otro documento XML:

<?xml version="1.0"?>
<library xmlns="http://dyomedea.com/ns/library" xmlns:xlink="http://www.w3.org/1999/xlink">
    <book id="b3810518883">
        <title>
            Auf den Hund gekommen
        </title>
        <authors>
            <person xlink:href="authors.xml#CMS"/>
        </authors>
    </book>
</library>   

De nuevo, declare el espacio XLink e importe un esquema antes de utilizar sus atributos:

<?xml version="1.0"?>
<xs:schema targetNamespace="http://dyomedea.com/ns/library" elementFormDefault="qualified" attributeFormDefault="unqualified" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:lib="http://dyomedea.com/ns/library">
    <xs:import namespace="http://www.w3.org/1999/xlink" schemaLocation="xlink.xsd"/>
    <xs:element name="person">
        <xs:complexType>
            <xs:attribute ref="xlink:href" use="required"/>
            <xs:attribute ref="xlink:type" fixed="simple"/>
            <xs:attribute ref="xlink:show" fixed="embed"/>
            <xs:attribute ref="xlink:actuate" fixed="onLoad"/>
        </xs:complexType>
    </xs:element>
    <xs:element name="library">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="lib:book"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="book">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="title" type="xs:string"/>
                <xs:element ref="lib:authors"/>
            </xs:sequence>
            <xs:attribute name="id" type="xs:ID" use="required"/>
        </xs:complexType>
    </xs:element>
    <xs:element name="authors">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="lib:person"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

Siguiendo la especificación XLink, fijamos los atributos adicionales (type, show y actuate). Es válido, pero estos valores solo están disponibles para aplicaciones que utilizan un infoset posterior a la validación del esquema (PSVI) según W3C XML Schema.

XLink no proporciona un esquema, pero las partes necesarias son fáciles de crear:

<?xml version="1.0"?>
<xs:schema targetNamespace="http://www.w3.org/1999/xlink" elementFormDefault="qualified" attributeFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xlink="http://www.w3.org/1999/xlink">
    <xs:attribute name="type">
        <xs:simpleType>
            <xs:restriction base="xs:NMTOKEN">
                <xs:enumeration value="simple"/>
                <xs:enumeration value="extended"/>
                <xs:enumeration value="locator"/>
                <xs:enumeration value="arc"/>
                <xs:enumeration value="resource"/>
                <xs:enumeration value="title"/>
            </xs:restriction>
        </xs:simpleType>
    </xs:attribute>
    <xs:attribute name="href" type="xs:anyURI"/>
    <xs:attribute name="role" type="xs:anyURI"/>
    <xs:attribute name="arcrole" type="xs:anyURI"/>
    <xs:attribute name="title" type="xs:string"/>
    <xs:attribute name="show">
        <xs:simpleType>
            <xs:restriction base="xs:NMTOKEN">
                <xs:enumeration value="new"/>
                <xs:enumeration value="replace"/>
                <xs:enumeration value="embed"/>
                <xs:enumeration value="other"/>
                <xs:enumeration value="none"/>
            </xs:restriction>
        </xs:simpleType>
    </xs:attribute>
    <xs:attribute name="label" type="xs:NMTOKEN"/>
    <xs:attribute name="actuate">
        <xs:simpleType>
            <xs:restriction base="xs:NMTOKEN">
                <xs:enumeration value="onLoad"/>
                <xs:enumeration value="onRequest"/>
                <xs:enumeration value="other"/>
                <xs:enumeration value="none"/>
            </xs:restriction>
        </xs:simpleType>
    </xs:attribute>
    <xs:attribute name="from" type="xs:NMTOKEN"/>
    <xs:attribute name="to" type="xs:NMTOKEN"/>
</xs:schema>

   

<< atrás

siguiente >>

 

 

 

volver arriba