由於@DevNull表明您使用的身份轉換爲更容易和更簡潔。總之,這裏是for-each
並沒有apply-templates
你要求一個可能的解決方案:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/root">
<root>
<xsl:for-each select="child::node()">
<xsl:choose>
<xsl:when test="position()=last()-1"/>
<xsl:otherwise>
<xsl:copy>
<xsl:copy-of select="@att"/>
<xsl:copy-of select="child::node()"/>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</root>
</xsl:template>
注意有關使用身份變換
如果情況真的是什麼樣子,我意思是元素的未知名稱,@DevNull將不起作用,並且您需要更像此類的東西:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="root/child::node()[position()=last()]|@att2"/>
</xsl:stylesheet>
即使使用最後一個元素e4
或e1000
,此解決方案也可以工作。
你爲什麼喜歡用'for-each'而不是'apply-templates'? – 2011-05-12 23:53:16
@lwburk - 我認爲「我對xsl:element和xsl:attribute有一些問題......」指向一些更深層根源的問題。 – 2011-05-13 00:09:35
目前還不清楚您是在尋找一個通用解決方案(未知元素名稱)還是特定的解決方案(過濾器'e3')。我提供的答案在前一種情況下將對您有所幫助,即使它很容易適應特定情況。 – 2011-05-13 05:46:29