重命名元素并保留属性 [英] Rename Element and retain attributes

查看:37
本文介绍了重命名元素并保留属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to rename the mattext node to text, but retain its attributes and all the child node/attributes

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

输出

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

我使用了以下 xsl:

I have used the following 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>

但是它没有保留初始的 fontface 属性,并且似乎输出了去除标签的纯文本

But it does not retain the initial fontface attribute and seems to output plain text stripping the tags

推荐答案

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

I can understand your result if that is your complete XSLT. You are inly matching one element, the <mattext>. All others are handled by the default behavior which is to copy the text nodes. I guess you want an Identity Transformation with a special handling of the <mattext> element:

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

这篇关于重命名元素并保留属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆