重命名元素并保留属性

Rob*_*Rob 2 xml xslt

我想将 mattext 节点重命名为 text,但保留其属性和所有子节点/属性

输入 XML

<material>
  <mattext fontface="Tahoma">
    <p style="white-space: pre-wrap">
      <font size="11">Why are the astronauts in the video wearing special suits? (Select two)</font>
    </p>
  </mattext>
</material>
Run Code Online (Sandbox Code Playgroud)

输出

<material>
  <text fontface="Tahoma">
    <p style="white-space: pre-wrap">
      <font size="11">Why are the astronauts in the video wearing special suits? (Select two)</font>
    </p>
  </text>
</material>
Run Code Online (Sandbox Code Playgroud)

我使用了以下 xsl:

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


<!-- Build stem -->
<xsl:template match="mattext">
    <text>
        <!-- Option text -->
        <xsl:call-template name="content"/>
    </text>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)

但它不保留初始 fontface 属性,并且似乎输出剥离标签的纯文本

for*_*two 5

如果这是您完整的 XSLT,我可以理解您的结果。您仅匹配一个元素,即 <mattext>。所有其他内容均由默认行为处理,即复制文本节点。我猜你想要一个对 <mattext> 元素进行特殊处理的身份转换:

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

<xsl:template match="mattext">
    <text>
        <xsl:apply-templates select="@* | node()" />
    </text>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)