Rename Element and retain attributes

2019-07-15 17:40发布

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

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

Output

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

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>

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

标签: xml xslt
1条回答
老娘就宠你
2楼-- · 2019-07-15 18:12

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>
查看更多
登录 后发表回答