Hi there
I have got some XML which is built by my application. This XML is dropped to an XML file, which I then wish to apply an XSL stylesheet to in order to convert it to a HTML page. However, every time, it just keeps coming out with the original XML rather than the transformed HTML
Here is the XML:
<firelist>
<visitor>
<Title>Mr</Title>
<Forename>Gregory</Forename>
<Surname>House</Surname>
<Visiting>asasasas</Visiting>
<VisitTime>11:41</VisitTime>
<PurposeOfVisit>asasasasa</PurposeOfVisit>
<BadgeID>a</BadgeID>
<Campus>KWA</Campus>
<VisitingFrom>Princeton-Plainsboro Teaching Hospital</VisitingFrom>
<ImagePath>\\more\DataCard\VisitorPhotos\V0004.jpg</ImagePath>
</visitor>
</firelist>
Here is the stylesheet :
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="visitor">
<xsl:value-of select="title"/>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
and here is the code which does the transform :
Dim document As XmlDocument ''# Xml document root
Dim navigator As XPathNavigator ''# navigate document
Dim transformer As XslTransform ''# transform document
Dim output As StringWriter
document = New XmlDocument()
document.Load("firelist.xml")
''# create navigator
navigator = document.CreateNavigator
''# load style sheet
transformer = New XslTransform()
transformer.Load("firelist.xslt")
''# transform XML data
output = New StringWriter()
transformer.Transform(navigator, Nothing, output)
''# display transformation in text box
Console.WriteLine(output.ToString)
''# write transformation result to disk
Dim stream As FileStream = New FileStream("firelist.html", FileMode.Create)
Dim writer As StreamWriter = New StreamWriter(stream)
writer.Write(output.ToString)
''# close streams
writer.Close()
output.Close()
It is ages since I did anything with XSL and .NET so I'm sure I am probably missing something obvious!
UPDATE: Here is the code as it currently stands following modifications made as a result of below suggestions... Code-Behind:
Dim document As XmlDocument ' Xml document root
Dim navigator As XPathNavigator ' navigate document
Dim transformer As XslCompiledTransform ' transform document
Dim output As StringWriter
document = New XmlDocument()
document.Load("firelist.xml")
' create navigator
navigator = document.CreateNavigator
' load style sheet
transformer = New XslCompiledTransform()
transformer.Load("firelist.xslt")
' transform XML data
output = New StringWriter()
transformer.Transform(navigator, Nothing, output)
' display transformation in text box
Console.WriteLine(output.ToString)
' write transformation result to disk
Dim stream As FileStream = _
New FileStream("firelist.html", FileMode.Create)
Dim writer As StreamWriter = New StreamWriter(stream)
writer.Write(output.ToString)
' close streams
writer.Close()
output.Close()
XML:
<?xml version="1.0" encoding="utf-8"?>
<firelist>
<visitor>
<Title>Dr</Title>
<Forename>James</Forename>
<Surname>Wilson</Surname>
<Visiting>bob</Visiting>
<VisitTime>11:30</VisitTime>
<PurposeOfVisit>dunno</PurposeOfVisit>
<BadgeID>4</BadgeID>
<Campus>KWA</Campus>
<VisitingFrom>Princeton-Plainsboro Teaching Hospital</VisitingFrom>
<ImagePath>\\more\DataCard\VisitorPhotos\V0005.jpg</ImagePath>
</visitor>
</firelist>
XSLT:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/firelist">
<html>
<body>
<xsl:for-each select="visitor">
<xsl:value-of select="title"/>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
I am still only getting the original XML being output to my HTML file, rather than the HTML which should result from the XML/XSLT Transformation.