0

我是 XSLT 转换的新手,被这个递归映射困住了。

<Element1>
  <Element11/>
  <Element12/>
  <Element13/>
  <Element1>
     <Element11/>
     <Element12/>
     <Element13/>
  </Element1>
</Element1>

会变身

<Information>
 <Element11/>
 <Element12/>
 <Element13/>
</Information>
<!-- This will be the child Element1 -->
<Metadata>
 <Element11/>
 <Element12/>
 <Element13/>
</Metadata>

绝对我不能使用:

<xsl:template match="/">
            <xsl:for-each select="Element1">
                <Information>
            </xsl:for-each>
    </xsl:template>
4

1 回答 1

1

这应该做的工作:

<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>

正如 Tim 所说,结果不是有效的 XML,因为它有两个根元素。要生成额外root元素以使输出有效 XML,请添加此模板:

<xsl:template match="/">
  <root>
    <xsl:apply-templates></xsl:apply-templates>
  </root>
</xsl:template>
于 2013-09-19T14:47:07.977 回答