gpt4 book ai didi

c# - 将 XSLT 中的模板附加到另一个 XSLT

转载 作者:太空宇宙 更新时间:2023-11-03 13:26:34 25 4
gpt4 key购买 nike

我在使用 C# 的 Visual Studio 2012 中工作。我有两个 xslt 文件。一个有几个模板。另一个在那里定义了一些节点。我想要的只是在 C# 中构建一个函数,使用它传递模板名称。它使用该名称在一个 xslt 中搜索,如果存在具有给定名称的模板,它会将其复制到第二个 xslt 中。

F("GetMonth") 的结果应该如下:

XSLT1:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template name="GetMonth">
<xsl:param name="Month"/>
<xsl:param name="PutCall"/>
<xsl:value-of select ="'A'"/>
</xsl:template>

XSLT2:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<DocumentElement>
// Some Code written
</DocumentElement>
</xsl:template>
</xsl:stylesheet>

Resultant XSLT2:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template name="GetMonth">
<xsl:param name="Month"/>
<xsl:param name="PutCall"/>
<xsl:value-of select ="'A'"/>
</xsl:template>

<xsl:template match="/">
<DocumentElement>
// Some Tags defined here
</DocumentElement>
</xsl:template>
</xsl:stylesheet>

我的尝试:

XmlDocument xslDoc1 = new XmlDocument();
XmlDocument xslDoc2 = new XmlDocument();
xslDoc1.Load("XSLT1.xslt");
xslDoc2.Load("XSLT2.xslt");

XmlNamespaceManager nsMg1r = new XmlNamespaceManager(xslDoc1.NameTable);
nsMgr1.AddNamespace("xsl", "http://www.w3.org/1999/XSL/Transform");

XmlNamespaceManager nsMgr2 = new XmlNamespaceManager(xslDoc2.NameTable);
nsMgr2.AddNamespace("xsl", "http://www.w3.org/1999/XSL/Transform");

XmlNodeList template = (XmlNodeList)xslDoc.SelectNodes("/xsl:stylesheet/xsl:template[@name = templateName]", nsMgr);
if(template != null)
{
// What code should be written here???
}

请提出建议。

最佳答案

string templateName = "GetMonth";

XmlNode template = xslDoc.SelectSingleNode(string.Format("/xsl:stylesheet/xsl:template[@name = '{0}']", templateName), nsMgr);

if (template != null)
{
// will append the template as last child of xsl:stylesheet
xslDoc2.DocumentElement.AppendChild(xslDoc2.ImportNode(template, true));
// as alternative to insert as the first child use
// xslDoc2.DocumentElement.InsertBefore(xslDoc2.ImportNode(template, true), xslDoc2.DocumentElement.FirstChild);
// now Save
xslDoc2.Save("XSLT2.xslt");
}

关于c# - 将 XSLT 中的模板附加到另一个 XSLT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22221177/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com