在 XSLT 中使用 for 循环 [英] Using for loop in XSLT

查看:51
本文介绍了在 XSLT 中使用 for 循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下格式的 xml:

I have an xml in the following format:

<?xml version="1.0" encoding="UTF-8"?>
<response>
   <cases>
      <case>CASE-ONE</case>
      <case>CASE-TWO</case>
   </cases>
   <results>
      <Final-Results>
         <issues>
            <row>
               <IKEY>2014-03-26-05.22.22.193840T01</IKEY>
               <PRTY>999</PRTY>
            </row>
         </issues>
      </Final-Results>
      <Final-Results>
         <issues>
            <row>
               <IKEY>2014-03-26-05.05.51.077840T01</IKEY>
               <PRTY>999</PRTY>
            </row>
            <row>
               <IKEY>2014-03-26-05.10.51.077840T01</IKEY>
               <PRTY>999</PRTY>
            </row>
         </issues>
      </Final-Results>
   </results>
</response>

现在,我想使用 XSLT 将上述 xml 转换为以下格式:

Now, I want to convert the above xml to the following format using XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<response>
   <cases>
      <case>
         CASE-ONE
         <issues>
            <row>
               <IKEY>2014-03-26-05.22.22.193840T01</IKEY>
               <PRTY>999</PRTY>
            </row>
         </issues>
      </case>
      <case>
         CASE-TWO
         <issues>
            <row>
               <IKEY>2014-03-26-05.05.51.077840T01</IKEY>
               <PRTY>999</PRTY>
            </row>
            <row>
               <IKEY>2014-03-26-05.10.51.077840T01</IKEY>
               <PRTY>999</PRTY>
            </row>
         </issues>
      </case>
   </cases>
</response>

这个想法是在第一个 之间移动内容.</issues> 标记到第一个 </case> 标签和第二个 </issues> 标签到第二个标签,依此类推.这里标签和标签的数量是不确定的.所以,我想我必须在这里使用 for 循环之类的东西.老实说,我不是使用 XSLT 的专家.如果您能提供解决方案,那就太好了.如果我的问题不清楚,请随时添加评论,以便我可以用更好的词来表达.提前致谢.

The idea is to move the content between the first <issues> </issues> tags to the first <case> </case> tags and the second <issues> </issues> tags to the second tags and so on. Here the number of <case> tags and <issues> tags are uncertain. So,I think I have to use for loop kind of thing here.Honestly, I am not expert in using XSLT. It would be great if you can provide a solution. Please feel free to add a comment if my question is not clear so that I can put it in better words. Thanks in advance.

推荐答案

无需使用 for 循环.请改用 身份转换xsl:apply-templates.

No need to use a for loop. Use the identity transform and xsl:apply-templates instead.

XML 输入

<response>
    <cases>
        <case>CASE-ONE</case>
        <case>CASE-TWO</case>
    </cases>
    <results>
        <Final-Results>
            <issues>
                <row>
                    <IKEY>2014-03-26-05.22.22.193840T01</IKEY>
                    <PRTY>999</PRTY>
                </row>
            </issues>
        </Final-Results>
        <Final-Results>
            <issues>
                <row>
                    <IKEY>2014-03-26-05.05.51.077840T01</IKEY>
                    <PRTY>999</PRTY>
                </row>
                <row>
                    <IKEY>2014-03-26-05.10.51.077840T01</IKEY>
                    <PRTY>999</PRTY>
                </row>
            </issues>
        </Final-Results>
    </results>
</response>

XSLT 1.0

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="/*">
        <xsl:copy>
            <xsl:apply-templates select="@*|cases"/>
        </xsl:copy>        
    </xsl:template>

    <xsl:template match="case">
        <xsl:variable name="pos" select="position()"/>
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
            <xsl:apply-templates select="/*/results/Final-Results[position()=$pos]/issues"/>
        </xsl:copy>        
    </xsl:template>

</xsl:stylesheet>

XML 输出

<response>
   <cases>
      <case>CASE-ONE<issues>
            <row>
               <IKEY>2014-03-26-05.22.22.193840T01</IKEY>
               <PRTY>999</PRTY>
            </row>
         </issues>
      </case>
      <case>CASE-TWO<issues>
            <row>
               <IKEY>2014-03-26-05.05.51.077840T01</IKEY>
               <PRTY>999</PRTY>
            </row>
            <row>
               <IKEY>2014-03-26-05.10.51.077840T01</IKEY>
               <PRTY>999</PRTY>
            </row>
         </issues>
      </case>
   </cases>
</response>

这篇关于在 XSLT 中使用 for 循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆