VB.Net将XSL转换应用于XML文件

时间:2009-07-27 11:58:45

标签: xml vb.net winforms xslt

我有一些由我的应用程序构建的XML。此XML被删除为XML文件,然后我希望将XSL样式表应用于该文件,以便将其转换为HTML页面。但是,每次,它都会继续使用原始XML而不是转换后的HTML

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

这是样式表:

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

以下是执行转换的代码:

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

我用XSL和.NET做了很多事情,所以我确定我可能错过了一些明显的东西!

更新: 以下是根据以下建议进行的修改后的代码... 代码隐藏:

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>

我仍然只是将原始XML输出到我的HTML文件,而不是应该由XML / XSLT转换产生的HTML。

4 个答案:

答案 0 :(得分:1)

你的循环中有错误的XPath:

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

虽然为了提高可读性并更好地使用XSLT的功能,我建议使用专用模板而不是显式的for-each循环:

<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 :(得分:0)

尝试将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>

答案 2 :(得分:0)

我不确定最初的问题是什么,但你绝对应该完全避免使用XslTransform(它已被弃用),而是使用XslCompiledTransform。方法如下:

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

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

试试这个,看看你是否有不同的行为。

答案 3 :(得分: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);

享受!!