Reputation: 2404
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 ?)
Upvotes: 1
Views: 679
Reputation: 10772
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.
Upvotes: 1
Reputation: 499302
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.
Upvotes: 1