Solved

get value of XML element with namespace

Posted on 2003-11-06
6
546 Views
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="http://tempuri.org/DSPR.xsd">
  <DSPR>
    <KCOO>10100</KCOO>
    <DOCO>3000254</DOCO>
    <DCTO>SO</DCTO>
    <LNID>2000</LNID>
  </DSPR>
</DSPR>

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>
   </TR>
</xsl:for-each>
..
</xsl:template>

the problem is i couldnt seem to be able to retrive anything out. BUT if i remove the namespace xmlns="http://tempuri.org/DSPR.xsd", i'd be doing just fine.

Anybody has any idea WHY?

0
Comment
Question by:rudytan2000
  • 3
  • 2
6 Comments
 
LVL 9

Accepted Solution

by:
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="http://www.w3.org/1999/XSL/Transform"  xmlns:x="http://tempuri.org/DSPR.xsd">
<xsl:output method="html"/>

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

</xsl:stylesheet>

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="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://tempuri.org/DSPR.xsd">

>S'Plug<
0
 
LVL 26

Expert Comment

by:rdcpro
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 "http://tempuri.org" is a clue for this...  Web services will do this sometimes, if you haven't specified a namespace in the setup.

Regards,
Mike Sharp
0
 

Author Comment

by:rudytan2000
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..

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 26

Expert Comment

by:rdcpro
ID: 9698385
TopXML.com has a lot of tutorials and good stuff.  For XML Schemas, the best by far in my opinion is Roger Costello's site at http://www.xfront.com.  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:  http://xml.com

Also, XMLHack.com 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:

http://dev.rdcpro.com/

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"
                   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  
                   xmlns:x="http://tempuri.org/DSPR.xsd"
                    exclude-result-prefixes="x">

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.

Regards,
Mike Sharp
0
 

Author Comment

by:rudytan2000
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..
0
 
LVL 26

Expert Comment

by:rdcpro
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.

Regards,
Mike Sharp
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

679 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