首页 > 解决方案 > 使用 XSLT 将父元素添加到不同的 XML 同级

问题描述

我有一个关于 XSLT 2.0 的问题。我处于这样的情况:

<config>
    <!-- elements that needs to be copied as they are -->
    <a>
        <childA attr="attr">text</childA>
    </a>
    <b>
        <childB attrb="attr">text B</childB>
    </b>
    <c>
        <childC attrc="attr">text</childC>
    </c>
</config>

我想要这个:

<config>
    <!-- elements that needs to be copied as they are -->
    <parent>
        <a>
            <childA attr="attr">text</childA>
        </a>
        <b>
            <childB attrb="attr">text B</childB>
        </b>
        <C>
            <childC attrc="attr">text</childC>
        </c>
    </parent>
</config>

我做了很多尝试,但它们基本上都是错误的,我也尝试使用 for-each-group 但绝对我不认为那是方法......你能给我一些关于如何/在哪里寻找的提示吗?

太感谢了!

标签: xmlxslt

解决方案


config

<xsl:template match="config">
  <xsl:copy>
     <xsl:apply-templates select="* except (a, b, c)"/>
     <parent>
       <xsl:apply-templates select="a, b, c"/>
     </parent>
  </xsl:copy>
</xsl:template>

连同恒等变换为起点(即<xsl:mode on-no-match="shallow-copy"/>或同拼写为模板)。


推荐阅读