get value of XML element with namespace

Posted on 2003-11-06
Last Modified: 2013-11-19
I've been trying to get value of an xml element with a namespace declared. below is the sample of the xml file:

<DSPR xmlns="">

and here's the a fragment of the xsl i used:

<xsl:template match="/">
<xsl:for-each select="DSPR">
   <TR bgcolor="#ffffff" >
      <TD width="7%"><font class="detail"><xsl:value-of select="LNID"/></font></TD>

the problem is i couldnt seem to be able to retrive anything out. BUT if i remove the namespace xmlns="", i'd be doing just fine.

Anybody has any idea WHY?

Question by:rudytan2000
  • 3
  • 2

Accepted Solution

sparkplug earned 100 total points
ID: 9693397
You need to specify the namespace and give it a name in the XSLT file. e.g.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl=""  xmlns:x="">
<xsl:output method="html"/>

<xsl:template match="/x:DSPR">
<xsl:for-each select="x:DSPR">
   <TR bgcolor="#ffffff" >
     <TD width="7%"><font class="detail"><xsl:value-of select="x:LNID"/></font></TD>


I wonder why you need to use a namespace to point to an XSD file. If you use the following syntax then you can attach a schema without dealing with namespaces.
<DSPR xmlns:xsi="" xsi:noNamespaceSchemaLocation="">

LVL 26

Expert Comment

ID: 9695396
I've seen situations where the namespace is attached by some other processor to the XML, as part of the processing, and you just have to live with it.  I don't think he's using the namespace for pointing to the schema, but probably the schema is being used to generate the instance XML, with the data coming from a database perhaps, similar to how an ADO recordset is generated in XML.  The "" is a clue for this...  Web services will do this sometimes, if you haven't specified a namespace in the setup.

Mike Sharp

Author Comment

ID: 9698127
Sparkplug: many thanks to ur response. yes, i've followed ur example and get it right in the first try. Truly awesome u are!

Mike: yeap. u're right. the XML comes out from a VB.NET Dataset. I fill up the dataset from a database and hence it gives me the namespace. I could have removed this (namespace) but i didnt coz i need to read the XML back to the Dataset again later.

I have been doing XML/XSL/XSD for a little while and have to admit i dont really have a strong hold on it yet. Been wondering if there's any link u guys could provide for this? Microsoft offers documentations but separately on those topics. Have been looking for one that combines all them together in real scenario. Hmm.. guess experience counts..

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

LVL 26

Expert Comment

ID: 9698385 has a lot of tutorials and good stuff.  For XML Schemas, the best by far in my opinion is Roger Costello's site at  I use his schema tutorial in a class I teach.  It's so good, I've never bothered to create one myself.  He also has a Schema Best Practice site that's outstanding, given that there's so many ways to do the same thing with Schemas.  The contributers of that material is a who's who of XML...

The O'Reilly site's pretty good:

Also, has lots of interesting news about the XML community, which helps to keep current.  I have a newsfeed of theirs integrated into an internal site.

One of the books I use in classes I teach is XML Bible by Elliotte Rusty Harold.  Make sure you get the second edition.  It might be a bit basic for you, but covers most aspects of XML pretty well.

I also have a number of examples (mostly XSLT) on my site.  These are mostly generated in response to postings on Usenet or EE here.  You can find them (along with a set of examples regarding how XSLT deals with namespaces)  at:

Also, in your case you want to preserve the namespace in the orginal XML, but if you want to strip it from the result of the transformation, you can specify that using the  exclude-result-prefixes attribute of the stylesheet root.  For example:

<xsl:stylesheet version="1.0"

will remove it from the transformation, in the event that you're copying nodes from the original XML to the output, and don't want the namespace.

Mike Sharp

Author Comment

ID: 9704754
thanks. many many thanks. i've been trying to preserve whitespace now. will

<xsl:preserve-space elements="x:*"/> preserve all spaces including carriage return character? it doesnt seem to work..
LVL 26

Expert Comment

ID: 9705370
Whitespace handling is a huge and difficult issue.  xsl:preserve-space only works in that it offsets the xsl:strip-space. That is, initially, the set of "white-space-preserving elements" contains all elements. If an element name matches a name in an <xsl:strip-space> element, then it is removed from the set of white space-preserving element names. If an element name matches a name in an <xsl:preserve-space> element, then it is added to the set of white space-preserving element names.

Your whitespace has probably been stripped before the XSLT ever gets to see it.  Read MSDN, they have some good articles on handling whitespace.  MSXML does not follow the XML specification in this regard, mainly for performance reasons.  If you still can't figure it out, post a new question with more information on the problem.

Mike Sharp

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Fetch XML Unions? 3 357
website content maintenance 3 57
cookies analysis tools 2 26
Adjust the position 3 0
Most of the sites are being standardized with W3C Web Standards. W3C provides lot of web standard services to the web. They have the web specification, process and documentation for all the web standards. You can apply HTML, CSS and Accessibility st…
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now