Passing parameters to XSLT Stylesheet via .NET

后端 未结 2 2008
不知归路
不知归路 2020-11-29 04:48

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.

Thi

相关标签:
2条回答
  • 2020-11-29 05:07

    you probably need to define the param at the top of the XSLT:

    <xsl:param name="Boss_ID" />
    <OrgDoc>
     //rest of the XSLT
    </OrgDoc>
    

    See this link

    http://projects.ischool.washington.edu/tabrooks/545/2004Autumn/ContentManagement/PassingParameters.htm

    Not a great example but the best I could find with a quick google.

    0 讨论(0)
  • 2020-11-29 05:12

    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);
        }
    }
    

    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>
    

    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>
    
    0 讨论(0)
提交回复
热议问题