【问题标题】:How to get a particular node from xml to xslt如何从 xml 获取特定节点到 xslt
【发布时间】: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# 代码就在那里。

标签: c# xml xslt


【解决方案1】:

您需要在 XSLT 中的 xsl:stylesheet 元素内定义一个参数:

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

【讨论】:

    【解决方案2】:

    是否有类似基于计数的东西,我可以告诉 xslt 获取那个节点,说count是1,那么我可以让xslt知道吗 需要打印第一个 OptionCd 等等。

    在 XSLT 中,您可以使用:

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

    从您的 XML 示例中返回值“SUB”。如果在运行时将名为“count”的参数传递给样式表,那么

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

    将选择第 n 个 Option 节点以从中获取 OptionCd 值(其中 n = $count 参数)。

    --
    注意:您发布的 XSLT 不是。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-29
    • 1970-01-01
    相关资源
    最近更新 更多