Library tutorials & articles
Understanding XML Namespaces
Unique Names
The names payrollData and HRData are descriptive but they are also very common and two different organizations could potentially use the name payrollData for two different namespaces. This might become a problem if you start exchanging XML documents with other organizations. For example, you might want to send the payroll information to the IRS in XML. So you’d send them an XML document with a bunch of elements that belong to the payrollData namespace. If for some reason the IRS decides to process your payroll data along with payroll data from other companies, its likely another company will also have used the payrollData namespace because that name is just not unique. Its also likely that this company’s payroll document structure is different from yours. Bottom line: You’d have a situation where an application needs to deal with two different namespaces that have the same name.
To prevent such situations, namespace names must be globally unique. Technically, a namespace must be a Uniform Resource Identifer as defined in IETF RFC 2396. Basically, a URI is a unique string that can be classified as a locator (a Uniform Resource Locator or URL) or a name (a Uniform Resource Name or URN) or both. We all know URLs and use them regularly to access Web pages and other Internet resources. For example, http://www.developerfusion.com/ is a URL. A URN is globally unique resource name that sticks around forever and never goes away even if the resource itself no longer exists. For example, you could use a Universally Unique Identifier (UUID) as a namespace like this:
urn:uuid:EB3859CB-0B67-4053-9C40-CF9DED28C33F
This is considered a URN and is therefore a valid namespace name. So if you pick that as your namespace name for the payroll data (instead of payrollData), the XML document would become:
<hr:employees xmlns:hr="HRData"
xmlns:py=" urn:uuid:EB3859CB-0B67-4053-9C40-CF9DED28C33F">
<hr:employee>
<hr:id>49982</hr:id>
<hr:name>Bart Simpson</hr:name>
<hr:hireDate>2000-07-04</hr:hireDate>
<py:salary>4000765.00</py:salary>
<py:taxes>3980765.27</py:taxes>
</hr:employee>
<hr:employee>
<hr:id>12345</hr:id>
<hr:name>Hugo Simpson</hr:name>
<hr:hireDate>2000-05-29</hr:hireDate>
<py:salary>82000.00</py:salary>
<py:taxes>16567.87</py:taxes>
</hr:employee>
</hr:employees>
All I did was change the namespace prefix mapping for the py prefix to point
to the new namespace name. You can also use a URL as your namespace name, for
example, http://www.developerfusion.com/schemas/payroll/ for the
payroll data and
http://www.developerfuison.com/schemas/hr/ for the HR data:
<hr:employees xmlns:hr="http://www.developerfusion.com/schemas/hr/"
xmlns:py="http://www.developerfusion.com/schemas/payroll/">
<hr:employee>
<hr:id>49982</hr:id>
<hr:name>Bart Simpson</hr:name>
<hr:hireDate>2000-07-04</hr:hireDate>
<py:salary>4000765.00</py:salary>
<py:taxes>3980765.27</py:taxes>
</hr:employee>
<hr:employee>
<hr:id>12345</hr:id>
<hr:name>Hugo Simpson</hr:name>
<hr:hireDate>2000-05-29</hr:hireDate>
<py:salary>82000.00</py:salary>
<py:taxes>16567.87</py:taxes>
</hr:employee>
</hr:employees>
Note that the URL need not point to anything on the Internet. It is just used as a unique string. This is a source of confusion for most people who tend to think that there’s some document located at this URL. Just keep in mind a namespace name is simply a unique string, nothing more nothing less.
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.