【问题标题】:Rename Element and retain attributes重命名元素并保留属性
【发布时间】:2013-07-29 09:39:13
【问题描述】:

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

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

<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属性,并且似乎输出剥离标签的纯文本

【问题讨论】:

    标签: xml xslt


    【解决方案1】:

    如果那是您的完整 XSLT,我可以理解您的结果。您只匹配一个元素,。所有其他都由复制文本节点的默认行为处理。我猜你想要一个对 元素进行特殊处理的Identity Transformation

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-26
      • 2016-06-19
      • 1970-01-01
      • 1970-01-01
      • 2014-05-20
      • 2013-11-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多