重命名元素并保留属性 [英] Rename Element and retain attributes
问题描述
我想将 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屋!