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.
Related articles
Related discussion
-
Help required to know about sync of RFID read tag thru Handheld-sample code in .net
by usha@myrf (0 replies)
-
A particular gallery image
by margy80 (0 replies)
-
Looking for real-life XML publish/subscribe applications
by perl0101 (0 replies)
-
Creating a Windows Service in VB.NET
by Templario55 (107 replies)
-
Watching Folder Activity in VB.NET
by emmaddai (17 replies)
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 .
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?
Glad you found the answer.
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>
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">
<xsl
<xsl:strip-space elements="*"/>
<xsl:template match="/SERVICE">
<sl:getAvailabilityResponse>
<sl
<sl:msgEnvelope >
<sl
<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>
<sl
<xsl:call-template name="DATEFORMAT1_TEMPLATE"/>
</sl
</sl:date></sl:scheduleInformationAnswer></xsl:for-each>
</sl
</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>
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>
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
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 ????
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 ????
some thing like
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl
<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>
<cargoInterfaceMessage
<cargoInterfaceMessage
<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 ????
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.
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.)
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
I didn't see what this article said about it, but it's true that default namespaces do not apply directly to attributes.
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?
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.
This thread is for discussions of Understanding XML Namespaces.