該樣式也將工作:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" />
<!--Identity template to copy content forward-->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<!--specialized template for elements with an "n" attribute-->
<xsl:template match="*[@n]">
<xsl:element name="{@n}">
<xsl:apply-templates select="node()"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
在identity template第一模板,該模板上匹配@*
(這意味着任何屬性)和node()
(這意味着任何元素,文本節點,處理指令或註釋)。默認行爲是複製匹配的項目,然後apply-templates
到它的任何屬性或子節點。
如果在樣式表中沒有聲明其他模板,那麼該標識模板將匹配所有內容,並且XSLT將簡單地複製XML文檔。
第二個模板匹配*[@n]
,它是任何具有「n」屬性的元素。 *
是任何元素的通配符匹配。方括號是謂詞過濾器,其行爲類似於SQL WHERE子句。謂詞中指定的任何條件都必須爲真,以便模板匹配選擇適合的任何條件。謂詞過濾器根據匹配項目的上下文進行評估,因此假設您「站立」在一個元素上,它是否具有@n
?如果是這樣,那麼這個模板匹配。
第二個模板比第一個「標識」模板具有更具體的匹配標準,所以它會「勝出」並匹配具有@n
的元素。
用於第二模板的邏輯是創建具有@n
的值的名稱和元素那麼對於任意子node()
的apply-templates
(它包括text()
和元素,但不包括屬性)。
是的。是的你可以。 – amphetamachine 2010-09-19 10:20:14
可以請你發帖的方法 – new 2010-09-19 10:21:40