通过.NET将参数传递给XSLT样式表 [英] Passing parameters to XSLT Stylesheet via .NET
问题描述
我想传递参数给XSLT样式表,但所有我得到是当文档使用XSlCompiledTransform变换一个空的XML文档。
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.
这是用于添加参数C#的方法(在人们的建议,加入后)
This is the C# method used to add the parameters(after adding in people's suggestions)
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);
}
}
和这里是样式表。我传递的参数是Boss_ID
and here is the stylesheet. The parameter i'm passing in is '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>
谁能帮助?
感谢
推荐答案
您需要在XSLT中定义的参数,还需要通过的XsltArgumentList
作为参数到变换
电话:
You need to define the parameter within your XSLT and you also need to pass the XsltArgumentList
as an argument to the Transform
call:
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);
}
}
请注意,的xsl:参数
必须定义的 XSL如下:样式
元素:
Please note that the xsl:param
must be defined below the xsl:stylesheet
element:
<?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>
这个简单的XSLT示例将创建一个仅仅包含其内容设置为您的参数值一XML节点小输出文档。有一个尝试:
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:
<?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>
这篇关于通过.NET将参数传递给XSLT样式表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!