C# 通过 .NET 将参数传递给 XSLT 样式表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1521064/
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
Passing parameters to XSLT Stylesheet via .NET
提问by MrBliz
I'm trying to pass a parameter to an XSLT stylesheet, but all i'm getting is an empty xml document when the document is transformed using XSlCompiledTransform.
我正在尝试将参数传递给 XSLT 样式表,但是当使用 XSlCompiledTransform 转换文档时,我得到的只是一个空的 xml 文档。
This is the C# method used to add the parameters(after adding in people's suggestions)
这是用于添加参数的C#方法(在人们的建议中添加后)
private static void CreateHierarchy(string manID)
{
string man_ID = manID;
XsltArgumentList argsList = new XsltArgumentList();
argsList.AddParam("Boss_ID","",man_ID);
XslCompiledTransform transform = new XslCompiledTransform();
transform.Load("htransform.xslt");
using (StreamWriter sw = new StreamWriter("output.xml"))
{
transform.Transform("LU AIB.xml", argsList, sw);
}
}
and here is the stylesheet. The parameter i'm passing in is 'Boss_ID'
这是样式表。我传入的参数是“Boss_ID”
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="OrgDoc">
<xsl:param name="Boss_ID"></xsl:param>
<xsl:processing-instruction name="xml-stylesheet">
<xsl:text>type="text/xsl" href="..\styles\orgcharts.xsl" </xsl:text>
</xsl:processing-instruction>
<OrgDoc>
<xsl:for-each select="PosDets[@OC_Man = $Boss_ID]">
<PosDets OC_Pos="{@OC_Pos}" OC_Sub="{@OC_Sub}" OC_Man="{@OC_Man}" OC_Ttl="{@OC_Ttl}" OC_Rnk="{@OC_Rnk}" OC_Bdg="{@OC_Bdg}" OC_Fnd="{@OC_Fnd}" OC_OL3="{@OC_OL3}" OC_Tmp="{@OC_Tmp}">
<xsl:apply-templates select="../PosDets">
<xsl:with-param name="mgrid" select="@OC_Pos"/>
</xsl:apply-templates>
</PosDets>
</xsl:for-each>
</OrgDoc>
</xsl:template>
<xsl:template match="PosDets" >
<xsl:param name="mgrid" />
<xsl:if test="@OC_Man=$mgrid" >
<PosDets OC_Pos="{@OC_Pos}" OC_Sub="{@OC_Sub}" OC_Man="{@OC_Man}" OC_Ttl="{@OC_Ttl}" OC_Rnk="{@OC_Rnk}" OC_Bdg="{@OC_Bdg}" OC_Fnd="{@OC_Fnd}" OC_OL3="{@OC_OL3}" OC_Tmp="{@OC_Tmp}">
<xsl:apply-templates select="../PosDets">
<xsl:with-param name="mgrid" select="@OC_Pos"/>
</xsl:apply-templates>
</PosDets>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
I can't post all of the input document as it's confidential info, but here's a brief sanitised version
我无法发布所有输入文档,因为它是机密信息,但这里有一个简短的消毒版本
<OrgDoc><PosDets OC_Pos="161" OC_Man="9" OC_Ttl="Boss" OC_Rank="" OC_OL3="LU AIB" OC_Bdg="Has Budget" OC_Fnd="Payroll" OC_Sub="" OC_Tmp="" /><PosDets OC_Pos="190" OC_Man="161" OC_Ttl="Boss" OC_Rank="" OC_OL3="LU AIB" OC_Bdg="Has Budget" OC_Fnd="Payroll" OC_Sub="" OC_Tmp="" /><PosDets OC_Pos="199" OC_Man="190" OC_Ttl="Boss" OC_Rank="" OC_OL3="LU AIB" OC_Bdg="Has Budget" OC_Fnd="Payroll" OC_Sub="" OC_Tmp="" /></OrgDoc>
Can anyone help?
任何人都可以帮忙吗?
Thanks
谢谢
采纳答案by Dirk Vollmar
You need to define the parameter within your XSLT and you also need to pass the XsltArgumentList
as an argument to the Transform
call:
您需要在 XSLT 中定义参数,并且还需要将XsltArgumentList
参数作为参数传递给Transform
调用:
private static void CreateHierarchy(string manID)
{
string man_ID = manID;
XsltArgumentList argsList = new XsltArgumentList();
argsList.AddParam("Boss_ID", "", man_ID);
XslCompiledTransform transform = new XslCompiledTransform(true);
transform.Load("htransform.xslt");
using (StreamWriter sw = new StreamWriter("output.xml"))
{
transform.Transform("LU AIB.xml", argsList, sw);
}
}
Please note that the xsl:param
must be defined below the xsl:stylesheet
element:
请注意,xsl:param
必须在xsl:stylesheet
元素下方定义:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:param name="Boss_ID"></xsl:param>
<xsl:template match="OrgDoc">
<!-- template body goes here -->
</xsl:template>
</xsl:stylesheet>
This simple XSLT sample will create just a small output document containing one XML node with its contents set to the value of your parameter. Have a try:
这个简单的 XSLT 示例将创建一个小的输出文档,其中包含一个 XML 节点,其内容设置为您的参数值。试试:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:param name="Boss_ID"></xsl:param>
<xsl:template match="/">
<out>
<xsl:value-of select="$Boss_ID" />
</out>
</xsl:template>
</xsl:stylesheet>
回答by Michael Edwards
you probably need to define the param at the top of the XSLT:
您可能需要在 XSLT 的顶部定义参数:
<xsl:param name="Boss_ID" />
<OrgDoc>
//rest of the XSLT
</OrgDoc>
See this link
看这个链接
Not a great example but the best I could find with a quick google.
不是一个很好的例子,但我可以通过快速谷歌找到最好的例子。