转型XSLT递归嵌套模板(Transformation in XSLT with Recursive

2019-10-30 09:56发布

我有这个输入输入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>

Answer 1:

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>



文章来源: Transformation in XSLT with Recursive Nested Templates