VB.Net Applies XSL to XML File Conversion

I have XML that is built by my application. This XML is discarded into an XML file, which I then want to apply the XSL stylesheet to convert it to an HTML page. However, each time, it just keeps coming out with the original XML, not the converted 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 that performs the conversion:

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 has been a long time since I did something with XSL and .NET, so I'm sure that I probably missed something obvious!

UPDATE: Here is the code that is currently behind the changes made as a result of the suggestions below ... 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 still get only the XML source file that is being output to my HTML file, not the HTML that should be the result of the XML / XSLT conversion.

+3
4

XPath :

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

XSLT :

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

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

, , XslTransform ( ) XslCompiledTransform. :

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

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

, .

0

... .

<xsl:value-of select="title" />

<xsl:value-of select="title" />

transformer.Transform(navigator, Nothing, output)

, xslt.

xml

<Title>Dr</Title>

xslt

<xsl:value-of select="title" />

,

// Enable XSLT debugging. 
XslCompiledTransform xslt = new XslCompiledTransform(true);

!

0

Source: https://habr.com/ru/post/1713647/


All Articles