c# - 如何在 XSLT 中进行循环?

标签 c# .net xml xslt

我有以下 XML 结构:

    <Article>
         <id>1</id>
         <line>L11</line>
         <line>L12</line>
         <line>L13</line>
     </Article>
    <Article>
         <id>2</id>
         <line>L21</line>
         <line>L22</line>
         <line>L23</line>
     </Article>

我想使用 XSLT 一次只遍历一篇文章的所有行,这样我就可以实现以下结构:(每篇文章都转换为订单,其行在新的中转换为行结构结构)

        <orders>
           <order>
              <id>1</id>
              <order_line>L11</order_line>
              <order_line>L12</order_line>
              <order_line>L13</order_line>
            </order>
           <order>
              <id>2</id>
              <order_line>L21</order_line>
              <order_line>L22</order_line>
              <order_line>L23</order_line>
            </order>
        </orders>

最佳答案

使用 XSLT,尝试将手头的任务视为您必须匹配的一组模式或规则,以及每次遇到此类模式时要采取的操作。您通常可以让运行时担心循环等问题,以发现模式。

在您的具体情况下,您描述了两种需要特殊逻辑的模式。每次出现元素 Article 时,您都希望应用规则将其名称更改为 order。 每次遇到元素 line 作为 Article 的子元素时,将其替换为 order_line。对于任何其他模式,您只想复制原始文档中的内容:

<!-- Match element Article, and whenever it's encountered, insert an 'order' element, and copy the contents of Article -->
<xsl:template match="Article">
  <order> <xsl:apply-templates/> </order>
</xsl:template>

<!-- Match element 'line', and whenever it's encountered, insert an 'order_line' element, and copy the contents -->
<xsl:template match="Article/line">
  <order_line> <xsl:apply-templates/> </order_line>
</xsl:template>

<!-- Match any other element we haven't specified explicity, and copy it verbatim -->
<xsl:template match="node()">
  <xsl:copy>
    <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>

关于c# - 如何在 XSLT 中进行循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5303728/

相关文章:

c# - ASP.Net 表单抛出 SQL 异常

c# - 从 getType().GetProperties() 中排除属性

c# - 确保 streamreader 不会挂起等待数据

python - odoo TreeView 中无法识别的字段

c++ - RapidXML 以深度优先模式解析 XML

c# - 在输入 json 中将值 {null} 转换为类型 'System.DateTime' 时出错

c# - WPF多个控件OutOfMemory

c# - Ranorex 测试自动化问题 : Unable to reliably click a button on silverlight web app

c# - .NET 服务器调度服务 - 我应该如何处理这个问题?

java - android.graphics.drawable.NinePatchDrawable 无法转换为 android.graphics.drawable.TransitionDrawable”