Re: xmlns - proper usage

"Anthony Jones" <[email protected]> Thu, 3 Jan 2008 16:16:21 -0000
Newsgroups gmane.comp.mozilla.devel.layout.xslt
Message-ID <[email protected]>
<[email protected]> wrote in message
news:[email protected]...
> I've been looking everywhere to find the answer to - what is xmlns and
> how should one use it properly? I haven't got very much farther than
> it's the namespace...  Most xslt documents just use
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform.  There isn't much at
> that url.
>
> I have an xml file, which includes an xmlns of its own, which points
> to a .xsd file, which does what  you'd expect - defines variable names
> & such. The xmlns is defined in the top-level element:
>
> <Query Response xmlns="http://some_uri/API_Response.xsd">
>
> I couldn't get the stylesheets to work until I removed the xmlns from
> the .xml document.
>
> Could somebody shed light on this?


XML is designed to be 'eXtensible', that is tag names defined externally for
another purpose may be merged into an existing XML structure.  What happens
though if the externally defined XML uses some tag names already in use?
There is a collision where a single tag name may have very different uses.

This problem is solved by using namespaces.  A namespace is place where
names can be created as symbols to represent in this case XML tag names.
When a tag name is created in a namespace its full name includes both the
namespace and the tag name.  To ensure that all names are unique it was
decided to use a URI as the namespace identifier.

So for example your QueryResponse tagname has a full name of
http://some_uri/API_Response.xsd:QueryResponse.

Since you control the contents of some_uri no one else can create a
duplicate tag name.  Of course you will never see XML actually use names
like this.  Instead we use the special  xmlns attibute to specify a
namespace. Hence:-

<QueryResponse xmlns="http://some_uri/API_Response.xsd">

Indicates that this element and its descendants should be treated as being
part of the http://some_uri/API_Response.xsd namespace (as if it were
prefixed to the tag name as above).  This is known as defining the default
namespace.

Another form of the xmlns attribute defines an alias prefix.  For example:-

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

The xmlns in this element specifies that this element and its descendants
may use the xsl: prefix on a tagname to indicate that the name belongs in
the http://www.w3.org/1999/XSL/Transform namespace.

Aliasing allows multiple namespaces to be included in a document.  Only one
namespace can be the default (although at any level that can be changed)
hence to including elements from other namespaces will require an alias.

Typically in XSL the default namespace is left as undefined (leaving it
available to define default namespace for the result) hence in XSL you will
find the xsl elements themselves prefixed with xsl:  (sometimes you see
xmlns:xs="http://www.w3.org/1999/XSL/Transform" in which case xs: is used,
the actual alias isn't important).


In order to get your XSL to be able to read your elements in the input XML
you need to define an alias for it.  Like this:-

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:a="http://some_uri/API_Response.xsd" exclude-result-prefixes="a">

Now when you want to use some XPath in a match or select attribute you need
to prefix your tag names with a:

Eg.

<xsl:template match="a:QueryResponse">

Think of it this way before using the aliased name it replaces the a:
http://some_uri/API_Response.xsd: and compares it with names found in the
document.  Also when comparing names it includes any namespace of tag names
defined n the document so in this case
http://some_uri/API_Response.xsd:QueryResponse is compared to
http://some_uri/API_Response.xsd:QueryResponse which is match.

However what you were doing was QueryResponse compared to
http://some_uri/API_Response.xsd:QueryResponse which is not a match.

BTW, using the XSD file as the namespace identifier doesn't cause Schema
compatible parsers to validate the document with the contents of the XSD.
Just a side point Firefox doesn't do Schema anyway.

-- 
Anthony Jones - MVP ASP/ASP.NET