Library tutorials & articles
Understanding XML Namespaces
Why namespaces?
Namespaces have been a W3C standard since January 1999. The idea behind namespaces is simple: We need a way to fully qualify XML element and attribute names to prevent from confusing two elements that have the same name but mean different things.
For example, you might have an accounting application that uses an
element called <schedule> to mean an accounting schedule. A time management application
might use the same element <schedule> to mean a time schedule. You might
need to put accounting and time management data in one XML document. For example,
you might be keeping track of all project information including accounting and
project timeline in the same document. Now each application would not be able
to tell which <schedule> element belongs to it. To solve this problem,
you could prefix each <schedule> element with a unique prefix that indicates
to which application it belongs. For example <accounting:schedule> and <time:schedule> would
make things much clearer. In this example accounting and time are namespaces.
Namespaces are simply a way to uniquely identify elements and attributes within a document.
Related articles
Related discussion
-
vb6 generated help file not working in Window 7 and Vista
by Thushan Fernando (1 replies)
-
doctype in xml using c#.net
by madarapurajesh (0 replies)
-
Like statement in Xpath
by madarapurajesh (3 replies)
-
Problem looping through xml file to play a flv video file
by Bozmeister (0 replies)
-
Creating a Windows Service in VB.NET
by davidvanr (108 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.
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.