tags:

views:

1292

answers:

4

Hi,

I make a request to an xml web service, and get back a response. This response, being a stream, is then saved to a string. The problem is, the response is full of tags, CDATA, etc (as you would expect). There is no line breaking either, as to be expected.

I want to take this string, which represents an xml document, and strip it of all its tags but keep the actual values, and also, make sure that each record is in one line, so:

<Record>
  <name>adam</name>
  <telephoneno>000</telephonenumber>
</Record>
<Record>
  <name>mike</name>
  <telephoneno>001</telephonenumber>
</Record>

Will be transformed to:

adam 000
mike 001

Headings is an easy issue, but how could I achieve this? I've tried datatables and datasets but I don't think they have great support for achieving what I am trying to do.

+7  A: 

This is exactly what XSLT is for! It transforms XML files into a different output. In your case, you could use a relatively simple XSL transformation to output a list.

This might do it:

records.xml:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="style.xsl"?>
<Records>
  <Record>
    <name>adam</name>
    <telephonenumber>000</telephonenumber>
  </Record>
  <Record>
    <name>mike</name>
    <telephonenumber>001</telephonenumber>
  </Record>
</Records>

style.xsl

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="text" omit-xml-declaration="yes" indent="no"/>
  <xsl:template match="Record">
    <xsl:value-of select="name"/><xsl:text> </xsl:text><xsl:value-of select="telephonenumber"/>
  </xsl:template>
</xsl:stylesheet>

I tested it with this tool and it works.

Michael Haren
Reference: http://www.xml.com/pub/a/2001/08/01/gettingloopy.html
Michael Haren
Generally if a transform emits text, you should set the xsl:output method to "text". It won't change anything in this specific transform (though it will with some). But I've seen XML tools that check the method to see if they should check the output for well-formedness, which you don't want here.
Robert Rossney
Good call, Robert.
Michael Haren
A: 

Michael Haren's XSLT response is the best answer.

But alternatively if you're not actually wanting the response as XML you can return whatever it is that you DO desire from the web service (provided it's yours, rather than a third party service).

There's no rule that says a web service must return XML. Just make sure to serve the appropriate MIME-Type.

BQ
A: 

Thanks guys!

I was actually trying to avoid xsl as I know it gets rather complex with the use of xpath queries and so forth. But where in the xsl file can I control appending the attributes/nodes to one line?

A: 

Ok I tested that in my browser but it comes up as adam 000 mike 001, however, in that app (which is great btw), it comes up as I want it. My browser is Opera.

So I will take my string, apply xsl, and output the stream and write it to csv. Neat.

What gives? Thanks for the education guys

Your browser is looking for HTML. In HTML, whitespace and line breaks are meaningless--you have to use the br tag instead.
Michael Haren