Xslt 移动节点到父节点

Rne*_*net 2 xml xslt xslt-2.0

我有以下xml结构:

<root>
 <a>
   <b>
     <c>
       <c1>123</c1>
       <c2>abc</c2>
     </c>
     <d/>
     <e/>
     <f/>
   </b>
 </a>
</root>
Run Code Online (Sandbox Code Playgroud)

如何删除<b>但保留<c>a 下的子节点,例如

<root>
 <a>
   <c>
     <c1>123</c1>
     <c2>abc</c2>
   </c>
 </a>
</root>
Run Code Online (Sandbox Code Playgroud)

Mat*_*ler 5

使用身份转换但有例外。这个额外的模板

<xsl:template match="a/*[not(self::c)]">
Run Code Online (Sandbox Code Playgroud)

如果元素是 的子元素a,但未命名为“c”,则匹配该元素。如果是这种情况,则该元素及其所有内容都会被忽略。

编辑:您更改了要求,我更改了代码和输出。现在,b元素遍历和所有子元素b都将被忽略,除了c。

样式表

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="xml" encoding="UTF-8" indent="yes" />

    <xsl:strip-space elements="*"/>

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

    <xsl:template match="b">
        <xsl:apply-templates select="@*|node()"/>
    </xsl:template>

    <xsl:template match="b/*[not(self::c)]"/>

</xsl:transform>
Run Code Online (Sandbox Code Playgroud)

XML 输入

<root>
 <a>
   <b>
     <c>
       <c1>123</c1>
       <c2>abc</c2>
     </c>
     <d/>
     <e/>
     <f/>
   </b>
 </a>
</root>
Run Code Online (Sandbox Code Playgroud)

XML 输出

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <a>
      <c>
         <c1>123</c1>
         <c2>abc</c2>
      </c>
   </a>
</root>
Run Code Online (Sandbox Code Playgroud)