0

The XML has repetitive node OptionCd

<AdjusterParty AdjusterPartyIdRef="20130000074-001">

    <Option>
       <OptionCd>SAL</OptionCd>
            <OptionValue>N</OptionValue>
    </Option>
    <Option>
    <OptionCd>SUB</OptionCd>
      <OptionValue>N</OptionValue>
   </Option>

</AdjusterParty>

The xslt is:

<w:p >

  <w:r>

   <w:t>

    <xsl:value-of select ="OptionCd"/>

</w:r>

</w:p>

The requirement is based on the parameter value passed in the function I would either populate the OptionCD SAL or SUB. So how to set the value in OptionCD.

The C# code is

function node(int count)

{

}  

is there something like based on the count, I can tell the xslt to fetch that node, say count is

1, then could I let the xslt know it need to print the first OptionCd and so on.

Thanks

Updated the code which I use to read the xml and xslt from a template document and then generates the word document with the values.

        string rootPath = @"C:\ExampleWordProcessingML\Docs";
        string xmlDataFile = rootPath + @"\Original.xml";
        string xsltFile = rootPath + @"\Transactions.xslt";
        string templateDocument = rootPath + @"\Transactions.docx";
        string outputDocument = rootPath + @"\MyTransactions.docx";

        //Create a writer for the output of the Xsl Transformation.
        StringWriter stringWriter = new StringWriter();
        XmlWriter xmlWriter = XmlWriter.Create(stringWriter);

        //Create the Xsl Transformation object.
        XslCompiledTransform transform = new XslCompiledTransform();
        transform.Load(xsltFile);
        //Transform the xml data into Open XML 2.0 Wordprocessing format.
        transform.Transform(xmlDataFile, xmlWriter);

        //Create an Xml Document of the new content.
        XmlDocument newWordContent = new XmlDocument();
        newWordContent.LoadXml(stringWriter.ToString());

        //Copy the Word 2007 source document to the output file.
        System.IO.File.Copy(templateDocument, outputDocument, true);
        //Use the Open XML SDK version 2.0 to open the output 
        //  document in edit mode.
        using (WordprocessingDocument output =
        WordprocessingDocument.Open(outputDocument, true))
        {
            //Using the body element within the new content XmlDocument
            //  create a new Open Xml Body object.
            Body updatedBodyContent =
              new Body(newWordContent.DocumentElement.InnerXml);

            //Replace the existing Document Body with the new content.
            output.MainDocumentPart.Document.Body = updatedBodyContent;

            //Save the updated output document.
            output.MainDocumentPart.Document.Save();
        }
resthere
  • 129
  • 3
  • 13
  • Where's the code where you're executing the XSLT? – JLRishe Aug 20 '14 at 18:57
  • This is the question,what I want needs to be filled in the function node. how do I let the xslt know this parameter count from the functin. – resthere Aug 20 '14 at 19:04
  • We can help you figure out how to pass the `count` number into the XSLT when you execute it, but before that happens, you need to do the work of writing code that actually executes the XSLT. At this point, you're just asking us to do all of the work for you. – JLRishe Aug 20 '14 at 19:30
  • I tried to keep the sample xml simple and to the point but now have the entire code for you – resthere Aug 20 '14 at 19:43
  • Thanks for posting your code. Please see my answer below. You didn't provide much example XSLT, so it's hard to provide very detailed guidance on that point, but the necessary C# code is there. – JLRishe Aug 20 '14 at 19:57

2 Answers2

0

is there something like based on the count, I can tell the xslt to fetch that node, say count is 1, then could I let the xslt know it need to print the first OptionCd and so on.

In XSLT, you can use:

<xsl:value-of select="/AdjusterParty/Option[2]/OptionCd"/>

to return the value "SUB" from your XML example. If you pass a parameter named "count" to the stylesheet at runtime, then

<xsl:value-of select="/AdjusterParty/Option[$count]/OptionCd"/>

will select the n-th Option node to get the OptionCd value from (where n = $count parameter).

--
Note: what you posted as XSLT is not.

michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
  • can you give example of "pass parameter named "count" to the stylesheet at runtime, " – resthere Aug 20 '14 at 19:47
  • @resthere Actually, I know nothing about that (in C#), but it shouldn't be too hard to discover - see http://msdn.microsoft.com/en-us/library/dfktf882%28v=vs.110%29.aspx or http://stackoverflow.com/questions/1521064/passing-parameters-to-xslt-stylesheet-via-net for example. – michael.hor257k Aug 20 '14 at 19:52
0

You need to define a parameter in your XSLT, inside the xsl:stylesheet element:

<xsl:param name="count" select="1" />

And then you can use something like this in your XSLT:

<xsl:value-of select="/AdjusterParty/Option[$count]/OptionCd"/>

although this might be a bit safer, in case count was passed in as a string value:

<xsl:value-of select="/AdjusterParty/Option[number($count)]/OptionCd"/>

In order to pass the count value into your XSLT, you can do this:

// Create the XsltArgumentList.
XsltArgumentList argList = new XsltArgumentList();
argList.AddParam("count", "", count);    

//Transform the xml data into Open XML 2.0 Wordprocessing format.
transform.Transform(xmlDataFile, argList, xmlWriter);
JLRishe
  • 99,490
  • 19
  • 131
  • 169