Solved

get value of XML element with namespace

Posted on 2003-11-06
6
539 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
XML to SQL Table using c# 5 57
XML Data Missing in PHP SimpleXML 8 45
Having trouble setting Apache to recognise 2 vitual sites (on different drive). 3 61
XSLT XML 4 14
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…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

863 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

27 Experts available now in Live!

Get 1:1 Help Now