1
我有一個XML,其中值爲「空標記」的文本節點隨機出現在文件的不同位置。
我的問題是如何用替換其他文本,其中元素(和父節點)的名稱是未知的。我已經創建了一個XSLT文件,看起來有點笨重,我不確定它在轉換時間上的效率。
這是樣品測試XML我已經創建:使用XSLT替換文本節點和其他文本的高效代碼?
<root>
<sub_root>abc</sub_root>
<sub_root>
<child>test value</child>
<child2>test value</child2>
<sub_child>
<node1>data</node1>
<node2>data2</node2>
<node3>
<grand_child>test value</grand_child>
</node3>
<node4>test value</node4>
</sub_child>
</sub_root>
</root>
這是XSLT:
<xsl:template match="@*|*|text()">
<xsl:copy>
<xsl:choose>
<xsl:when test="text()='test value'">
<xsl:apply-templates select="@*|*"/>
<xsl:text>replaced</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="@*|*|text()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:copy>
</xsl:template>
這是所需的輸出,我試圖生成:
<root>
<sub_root>abc</sub_root>
<sub_root>
<child>replaced</child>
<child2>replaced</child2>
<sub_child>
<node1>data</node1>
<node2>data2</node2>
<node3>
<grand_child>replaced</grand_child>
</node3>
<node4>replaced</node4>
</sub_child>
</sub_root>
</root>
可以用更好的方式(以任何方式)寫這段代碼嗎?或者是我的代碼更好?
這是減少我的代碼中的抽象性.. :-) thanx .. – 2010-01-27 07:25:45