用订单翻译XML节点

时间:2015-01-09 08:49:01

标签: xml xslt

XML输入:

<a>
    <b>text_b</b>
    <d>text_d</d>
    <c>text_c</c>
    <e>text_e</e>
    <d>text_d</d>
    <f>text_f</f>
</a>
<g>
    <b>text_b</b>
    <d>text_d</d>
    <c>text_c</c>
    <e>text_e</e>
    <d>text_d</d>
    <f>text_f</f>
</g>

期望的输出:

<aa>
    <aaa>
        <some_extra_tag>some_text</some_extra_tag>
        <aab>text_b</aab>
    </aaa>
    <aad>text_d</aad>
    <aaa>
        <some_extra_tag>some_text</some_extra_tag>
        <aac>text_c</aac>
        <aae>text_e</aae>
    </aaa>
    <aad>text_d</aad>
    <aaa>
        <some_extra_tag>some_text</some_extra_tag>
        <aaf>text_f</aaf>
    </aaa>
</aa>
<gg>
    <aab>text_b</aab>
    <ggg>
        <some_extra_tag2>some_text</some_extra_tag2>
        <aad>text_d</aad>
    </ggg>
    <aac>text_c</aac>
    <aae>text_e</aae>
    <ggg>
        <some_extra_tag2>some_text</some_extra_tag2>
        <aad>text_d</aad>
    </ggg>
    <aaf>text_f</aaf>
</gg>

我可以使用XSLT将上面的XML输入转换为所需的输出吗? 限制: some_extra_tag-Tag应由a-Tag-Template生成,some_extra_tag2-Tag由g-Tag-Template生成。

2 个答案:

答案 0 :(得分:2)

你想要做的是什么(我认为)有点棘手,但我相信以下应该这样做。它的工作原理是使用递归模板:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

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

  <xsl:template match="outer">
    <xsl:copy>
      <xsl:apply-templates select="*[not(self::inner)] |
                                   inner[not(preceding-sibling::*[1][self::inner])]" />
    </xsl:copy>
  </xsl:template>

  <xsl:template match="inner[not(preceding-sibling::*[1][self::inner])]">
    <span>
      <xsl:call-template name="Copy" />
      <xsl:apply-templates select="following-sibling::*[1][self::inner]" />
    </span>
  </xsl:template>

  <xsl:template match="inner">
    <xsl:call-template name="Copy" />
    <xsl:apply-templates select="following-sibling::*[1][self::inner]" />
  </xsl:template>

</xsl:stylesheet>

在此输入上运行时:

<outer>
  <inner>text first</inner>
  <inner>text second</inner>
  <middle>text third</middle>
  <inner>text fourth</inner>
</outer>

它产生结果:

<outer>
  <span>
    <inner>text first</inner>
    <inner>text second</inner>
  </span>
  <middle>text third</middle>
  <span>
    <inner>text fourth</inner>
  </span>
</outer>

答案 1 :(得分:0)

XSLT

  <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
        <xsl:output method="xml" indent="yes"/>

      <xsl:template match="outer">
        <doc>
          <xsl:apply-templates select="*" />
        </doc>
      </xsl:template>


      <xsl:template match="*">
        <outer>
          <span>
            <xsl:copy-of select="."/>
          </span>
        </outer>
      </xsl:template>

    </xsl:stylesheet>

输出:

<?xml version="1.0" encoding="utf-8"?>
<doc>
  <outer>
    <span>
      <inner>text first</inner>
    </span>
  </outer>
  <outer>
    <span>
      <middle>text second</middle>
    </span>
  </outer>
  <outer>
    <span>
      <inner>text third</inner>
    </span>
  </outer>
</doc>