views:

686

answers:

2

Hello,

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.

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


    }

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>

Can anyone help?

Thanks

+2  A: 

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.

Michael Edwards
For some reason Stack overflow missed the parameter name out. Edited the question now. Thanks for the link. Although still not working ;(
Doozer1979
+4  A: 

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>
0xA3
Thanks Divo. Still not working though.Am i referring to the paramter correctly in the XSL with this line?<xsl:for-each select="PosDets[@OC_Man = $Boss_ID]">
Doozer1979
See my update. If it is still not working please post your input document. Maybe the problem is with that.
0xA3
Thanks Divo, that indeed did produce the desired parameter, but i can't understand why it isn't working for my document.
Doozer1979
Again thanks divo. If i manually add the parameter i want (ie; swap Boss_ID with "9" and make the necessary changes to the method then the doc comes out fine. I'll add the input doc
Doozer1979
Magic! didn't see the edit about defining the parameter outside of the template. MAny many Thanks!
Doozer1979
Are you sure you defined the xsl:param outside the template? Your input document runs fine through the transform for me.
0xA3
Ah, didn't refresh the page. My previous comment is obsolete now.
0xA3