get value of XML element with namespace

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?

rudytan2000Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sparkplugCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rdcproCommented:
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
rudytan2000Author Commented:
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
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

rdcproCommented:
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
rudytan2000Author Commented:
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
rdcproCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.