XML具有重复节点OptionCd
<AdjusterParty AdjusterPartyIdRef="20130000074-001">
<Option>
<OptionCd>SAL</OptionCd>
<OptionValue>N</OptionValue>
</Option>
<Option>
<OptionCd>SUB</OptionCd>
<OptionValue>N</OptionValue>
</Option>
</AdjusterParty>
xslt是:
<w:p >
<w:r>
<w:t>
<xsl:value-of select ="OptionCd"/>
</w:r>
</w:p>
该要求基于我将填充OptionCD SAL或SUB的函数中传递的参数值。那么如何在OptionCD中设置值。
C#代码是
function node(int count)
{
}
有基于计数的东西,我可以告诉xslt获取该节点,比如count是
1,然后我可以让xslt知道它需要打印第一个OptionCd,依此类推。
由于
更新了我用来从模板文档中读取xml和xslt的代码,然后生成带有值的word文档。
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();
}
答案 0 :(得分:0)
有基于计数的东西,我可以告诉xslt 获取该节点,比如count为1,然后我可以让xslt知道它 需要打印第一个OptionCd等等。
在XSLT中,您可以使用:
<xsl:value-of select="/AdjusterParty/Option[2]/OptionCd"/>
返回值&#34; SUB&#34;来自您的XML示例。如果传递一个名为&#34的参数; count&#34;到运行时的样式表,然后
<xsl:value-of select="/AdjusterParty/Option[$count]/OptionCd"/>
将选择第n个Option
节点以获取OptionCd
值(其中n = $ count参数)。
-
注意:您发布的XSLT不是。
答案 1 :(得分:0)
您需要在xsl:stylesheet
元素内的XSLT中定义一个参数:
<xsl:param name="count" select="1" />
然后你可以在你的XSLT中使用这样的东西:
<xsl:value-of select="/AdjusterParty/Option[$count]/OptionCd"/>
虽然这可能会更安全一些,但如果count
作为字符串值传入:
<xsl:value-of select="/AdjusterParty/Option[number($count)]/OptionCd"/>
为了将count
值传递给您的XSLT,您可以这样做:
// 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);