VB.Net 将 XSL 转换应用于 XML 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1187782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VB.Net apply XSL transformation to XML file
提问by hermiod
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
我有一些由我的应用程序构建的 XML。这个 XML 被放到一个 XML 文件中,然后我希望应用一个 XSL 样式表,以便将它转换为一个 HTML 页面。然而,每次,它只是不断地出现原始 XML 而不是转换后的 HTML
Here is the XML:
这是 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!
我已经很久没有使用 XSL 和 .NET 做过任何事情了,所以我确定我可能遗漏了一些明显的东西!
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:
<?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:
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.
我仍然只是将原始 XML 输出到我的 HTML 文件,而不是应该由 XML/XSLT 转换产生的 HTML。
回答by Tomalak
You've the wrong XPath in your loop:
您在循环中使用了错误的 XPath:
<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:
虽然为了提高可读性和更好地使用 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>
回答by Jason Evans
Try changing the XSLT to this:
尝试将 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>
回答by Pavel Minaev
I'm not sure what the original problem was, but you definitely should avoid using XslTransformaltogether (it's deprecated), and use XslCompiledTransforminstead. Here's how:
我不确定最初的问题是什么,但你绝对应该避免XslTransform完全使用(它已被弃用),XslCompiledTransform而是使用。就是这样:
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.
试试这个,看看你是否有不同的行为。
回答by blancail
found the prob... very basic. There's a diff between
发现问题...非常基本。之间有区别
<xsl:value-of select="title" />
and
和
<xsl:value-of select="Title" />
the transformer when doing transformer.Transform(navigator, Nothing, output)
做变压器时 transformer.Transform(navigator, Nothing, output)
is case sensitive when it uses the xslt file.
使用 xslt 文件时区分大小写。
the xml tag is
xml 标签是
<Title>Dr</Title>
the xslt tag is
xslt 标签是
<xsl:value-of select="title" />
I've used the debugger to track that enabling the transformer debugger
我已经使用调试器来跟踪启用变压器调试器
// Enable XSLT debugging.
XslCompiledTransform xslt = new XslCompiledTransform(true);
Enjoy !!
享受 !!

