[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Using the 4 Great XML Namespace Attributes

  • From: "Costello, Roger L." <costello@mitre.org>
  • To: "xml-dev@lists.xml.org" <xml-dev@lists.xml.org>
  • Date: Wed, 1 Sep 2010 14:49:59 -0400

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]


PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
 

Stylus Studio has published XML-DEV in RSS and ATOM formats, enabling users to easily subcribe to the list from their preferred news reader application.


Stylus Studio Sponsored Links are added links designed to provide related and additional information to the visitors of this website. they were not included by the author in the initial post. To view the content without the Sponsor Links please click here.

Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.