XML.orgXML.org
FOCUS AREAS |XML-DEV |XML.org DAILY NEWSLINK |REGISTRY |RESOURCES |ABOUT
OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.

 


Help: OASIS Mailing Lists Help | MarkMail Help

[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index]
Using the 4 Great XML Namespace Attributes

Hi Folks,

This is an extension of the discussion of a couple days ago where we discussed in-scope namespaces. It was noted then that the XML namespace is implicitly declared in every XML instance document. This message discusses how to use the 4 great attributes in the XML namespace. I welcome your comments.  /Roger

--------------------------------
Summary
--------------------------------
HTML has some great attributes--id, class, title--that can be dropped into just about any element. 

The XML namespace has 4 great attributes: xml:lang, xml:id, xml:space, xml:base. XML instance document authors should be able to drop those 4 great attributes into just about any element. The XML namespace is implicitly declared in every instance document, so the 4 great attributes are primed for use. However, to use them you need to design your XML Schema so that every element declaration has this:

<anyAttribute namespace="http://www.w3.org/XML/1998/namespace"/>


--------------------------------
The Great HTML Attributes
--------------------------------
HTML has some great attributes--id, class, and title--that can be dropped into just about every element. Here's an example:

<div id="Mercury">Mercury has a diameter 
       of <span class="diameter">3,032.4 miles</span>.
       It's distance from earth is 
       <span class="distance">57 million miles</span>,
       at the closest point in its orbit.</div>

This is very nice.


--------------------------------
The Great XML Namespace Attributes
--------------------------------
The XML namespace contains 4 great attributes:

1.	The lang attribute: use this on an element to state the language of its content.
2.	The id attribute: use this on an element to uniquely identify it.
3.	The space attribute: use this on an element to state that you want its whitespace nodes preserved during processing.
4.	The base attribute: use this on an element to create a base URL; URL's within the element can be relative to the base URL.

These are attributes that instance document authors should be able to drop into just about every element in their XML document. Here are some examples:

<movie>
	<title>The Laughing Cow</title>
	<title xml:lang="fr">La Vache Qui Rit</title>
</movie>


<Book xml:id="RB">
        <Title>Illusions The Adventures of a Reluctant Messiah</Title>
        <Author>Richard Bach</Author>
        <Date>1977</Date>
        <ISBN>0-440-34319-4</ISBN>
        <Publisher>Dell Publishing Co.</Publisher>
</Book>


<f xml:space="preserve">       </f>


<Books xml:base="http://www.xfront.com/schemas/";>
        <Book xsi:schemaLocation="Book.xsd" ...>
        ...
        <Book xsi:schemaLocation="Book.xsd" ...>
        ...
        <Book xsi:schemaLocation="Book.xsd" ...>
        ...
</Books>

This is very nice.


---------------------------------------------------
Enabling Use of the Great XML Namespace Attributes
---------------------------------------------------
Recall that the XML namespace (and the xml: prefix) is implicitly declared in every XML document. Thus, XML documents are primed for dropping the xml:lang attribute into any element, dropping the xml:id attribute into any element, and so forth. 

While the XML namespace is automatically available in every instance document, the attributes in it cannot be used unless you've explicitly designed your XML Schema to allow their use. Here's what you need to add to each element declaration:

<anyAttribute namespace="http://www.w3.org/XML/1998/namespace"/>

It says that any of the 4 great attributes from the XML namespace can be dropped into the element.

Here's an example where I declare a <BookStore> element and state that any of the 4 great attributes from the XML namespace can be dropped into it:

<element name="BookStore">
     <complexType>
            <sequence>
                ...
            </sequence>
            <anyAttribute namespace="http://www.w3.org/XML/1998/namespace"/>
     </complexType>
</element>

You must do this for every element declaration. Yes, it is tedious. The good news is that in XML Schema 1.1 you will be able to declare "schema-wide attributes." Thus, in one fell swoop you will be able to state that the 4 great XML namespace attributes are available on every element.


--------------------------------
Example
--------------------------------
Create an XML Schema for BookStore. BookStore contains Books, which contain Title, Author, Date, ISBN, and Publisher. Design the schema so that instance document authors can drop into any elements any of the 4 great XML namespace attributes. Here is a sample instance document:

<?xml version="1.0"?>
<BookStore xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
           xml:lang="en">
                           
        <Book xml:id="PM" xml:space="preserve">
                <Title>My Life and Times</Title>
                <Author>Paul McCartney</Author>
                <Date>1998</Date>
                <ISBN>1-56592-235-2</ISBN>
                <Publisher>McMillin Publishing</Publisher>
        </Book>
        
        <Book xml:id="RB">
                <Title>Illusions The Adventures of a Reluctant Messiah</Title>
                <Author>Richard Bach</Author>
                <Date>1977</Date>
                <ISBN>0-440-34319-4</ISBN>
                <Publisher>Dell Publishing Co.</Publisher>
        </Book>
        
        <Book xml:id="JK">
                <Title>The First and Last Freedom</Title>
                <Author>J. Krishnamurti</Author>
                <Date>1954</Date>
                <ISBN>0-06-064831-7</ISBN>
                <Publisher>Harper &amp; Row</Publisher>
        </Book>
        
</BookStore>

Note the use of xml:lang, xml:id, xml:space, and xml:base.

Here is the XML Schema:

<?xml version="1.0"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema";
        targetNamespace="http://www.bookstore.org";
        xmlns:bk="http://www.bookstore.org";
        elementFormDefault="qualified">
    
    <xsd:import namespace="http://www.w3.org/XML/1998/namespace";
                schemaLocation="http://www.w3.org/2001/xml.xsd"/>
    
    <element name="BookStore">
        <complexType>
            <sequence>
                <element name="Book" maxOccurs="unbounded">
                    <complexType>
                        <sequence>
                            <element name="Title" 
                                     type="bk:safe-string-plus-XML-attributes"/>
                            <element name="Author" 
                                     type="bk:safe-string-plus-XML-attributes"/>
                            <element name="Date" 
                                     type="bk:year-plus-XML-attributes"/>
                            <element name="ISBN" 
                                     type="bk:safe-string-plus-XML-attributes"/>
                            <element name="Publisher" 
                                     type="bk:safe-string-plus-XML-attributes"/>
                        </sequence>
                        <anyAttribute namespace="http://www.w3.org/XML/1998/namespace"/>
                    </complexType>
                </element>
            </sequence>
            <anyAttribute namespace="http://www.w3.org/XML/1998/namespace"/>
        </complexType>
    </element>
    
    <simpleType name="safe-string">
        <restriction base="string">
            <maxLength value="100" />
            <pattern value="[&#09;-&#127;]*" /> <!-- ASCII characters -->
        </restriction>
    </simpleType>
    
    <complexType name="safe-string-plus-XML-attributes">
        <simpleContent>
            <extension base="bk:safe-string">
                <anyAttribute namespace="http://www.w3.org/XML/1998/namespace"; />
            </extension>
        </simpleContent>
    </complexType>
    
    <complexType name="year-plus-XML-attributes">
        <simpleContent>
            <extension base="gYear">
                <anyAttribute namespace="http://www.w3.org/XML/1998/namespace"; />
            </extension>
        </simpleContent>
    </complexType>
    
</schema>


[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index]


News | XML in Industry | Calendar | XML Registry
Marketplace | Resources | MyXML.org | Sponsors | Privacy Statement

Copyright 1993-2007 XML.org. This site is hosted by OASIS