views:

1477

answers:

3

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"&gt;

    <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.

+1  A: 

You've the wrong XPath in your loop:

<xsl:for-each select="firelist/visitor">
  <!-- ... --->
</xsl:for-each>

Though for increased readability and better use of XSLT's features I would recommend working with dedicated templates instead of explicit for-each loops:

<xsl:template match="/">
  <html>
    <body>
      <xsl:apply-templates select="firelist/visitor" />
    </body>
  </html>
</xsl:template>

<xsl:template match="visitor">
  <xsl:value-of select="title"/>
</xsl:template>
Tomalak
Thanks for that, but I don't think the issue is in the XSL. If it was, then it would render as nothing, but all I get in the output is the original XML, so it looks like it is not even applying the XSL and is just pumping the original XML out.
hermiod
Still, your XPath was wrong. :) The rest works for me (tested on VS 2005).
Tomalak
A: 

Hi there.

Try changing the XSLT to this:

<?xml version="1.0"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;

    <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>

Cheers. Jas.

Jason Evans
A: 

I'm not sure what the original problem was, but you definitely should avoid using XslTransform altogether (it's deprecated), and use XslCompiledTransform instead. Here's how:

transformer = New XslCompiledTransform()
transformer.Load("firelist.xslt")

''# transform XML data
output = New StringWriter()
transformer.Transform(navigator, Nothing, output)

Try this and see if you get a different behavior.

Pavel Minaev
Sorry its taken me so long to reply to this, was busy with a few other projects last week!Have tried changing the code to using XSLCompiledTransform instead but still no luck.I will update the original post to reflect how the code now looks.
hermiod