XSLT:Recursive Mapping

后端 未结 1 346
醉梦人生
醉梦人生 2021-01-27 07:04

I am new to XSLT transformation and got stuck with this recursive mapping.


  
  
  
  <         


        
相关标签:
1条回答
  • 2021-01-27 07:57

    This should do the job:

    <xsl:stylesheet 
      version="1.0" 
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
      <xsl:output method="xml" indent="yes"/>
    
      <!-- Copy everything -->
      <xsl:template match="@* | node()">
        <xsl:copy>
          <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
      </xsl:template>
    
      <!-- Template handling the top-level 'Element1' -->
      <xsl:template match="Element1">
        <Information>
          <!-- Apply the copy template to all sub-elements except 'Element1' -->
          <xsl:apply-templates select="*[name()!='Element1']"/>
        </Information>
        <!-- Apply the templates to the 'Element1' sub-elements -->
        <xsl:apply-templates select="Element1"/>
      </xsl:template>
    
      <!-- Template handling the inner 'Element1' -->
      <xsl:template match="Element1/Element1">
        <Metadata>
          <xsl:apply-templates/>
        </Metadata>
      </xsl:template>
    
    </xsl:stylesheet>
    

    As Tim notes the result is not a valid XML because it has two root elements. To generate an extra root element to make the output valid XML add this template:

    <xsl:template match="/">
      <root>
        <xsl:apply-templates></xsl:apply-templates>
      </root>
    </xsl:template>
    
    0 讨论(0)
提交回复
热议问题