2

I am in a fix here where I have to pass the dynamically created xml in C# code to be passed to xslt as param and then get the values from it.

following is the sample xslt

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt"
  version="1.0">
  <xsl:param name="Keys"></xsl:param>  

  <xsl:template match="/">
    <MyKey>MYNODE</MyKey>
    <xsl:value-of select="msxsl:node-set($Keys)/Keys/Item/Header"/> 
  </xsl:template>

</xsl:stylesheet>

Then from the code in C# I call Transform method

XslCompiledTransform proc = new XslCompiledTransform();
proc.Load("sheet.xslt");

XsltArgumentList xsltArgs = new XsltArgumentList();

XmlDocument doc1 = new XmlDocument();
// populate as needed e.g.
doc1.LoadXml("<Keys><Item><Header>fooHeader</Header></Item></Keys>");

xsltArgs.AddParam("Keys", "", doc1.InnerXml.ToString());

// pass xsltArgs as second argument to Transform method

proc.Transform(someInput, xsltArgs, someOutput);

Here I am not able to get the value for MYNODE in results Thanks

Rakesh
  • 310
  • 3
  • 19

2 Answers2

2

You have two problems here. Firstly how you pass the parameters

xsltArgs.AddParam("doc1", "", doc1);

But in your XSLT you have it named "Keys"

<xsl:param name="Keys"></xsl:param>  

Therefore you need to change your C# code

xsltArgs.AddParam("Keys", "", doc1);

There is also a problem with your XSLT.

<xsl:value-of select="msxsl:node-set($Keys)/Keys/item/header"/> 

XML is case-sensitive. Your XML contains "Item", but XSLT is looking for "item". It should be these

<xsl:value-of select="msxsl:node-set($Keys)/Keys/Item/Header"/> 

Infact, I don't think you need node-set here. Try this too

<xsl:value-of select="$Keys/Keys/Item/Header"/> 
Tim C
  • 70,053
  • 14
  • 74
  • 93
  • gives me blank result gives an error that Expression should evaluate to node set Thanks – Rakesh Jun 26 '14 at 09:04
  • In your question you have changed the adding of the parameter to `xsltArgs.AddParam("Keys", "", doc1.InnerXml.ToString());`. Can you try changing it back to just `xsltArgs.AddParam("Keys", "", doc1);`? – Tim C Jun 26 '14 at 10:32
1

Once you have built a document with

XmlDocument doc1 = new XmlDocument();
// populate as needed e.g.
doc1.LoadXml("<Keys><Item><Header>fooHeader</Header></Item></Keys>");

you should pass that document in as the parameter value doing

xsltArgs.AddParam("Keys", "", doc1);

Then you should be able to use that parameter as in

<xsl:param name="Keys"></xsl:param>  

  <xsl:template match="/">
    <MyKey>MYNODE</MyKey>
    <xsl:value-of select="$Keys/Keys/Item/Header"/> 
  </xsl:template>

Complete example, C# is

        XslCompiledTransform proc = new XslCompiledTransform();
        proc.Load("../../XSLTFile1.xslt");

        XsltArgumentList xsltArgs = new XsltArgumentList();

        XmlDocument doc1 = new XmlDocument();
        // populate as needed e.g.
        doc1.LoadXml("<Keys><Item><Header>fooHeader</Header></Item></Keys>");

        xsltArgs.AddParam("Keys", "", doc1);

        proc.Transform(new XmlDocument(), xsltArgs, Console.Out);

XSLT is

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt"
  version="1.0">
  <xsl:param name="Keys"></xsl:param>

  <xsl:template match="/">
    <MyKey>MYNODE</MyKey>
    <xsl:value-of select="$Keys/Keys/Item/Header"/>
  </xsl:template>

</xsl:stylesheet>

the output to the console is

<?xml version="1.0" encoding="ibm850"?><MyKey xmlns:msxsl="urn:schemas-microsoft
-com:xslt">MYNODE</MyKey>fooHeader
Martin Honnen
  • 160,499
  • 6
  • 90
  • 110