Library tutorials & articles

Understanding XML Namespaces

Introduction

Understanding XML namespaces is essential to understanding and building Web services. Unfortunately, most developers do not understand how XML namespaces work and get confused when they see all the colons and URLs. The funny thing is, namespaces are so simple that once you understand them you wonder why you had trouble understanding them in the first place. I wrote this article to help you get over the initial hump of understanding XML namespaces, and to show you how they are used in XML documents.

This article is geared towards Visual Basic developers. If your work involves HTML more than programming, there’s an excellent namespace tutorial that speaks to your needs.

Comments

  1. 26 Jul 2005 at 12:08

    Hi,
    I'm having a problem with xml namespaces.
    My application accepts an xml document from another application, which is then validated using an xsd. The xsd has a namespace defined and uses a prefix "ns1" for the tags. The application from which i am supposed to get my xml doc for processing keeps changing the prefix but the namespace URI is the same. This causes the xsd to throw an error.


    Is there any way by which the xsd can accept any prefix from the same URI?

  2. 11 Feb 2005 at 08:26
    Just to say thank you for this article. I have just started learning xml. And after reading this article, I am clear in namespaces atleast.
  3. 30 Dec 2004 at 11:13

    Glad you found the answer.

  4. 29 Dec 2004 at 21:44

    The beloe format gives the required o/p



    <xsl:transform version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:sl="com.klm.cargo.ebiza.freightWebService">
      <xslutput indent="no" method="html"/>
      <xsl:strip-space elements="*"/>
      <xsl:template match="/SERVICE">
          <sl:getAvailabilityResponse>
              <slutFVA>
                  <sl:msgEnvelope >
                  <slriority>high</slriority>
                  <sl:recipientId>1 -2n</sl:recipientId>
                  <sl:gmt>W3</sl:gmt>
                  <sl:senderInfo>WQere</sl:senderInfo>                
                   </sl:msgEnvelope>
                  <sl:standardMessageIdentification> GTY</sl:standardMessageIdentification>
                  <sl:version>2n</sl:version>
                  <xsl:for-each select="AVLBLTY/FLIGHTLIST/STRETCH ">
                  <sl:scheduleInformationAnswer>
                      <sl:scheduleAndAvailabiltyinformationDetails>
                          <sl:date>
                              <slay>
                                  <xsl:call-template name="DATEFORMAT1_TEMPLATE"/>
                              </slay>
    </sl:date></sl:scheduleInformationAnswer></xsl:for-each>


    </slutFVA>
    </sl:getAvailabilityResponse>
    </xsl:template>


      <xsl:template name="DATEFORMAT1_TEMPLATE">
          <!--day-->
          <xsl:variable name="date" select="@DEPDATE"/>
          <xsl:value-of select="substring($date,7,2)"/>
      </xsl:template>

  5. 29 Dec 2004 at 21:43


    The beloe format gives the required o/p



    <xsl:transform version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:sl="com.klm.cargo.ebiza.freightWebService">
       <xslutput indent="no" method="html"/>
       <xsl:strip-space elements="*"/>
       <xsl:template match="/SERVICE">
           <sl:getAvailabilityResponse>
               <slutFVA>
                   <sl:msgEnvelope >
                   <slriority>high</slriority>
                   <sl:recipientId>1 -2n</sl:recipientId>
                   <sl:gmt>W3</sl:gmt>
                   <sl:senderInfo>WQere</sl:senderInfo>                
                    </sl:msgEnvelope>
                   <sl:standardMessageIdentification> GTY</sl:standardMessageIdentification>
                   <sl:version>2n</sl:version>
                   <xsl:for-each select="AVLBLTY/FLIGHTLIST/STRETCH ">
                   <sl:scheduleInformationAnswer>
                       <sl:scheduleAndAvailabiltyinformationDetails>
                           <sl:date>
                               <slay>
                                   <xsl:call-template name="DATEFORMAT1_TEMPLATE"/>
                               </slay>
    </sl:date></sl:scheduleInformationAnswer></xsl:for-each>


    </slutFVA>
    </sl:getAvailabilityResponse>
    </xsl:template>


       <xsl:template name="DATEFORMAT1_TEMPLATE">
           <!--day-->
           <xsl:variable name="date" select="@DEPDATE"/>
           <xsl:value-of select="substring($date,7,2)"/>
       </xsl:template>
           
               

  6. 29 Dec 2004 at 20:51

    Hi  lurs thanks for the reply....


    Here i have one more query


    i have a input XML  beloe which does not  have any namespace which is given as a input to a XSL and i need a XML  which shuld have a  XML with the namespace




    INPUT



    <AVLBLTY>
    <FLIGHTLIST DEPDATE="20041104">
    <STRETCH BRD="AMS" PALLETIZED="N" STOPS="0" CAO="N" AIRCRAFTTYPE="739">
    </STRETCH>
    <AVLBLTY>




    XSL




    is the beloe XSL is a correct format ????


    <xsl:transform version="1.0"  xmlns:kl="http://www.w3.org/1999/XSL/Transform"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
       <xsl:output indent="no" method="html"/>
       <xsl:strip-space elements="*"/>
       <xsl:template match="/SERVICE">
    <kl:getAvailabilityResponse>
    <kl:outFVA>
    <kl:msgEnvelope >
    <kl:providerId> 1-2n</kl:providerId>
    <kl:priority>high</kl:priority>
    <kl:recipientId>1 -2n</kl:recipientId>
    <kl:gmt>W3</kl:gmt>
    <kl:senderInfo>WQere</kl:senderInfo>                
    </kl:msgEnvelope>
    <kl:standardMessageIdentification> GTY</kl:standardMessageIdentification>
    <kl:version>2n</kl:version>
    <xsl:for-each select="AVLBLTY/FLIGHTLIST/STRETCH ">
    <kl:scheduleInformationAnswer>
    <kl:scheduleAndAvailabiltyinformationDetails>
    <kl:date>
    <kl:Day>
    <xsl:call-template name="DATEFORMAT1_TEMPLATE"/>
    </kl:Day>
    </kl:date>
    </kl:scheduleAndAvailabiltyinformationDetails>
    </kl:scheduleInformationAnswer>
    </xsl:for-each>
    </kl:outFVA></kl:getAvailabilityResponse>    </xsl:template>


    <xsl:template name="DATEFORMAT1_TEMPLATE">
           <!--day-->
           <xsl:Value-of select="@DEPDATE"/>
    </xsl:template>



    i need the o/p  XML in the following format


    <kl:getAvailabilityResponse> // here the o/p sholud have name space  for the o/p XML tags


    //some more
    </kl:getAvailabilityResponse>

  7. 17 Dec 2004 at 07:52
    Yes, using a namespace prefix in a match pattern is correct.

    However your stylesheet also needs to declare the namespace.
    So your xsl:transform (or xsl:stylesheet) element needs to look like this:

    <xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmnls:cargoInterfaceMessage="http://www.klm.cargo.com">
    ...

    Otherwise, XSLT will complain that it doesn't recognize the 'cargoInterfaceMessage' prefix.

    Lars
    www.huttar.net
  8. 17 Dec 2004 at 02:01
    selecting a node or nodes from a specific namespace. here i want to use the match statement too


    some thing like
    <xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xslutput indent="no" method="html"/>
      <xsl:strip-space elements="*"/>
      <xsl:template match="/cargoInterfaceMessage:FVR"> //is it a correct way to match a namespace

      </xsl:template>
         
    </xsl:transform>



    For the Above XSL i hav a XML input as

    <cargoInterfaceMessage:FVR   xmnls:cargoInterfaceMessage ="http://www.klm.cargo.com" >
          <cargoInterfaceMessage:msgEnvelope>
          <cargoInterfaceMessageroviderid> 1-2n</cargoInterfaceMessageroviderid>
          <cargoInterfaceMessageriority> high</cargoInterfaceMessageriority>
          <cargoInterfaceMessage:recipientid>1-2yt</cargoInterfaceMessage:recipientid>
          <cargoInterfaceMessage:gmt>w3</cargoInterfaceMessage:gmt>
          <cargoInterfaceMessage:senderinfo>wqare</cargoInterfaceMessage:senderinfo>
          <cargoInterfaceMessage:senderid>1-2n</cargoInterfaceMessage:senderid>
      </cargoInterfaceMessage:msgEnvelope>


    is it the correct way ????

  9. 17 Dec 2004 at 02:01
    selecting a node or nodes from a specific namespace. here i want to use the match statement too


    some thing like
    <xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xslutput indent="no" method="html"/>
      <xsl:strip-space elements="*"/>
      <xsl:template match="/cargoInterfaceMessage:FVR"> //is it a correct way to match a namespace

      </xsl:template>
         
    </xsl:transform>



    For the Above XSL i hav a XML input as

    <cargoInterfaceMessage:FVR   xmnls:cargoInterfaceMessage ="http://www.klm.cargo.com" >
          <cargoInterfaceMessage:msgEnvelope>
          <cargoInterfaceMessageroviderid> 1-2n</cargoInterfaceMessageroviderid>
          <cargoInterfaceMessageriority> high</cargoInterfaceMessageriority>
          <cargoInterfaceMessage:recipientid>1-2yt</cargoInterfaceMessage:recipientid>
          <cargoInterfaceMessage:gmt>w3</cargoInterfaceMessage:gmt>
          <cargoInterfaceMessage:senderinfo>wqare</cargoInterfaceMessage:senderinfo>
          <cargoInterfaceMessage:senderid>1-2n</cargoInterfaceMessage:senderid>
      </cargoInterfaceMessage:msgEnvelope>


    is it the correct way ????

  10. 17 Dec 2004 at 02:00
    selecting a node or nodes from a specific namespace. here i want to use the match statement too


    some thing like
    <xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
       <xslutput indent="no" method="html"/>
       <xsl:strip-space elements="*"/>
       <xsl:template match="/cargoInterfaceMessage:FVR"> //is it a correct way to match a namespace

       </xsl:template>
         
    </xsl:transform>



    For the Above XSL i hav a XML input as

    <cargoInterfaceMessage:FVR   xmnls:cargoInterfaceMessage ="http://www.klm.cargo.com" >
           <cargoInterfaceMessage:msgEnvelope>
           <cargoInterfaceMessageroviderid> 1-2n</cargoInterfaceMessageroviderid>
           <cargoInterfaceMessageriority> high</cargoInterfaceMessageriority>
           <cargoInterfaceMessage:recipientid>1-2yt</cargoInterfaceMessage:recipientid>
           <cargoInterfaceMessage:gmt>w3</cargoInterfaceMessage:gmt>
           <cargoInterfaceMessage:senderinfo>wqare</cargoInterfaceMessage:senderinfo>
           <cargoInterfaceMessage:senderid>1-2n</cargoInterfaceMessage:senderid>
       </cargoInterfaceMessage:msgEnvelope>


    is it the correct way ????


  11. 22 Sep 2004 at 09:08

    Just want to say thanks for this article. I'm new to XML and this helped clear my namespace confusion immediately. Much better than every other article that I have read so far... :-)
    I can't believe something so simple confused the heck outta me...


    Thanks.


    Craig.

  12. 10 Dec 2003 at 12:43

    Quote:
    [1]Posted by lars_huttar on 10 Dec 2003 12:31 PM[/1]
    I didn't see what this article said about it, but it's true that default namespaces do not apply directly to attributes.



    OK, I looked at what the article said, and I believe he is mistaken. (You notice his example didn't include any attributes.)

  13. 10 Dec 2003 at 12:35

    Quote:
    [1]Posted by JimboTaylor on 6 Aug 2003 04:13 AM[/1]
    An interesting article! Does anybody now how to apply using these namespaces in XPath? For example selecting a node or nodes from a specific namespace.


    For example in an XSL stylesheet, you would put the namespace declaration in the stylesheet (e.g. on the xsl:stylesheet element), and then use the prefix directly in the XPath expression as part of a nametest. E.g.


    <xsl:stylesheet xmlns:foo="http://barbaz" ...>
      ... select="/foo:schedule/@*" ...


    Note that the default namespace DOES NOT apply to unprefixed elements (or attributes) in XPath expressions.
    Not in XPath 1.0 anyway; there is a way to do that in 2.0 though.


    Lars

  14. 10 Dec 2003 at 12:31

    I didn't see what this article said about it, but it's true that default namespaces do not apply directly to attributes.

  15. 26 Sep 2003 at 12:45

    Based on the http://www.w3.org/TR/REC-xml-names/, default namespace do not apply directly to attributes. But in this article it says you can define a default namespace that applies to all non-prefixed elements and attributes. So which one is right?

  16. 06 Aug 2003 at 04:13

    An interesting article! Does anybody now how to apply using these namespaces in XPath? For example selecting a node or nodes from a specific namespace.

  17. 01 Jan 1999 at 00:00

    This thread is for discussions of Understanding XML Namespaces.

Leave a comment

Sign in or Join us (it's free).

Yasser Shohoud Yasser started programming at the age of 12 when he wrote his first text-based game on a Commodore PET. He's since moved to IBM mainframes then to Microsoft technologies and has worked as Systems E...
AddThis

Related podcasts

  • LINQ to XML

    Scott's been poking around with LINQ to XML and reports his findings to Carl about life with XDocuments and XElements. They also talk about the bridge classes that link (no pun intended) System.Xml and System.Xml.Linq.

Events coming up

  • Jul 13

    SmartClient

    California, United States

    A Smart Client is an application that uses local processing, consumes XML Web Services and can be deployed and updated from a centralized server. While the .NET Framework (Windows Forms) and the .

Want to stay in touch with what's going on? Follow us on twitter!