【发布时间】:2015-07-10 11:24:08
【问题描述】:
我正在使用 C# 来转换 XML 文档,它工作正常:
using System;
using System.IO;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;
public class XmlTransformUtil
{
public static void Main(string[] args)
{
if (args.Length == 2)
{
Transform(args[0], args[1]);
}
else
{
PrintUsage();
}
}
public static void Transform(string sXmlPath, string sXslPath)
{
try
{
//load the Xml doc
XPathDocument myXPathDoc = new XPathDocument(sXmlPath);
XslTransform myXslTrans = new XslTransform();
//load the Xsl
myXslTrans.Load(sXslPath);
//create the output stream
XmlTextWriter myWriter = new XmlTextWriter
("result.html", null);
//do the actual transform of Xml
myXslTrans.Transform(myXPathDoc, null, myWriter);
myWriter.Close();
}
catch (Exception e)
{
Console.WriteLine("Exception: {0}", e.ToString());
}
}
public static void PrintUsage()
{
Console.WriteLine
("Usage: XmlTransformUtil.exe <xml path> <xsl path>");
}
}
上面的代码完美运行,但是我想做的是,在转换 XSLT 之前,我希望它在 XSLT 的特定部分添加额外的代码行。
XSLT 代码:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html lang="en-GB">
<body style="font-family:'Praxis Com Light'; color:#632423; width:100%; font-size:14px !important;">
//MAIN BODY OF CODE
</body>
</html>
</xsl:template>
</xsl:stylesheet>
我希望在转换之前如何在 C# 中更改 XSLT 代码:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:include href="HEAD.xslt"/>
<xsl:include href="FOOT.xslt"/>
<xsl:template match="/">
<html lang="en-GB">
<body style="font-family:'Praxis Com Light'; color:#632423; width:100%; font-size:14px !important;">
<xsl:call-template name="Header"/>
//MAIN BODY OF CODE
<xsl:call-template name="Footer"/>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
如何做到这一点?
【问题讨论】:
-
查看而不是将参数传递给转换,然后关闭它。示例 - stackoverflow.com/questions/1521064/…
-
不确定难度是什么。 XSLT 样式表是一个 XML 文档,您有一个用于转换 XML 文档的工具 (XSLT),所以就去做吧。在这里转换样式表是否是正确的策略我不知道(这是一种强大的技术,但有时还有其他更好的方法),但本质上,如果你想使用 XSLT 来转换 XSLT,那没问题。跨度>