使用递归嵌套模板在XSLT中进行转换

时间:2013-06-28 17:37:48

标签: xslt recursion transformation

我有这个Entry Input XML

<BusinessInteractionTypes>
   <BusinessInteractionType>
     <TypeId>123</TypeId>
     <Name>Foo</Name>
     <Description>XSLT</Description>
   </BusinessInteractionType>
   ...a lot BusinessInteractionType Elements
</BusinessInteractionTypes>

但是有很多转换(对于SOA案例),我需要使用XSLT将此输入转换为:

<businessInteractionTypes>
  <businessInteractionType>
   <BusinessInteractionType>   <!-- Just first input node -->
     <TypeId/>
   </BusinessInteractionType>
  </businessInteractionType>
  <businessInteractionTypeHas> <!-- Recursive interaction began -->
    <businessInteractionType>
      <BusinessInteractionType>
       <TypeId />
      </BusinessInteractionType>

      <businessInteractionTypeHas>
       ... recursive
    </businessInteractionType>
  </businessInteractionTypeHas>
</businessInteractionTypes>

1 个答案:

答案 0 :(得分:0)

Srs,我解决了这个问题......跟随 谢谢大家

<?xml version="1.0" encoding="UTF-8"?>

         

<xsl:template match="cus:BusinessInteractionType">
    <xsl:call-template name="Master">
      <xsl:with-param name="nodeD" select="bus1:BusinessInteractionType"/>
  </xsl:call-template>
</xsl:template>

<xsl:template name="Master">
    <xsl:param name="nodeD" select="bus1:BusinessInteractionType"/>

    <cas:businessInteractionTypes>
        <cas:businessInteractionType>
            <xsl:if test="bus1:BusinessInteractionType[position() = 1]">
                <bus3:BusinessInteractionType>
                    <bus1:TypeId>
                        <xsl:value-of select="//bus1:TypeId"/>
                    </bus1:TypeId>
                </bus3:BusinessInteractionType>
            </xsl:if>
            <xsl:call-template name="MasterNested">
                <xsl:with-param name="current_node" select="bus1:BusinessInteractionType[position()>1]"/>
            </xsl:call-template>
        </cas:businessInteractionType>    
    </cas:businessInteractionTypes>
</xsl:template>

<xsl:template name="MasterNested">

    <xsl:param name="current_node" select="bus1:BusinessInteractionType"/>

    <xsl:if test="$current_node">
        <cas:businessInteractionTypeHas>
            <cas:businessInteractionType>
                <bus3:TypeId>
                    <xsl:value-of select="$current_node//bus1:TypeId"/>
                </bus3:TypeId>
                <bus3:Name>
                    <xsl:value-of select="$current_node//bus1:Name"/>
                </bus3:Name>
                <bus3:Description>
                    <xsl:value-of select="$current_node//bus1:Description"/>
                </bus3:Description>
            </cas:businessInteractionType>
            <xsl:call-template name="MasterNested">
                <xsl:with-param name="current_node" select="$current_node/following-sibling::node()"/>
            </xsl:call-template>
        </cas:businessInteractionTypeHas>
    </xsl:if>
</xsl:template>