【问题标题】:Add code to XSLT before transformation在转换之前将代码添加到 XSLT
【发布时间】: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,那没问题。跨度>

标签: c# xml xslt


【解决方案1】:
XNamespace ns = "http://www.w3.org/1999/XSL/Transform";

XElement xslt = XElement.Load(sXslPath);

xslt.AddFirst(new XElement(ns + "include", new XAttribute("href", "FOOT.xslt")));
xslt.AddFirst(new XElement(ns + "include", new XAttribute("href", "HEAD.xslt")));

XElement body = xslt.Descendants("body").Single();

body.AddFirst(new XElement(ns + "call-template", new XAttribute("name", "Header")));
body.Add(new XElement(ns + "call-template", new XAttribute("name", "Footer")));

【讨论】:

  • 我认为要正确解析新添加的xsl:include 和相对href URL 值,您需要使用XElement xslt = XElement.Load(xXslPath, LoadOptions.SetBaseUri)
猜你喜欢
  • 1970-01-01
  • 2023-01-30
  • 2022-08-20
  • 1970-01-01
  • 2018-12-21
  • 2012-07-09
  • 1970-01-01
  • 2023-03-29
  • 1970-01-01
相关资源
最近更新 更多