2

I am stuck in firstly creating a simple xslt. stylesheet to use for transforming an xml object.Secondly I am stuck in actually using the xslt stylesheet to do the transformation.

The background is the following:

I have a web part which is essentially a small form that has three inputs. These inputs then get submitted and used to query an external API by Http GET request. The results of the query then are displayed on a separate page in XML format. What I now need is to transform the xml to html and output that instead of the XML.

What I currently have:

I have a string variable "tmp" that holds the results from an api query by http get request. The results of the query get stored in the variable and I am able to display the results using: (Code given are small snippets of the whole code which are the most relevant for this particular case)

StreamReader reader = new StreamReader(response.GetResponseStream());
string tmp = reader.ReadToEnd();

Response.Write(tmp);
Response.End();

I then use "tmp" as an XML document object like so:

XmlDocument doc = new XmlDocument();
doc.Load(tmp);

To my project I then added an xslt file that will be used for the transformation.

Here is where I am stuck:

  1. I have created the XML document object as listed above. How do I then proceed to use the XSLT file I have added to my project to do the transformation?

  2. How do I actually achieve the transformation to have the output transformed to HTML.

I have been struggling with this for the best part of a week now.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Dev P
  • 1,157
  • 5
  • 32
  • 54

2 Answers2

4

Here's a simple example of how to use XSLT to transform XML into HTML:

string tmp = "<XML DATA>";
StringBuilder sbXslOutput = new StringBuilder();   

using (XmlWriter xslWriter = XmlWriter.Create(sbXslOutput))  
{  
    XslCompiledTransform transformer = new XslCompiledTransform();  
    transformer.Load("transformer.xsl");  
    XsltArgumentList args = new XsltArgumentList();  

    XmlDataDocument doc = new XmlDataDocument();
    doc.Loadxml(tmp);

    transformer.Transform(doc, args, xslWriter);  
}  

string dataSetHtml = sbXslOutput.ToString();  

Let's say this is your XML:

<RecentMatter>   
  <UserLogin>PSLTP6\RJK</UserLogin>   
  <MatterNumber>99999-2302</MatterNumber>   
  <ClientName>Test Matters</ClientName>   
  <MatterName>DP Test Matter</MatterName>   
  <ClientCode>99999</ClientCode>   
  <OfficeCode/>   
  <OfficeName/>   
  <Billable>true</Billable>   
  <ReferenceId/>   
  <LastUsed>2011-08-23T23:40:24.13+01:00</LastUsed>   
</RecentMatter>   
<RecentMatter>   
  <UserLogin>PSLTP6\RJK</UserLogin>   
  <MatterNumber>999991.0002</MatterNumber>   
  <ClientName>Lathe 1</ClientName>   
  <MatterName>LW Test 2</MatterName>   
  <ClientCode/>   
  <OfficeCode/>   
  <OfficeName/>   
  <Billable>true</Billable>   
  <ReferenceId/>   
  <LastUsed>2011-07-12T16:57:27.173+01:00</LastUsed>   
</RecentMatter>   
<RecentMatter>   
  <UserLogin>PSLTP6\RJK</UserLogin>   
  <MatterNumber>999991-0001</MatterNumber>   
  <ClientName>Lathe 1</ClientName>   
  <MatterName>LW Test 1</MatterName>   
  <ClientCode/>   
  <OfficeCode/>   
  <OfficeName/>   
  <Billable>false</Billable>   
  <ReferenceId/>   
  <LastUsed>2011-07-12T01:59:06.887+01:00</LastUsed>   
</RecentMatter>   
</NewDataSet>   

Here's an XSLT script that transforms the XML to HTML:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">  
  <xsl:template match="/">  
      <table border="1">  
        <tr>  
          <th>User Login</th>  
          <th>Matter Number</th>  
          ...  
        </tr>  
        <xsl:for-each select="NewDataSet/RecentMatter">  
          <tr>  
            <td>  
              <xsl:value-of select="UserLogin"/>  
            </td>  
            <td>  
              <xsl:value-of select="MatterNumber"/>  
            </td>  
            ...  
          </tr>  
        </xsl:for-each>  
      </table>  
  </xsl:template>  
</xsl:stylesheet> 
James Johnson
  • 45,496
  • 8
  • 73
  • 110
  • Thanks a lot James, this seems to be exactly what I am looking for. I will give this a go now and hopefully get this issue resolved. Many Thanks – Dev P Oct 19 '11 at 15:58
  • Great. Don't forget to accept the answer if you're satisfied. – James Johnson Oct 19 '11 at 16:05
  • Hi James, at: string tmp = ""; tmp is the string that already holds/stores xml data. Can i get away with just using it as is without having to actually asssigned xml data as you have suggested? – Dev P Oct 20 '11 at 09:14
  • Hey James. Is it possible to create XSL for XML programmatically? – Ghasem May 30 '15 at 07:16
1

You can write to a memory stream:

MemoryStream mStream = new MemoryStream();
mXslt.Transform(new XPathDocument(new XmlNodeReader(mXml)), null, mStream );
mStream.Position = 0;
StreamReader mReader = new StreamReader(mStream);
string mOutput = mReader.ReadToEnd();

Use XPathDocument and XslCompiledTransform. They are much faster than XslTransform and XmlDocument. Even if you use an XmlDocument to create the xml, covert it to an XPathDocument for the transform.

Refer this also for more info: Simplest way to transform XML to HTML with XSLT in C#?

Community
  • 1
  • 1
Siva Charan
  • 17,940
  • 9
  • 60
  • 95
  • If under ASP.NET you can do the transform directly to the HTTP response stream: `HttpResponse.Output` is a `TextWriter` and some of the overloads of `XslCompiledTransform.Transform` take a `TextWriter` for the output parameter. – Richard Oct 19 '11 at 15:02
  • Thanks for all your suggestions – Dev P Oct 19 '11 at 16:00
  • +1, do you have any references stating why its faster? i have to do a lot of transforming in a project and sometimes get performance issues when using large files.. – int32 Oct 20 '11 at 05:27