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]
Quiz: how many namespaces are in scope?

Hi Folks,

Consider this XML document:

<?xml version="1.0"?>
<N1:NumberList xmlns:N1="http://www.example1.org";
               xmlns:N2=" http://www.example2.org ">
        <Number>23</Number>
        <Number>41</Number>
        <Number>70</Number>
        <Number>103</Number>
        <Number>99</Number>
        <Number>6</Number>
</N1:NumberList>

For the first child element: 

        <Number>23</Number>

what namespaces are in scope?

I created an XSLT template to output the <Number> element's in-scope namespaces:

-------------------------------------------
<xsl:template match="*">
       <xsl:for-each select="namespace::*">
           <xsl:value-of select="."/></p>
       </xsl:for-each>
</xsl:template>
-------------------------------------------

Here is the output:

      http://www.w3.org/XML/1998/namespace
 
      http://www.example1.org
 
      http://www.example2.org

There are three (3) in-scope namespaces.

Lesson Learned: Implicit on the root element of every XML document is this namespace declaration:

      <root xmlns:xml="http://www.w3.org/XML/1998/namespace";>

NOTE: it is illegal to explicitly declare the xml namespace. The xml namespace is built into every XML application.

Since the xml namespace is implicitly declared in every XML document, you can immediately use the xml:lang attribute and the xml:space attribute, e.g.,

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

Here is an XSLT template to output the in-scope namespaces and their prefixes:

-------------------------------------------
<xsl:template match="*">
        <xsl:for-each select="namespace::*">
            Namespace in scope = <xsl:value-of select="."/>
            Namespace prefix = <xsl:value-of select="name(.)"/>          
        </xsl:for-each>
</xsl:template>
-------------------------------------------

Here is the output:

      Namespace in scope = http://www.w3.org/XML/1998/namespace
      Namespace prefix = xml 

      Namespace in scope = http://www.example1.org
      Namespace prefix = N1

      Namespace in scope = http://www.example2.org
      Namespace prefix = N2

QUESTIONS

1. Have I correctly analyzed the situation (regarding in-scope namespaces)?

2. What else would you add?

/Roger


[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