你正在尋找的模式是「修改的身份變換」。這種方法的基礎是身份轉換規則,這是下面樣式表中的第一個模板規則。之後的每條規則都表示複製行爲的例外。
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- By default, copy all nodes unchanged -->
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<!-- But strip out <foo> elements (including their content) -->
<xsl:template match="foo"/>
<!-- For <bar> elements, strip out start & end tags, but leave content -->
<xsl:template match="bar">
<xsl:apply-templates/>
</xsl:template>
<!-- For <bat> elements, insert an attribute and append a child -->
<xsl:template match="bat">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:attribute name="id">123</xsl:attribute>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
對我而言,最不滿意的是上面的模板規則中的邏輯重複。這只是添加一個屬性的很多代碼。想象一下,如果我們需要一堆這些。這裏是另一種方法,使我們能夠在我們要覆蓋哪些更多的手術精確:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- By default, copy all nodes unchanged -->
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates mode="add-atts" select="."/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<!-- By default, don't add any attributes -->
<xsl:template mode="add-atts" match="*"/>
<!-- For <bat> elements, insert an "id" attribute -->
<xsl:template mode="add-atts" match="bat">
<xsl:attribute name="id">123</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
最後,這可以更進一步使用各種編輯的不同的方式進行,你可能想使:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- For <bat> elements, insert an "id" attribute -->
<xsl:template mode="add-atts" match="bat">
<xsl:attribute name="id">123</xsl:attribute>
</xsl:template>
<!-- Append <new-element/> to <bat> -->
<xsl:template mode="append" match="bat">
<new-element/>
</xsl:template>
<!-- Insert an element in <foo> content -->
<xsl:template mode="insert" match="foo">
<inserted/>
</xsl:template>
<!-- Add content before the <bar/> and <bat/> elements -->
<xsl:template mode="before" match="bar | bat">
<before-bat-and-bar/>
</xsl:template>
<!-- Add content only after <bat/> -->
<xsl:template mode="after" match="bat">
<after-bat/>
</xsl:template>
<!-- Here's the boilerplate code -->
<!-- By default, copy all nodes unchanged -->
<xsl:template match="@* | node()">
<xsl:apply-templates mode="before" select="."/>
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates mode="add-atts" select="."/>
<xsl:apply-templates mode="insert" select="."/>
<xsl:apply-templates/>
<xsl:apply-templates mode="append" select="."/>
</xsl:copy>
<xsl:apply-templates mode="after" select="."/>
</xsl:template>
<!-- By default, don't add anything -->
<xsl:template mode="add-atts" match="*"/>
<xsl:template mode="insert" match="*"/>
<xsl:template mode="append" match="*"/>
<xsl:template mode="before" match="@* | node()"/>
<xsl:template mode="after" match="@* | node()"/>
</xsl:stylesheet>
在XSLT 2.0,一些樣板可以稍作簡化,由於多模模板規則:
<!-- By default, don't add anything -->
<xsl:template mode="add-atts
insert
append
before
after" match="@* | node()"/>
我有時會使用所有這些自定義模式在相同的樣式表中,但更多的時候我會懶惰地添加它們 - 根據需要。
那麼,xslt有一個「另一個xslt命名空間」,你可以使用它! – alamar 2009-06-05 19:06:02
http://www.w3.org/1999/XSL/Transform是一個普通的命名空間,而http://www.w3.org/1999/XSL/TransformAlias是另一個:) – alamar 2009-06-05 19:10:35