tags:

views:

88

answers:

2

Hi,

can anyone start me off with regard to converting some XML to HTML. I've worked on XSLT conversions before, but never from scratch, and I seem to be missing something.

Starting with XML that's something like this:

<order name="fred" value="123.45">
  <lines>
    <line description="foo" value="123"/>
    <line description="bar" value="0.45"/>
  </lines>
</order>

...and an XSLT file that looks like this:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
  <xsl:decimal-format name="sterling" decimal-separator="." grouping-separator=","/>
  <xsl:output method="html" indent="yes"/>

  <xsl:template match="/">
    <html>
      <head>
        <xsl:apply-templates select="order"/>
      </head>
      <body>
        <table border="2">
          <xsl:apply-templates  select="order/lines/line"/>
        </table>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="order">
    Name is '<xsl:value-of select="@name"/>' and value is <xsl:value-of select="format-number(@value, '£#,###.00', 'sterling')"/>
  </xsl:template>

  <xsl:template match="order/lines/line">
    <tr>
      <td>
        <xsl:value-of select="@description"/>
      </td>
      <td>
        <xsl:value-of select="@value"/>
      </td>
    </tr>
  </xsl:template>

</xsl:stylesheet>

When I do the conversion using an XslCompiledTransform object from C#, I get the basic html but no content for the the lines. The code I used to the the transform isas follows:

private static String GetHtml(String xml)
{
    String result;

    var doc = new XmlDocument();
    doc.LoadXml(xml);

    var transformer = new XslCompiledTransform(true);
    transformer.Load("foobar.xslt");

    using (var writer = new StringWriter())
    {
        transformer.Transform(doc, null, writer);
        result = writer.ToString();
    }

    return result;
}

Any help greatly appreciated.

Ross

(as an aside, what's the point of the xsl:decimal-format element if, in using the format-number function, I have to provide the format string ?)

+1  A: 

This will never be used, as there is no attribute or element named fred:

<xsl:template match="fred">

You need something like:

<xsl:template match="@name">

And similarly, the apply-templates section calling this attribute needs to change to:

<xsl:apply-templates select="@name"/>

Based on the structure of the XML you posted, you need to change every mention of fred to order in your XSLT.

Oded
Thankyou for pointing this out. Too much haste on my part otherwise I would have posted the correct xslt file. I have corrected the original post which now works for the main 'order' details but not the lines.Thanks,Ross
Ross Watson
+1  A: 

The content you appear to want to render into the HTML HEAD doesn't look valid to got in there.

Oded is correct, you have no fred element in your source document, only an attribute taking the value fred.

Also one of your templates mentions Requisition which is also not present in your source XML.

<xsl:template match="/">
  <html>
    <head>
      <title><xsl:apply-templates select="order"/></title>
    </head>
    <body>
      <table border="2">
        <xsl:apply-templates  select="order/lines/line"/>
      </table>
    </body>
  </html>
</xsl:template>

<xsl:template match="order">
  Name is '<xsl:value-of select="@name"/>' and value is <xsl:value-of select="format-number(@value, '£#,###.00', 'sterling')"/>
</xsl:template>

<xsl:template match="line">
  <tr>
    <td>
      <xsl:value-of select="@description"/>
    </td>
    <td>
      <xsl:value-of select="@value"/>
    </td>
  </tr>
</xsl:template>

Dry-coded, ymmv.

fd
Hi,thanks for pointing that out. I was trying to create a small test app from a much larger one, and didn't change the node/attribute names correctly. Now at least I have a working mini test app - now I need to find out why my main application doesn't work.One of the great things about forums like StackOverflow, is that your own errors become glaringly obvious to you as soon as you press "Post".Many thanks again.Ross
Ross Watson