Rename Element and retain attributes

霸气de小男生 提交于 2020-01-14 03:01:45

问题


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


回答1:


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>


来源:https://stackoverflow.com/questions/17920639/rename-element-and-retain-attributes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!