我有以下 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/