Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 553
  • Last Modified:

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?

0
rudytan2000
Asked:
rudytan2000
  • 3
  • 2
1 Solution
 
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
 
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now