【发布时间】:2014-08-20 18:53:16
【问题描述】:
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 获取该节点,比如计数是
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();
}
【问题讨论】:
-
执行 XSLT 的代码在哪里?
-
这是个问题,我要的需要在函数节点中填写。我如何让 xslt 从函数中知道这个参数计数。
-
我们可以帮助您弄清楚如何在执行时将
count数字传递到 XSLT,但在此之前,您需要编写实际执行 XSLT 的代码。此时,您只是要求我们为您完成所有工作。 -
我试图让示例 xml 简单明了,但现在为您准备了完整的代码
-
感谢您发布代码。请看下面我的回答。您没有提供太多 XSLT 示例,因此很难就这一点提供非常详细的指导,但必要的 C# 代码就在那里。