我正在用 C# 编辑一个 xslt。 它在其中定义了一个模板“Get”。 我想调用这个模板并将其传递给一个变量。
模板:
<xsl:template name="Get">
<xsl:param name="varMonth" />
<xsl:choose>
<xsl:when test="$varMonth='JAN'">
<xsl:value-of select="'A'" />
</xsl:when>
</xsl:choose>
</xsl:template>
XSLT:
<?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>
<PositionMaster>
<xsl:variable name="varName">
<xsl:value-of select="''" />
</xsl:variable>
</PositionMaster>
</DocumentElement>
</xsl:template>
</xsl:stylesheet>
代码: 我得到一个字符串作为模板参数的输入
string input = "A";
XmlDocument xslDoc = new XmlDocument();
xslDoc.Load("a.xslt");
XmlNamespaceManager nsMgr = new XmlNamespaceManager(xslDoc.NameTable);
nsMgr.AddNamespace("xsl", "http://www.w3.org/1999/XSL/Transform");
XmlElement valueOf = (XmlElement)xslDoc.SelectSingleNode("/xsl:stylesheet/xsl:template[@match = '/']/DocumentElement/PositionMaster/xsl:variable[@name = "varName"]/xsl:value-of", nsMgr);
if (valueOf != null)
{
// What should i write here to get the below modified XSLT
}
必需的 XSLT:
<?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>
<PositionMaster>
<xsl:variable name="varName">
<xsl:call-template name="Get">
<xsl:with-param name="Month" select="input"/>
</xsl:call-template>
</xsl:variable>
</PositionMaster>
</DocumentElement>
</xsl:template>
</xsl:stylesheet>
最佳答案
我是否正确理解您想使用 C# 代码转换样式表?这似乎很疯狂;您正在使用 XSLT,因此您可以随意使用 XML 转换语言:使用它!
但是,我很困惑,因为您的原始样式表和修改后的样式表都不是有效的 XSLT。你不能有 <DocumentElement>
元素作为 xsl:stylesheet
的子元素;它肯定需要在模板中。
使用 XSLT 转换 XSLT 样式表是常见的做法,它可以成为解决某些问题的明智方法。但是,当有更好的技术可用时,通常会这样做,例如添加样式表参数(全局 xsl:param 元素)。
关于c# - 更新 XSLT 以合并模板调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22372071/