2013-07-29 185 views
2

我想重命名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屬性,似乎輸出明文剝離標籤

回答

3

我能理解你的結果,如果這是你的完整的XSLT。您正在匹配一個元素,即< mattext>。所有其他處理都由複製文本節點的默認行爲處理。我想你想Identity Transformation與< mattext>元素:

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

完美的作品!謝謝 – Rob