一種方法是對的AName各種元素匹配模板。
AName元件必須跟一個逗號必須具有2個或更多以下AName元素:
<xsl:template match="AName[following-sibling::AName[following-sibling::AName]]">
其他AName具有未選擇了一個以下AName元件元件在第一場比賽後,必須要有一個「和」而不是一個逗號,在他們之後。
<xsl:template match="AName[following-sibling::AName]">
所有其他AName元素與是在列表的最後,等什麼都沒有下。
因此,考慮到下面的XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/>
<xsl:template match="Authors">
<xsl:apply-templates select="@*|node()"/>
</xsl:template>
<xsl:template match="book">
<p>
<xsl:apply-templates select="@*|node()"/>
</p>
</xsl:template>
<xsl:template match="bName">
<b>
<xsl:value-of select="."/>
</b>
<br/>
</xsl:template>
<xsl:template match="AName">
<xsl:value-of select="."/>
</xsl:template>
<xsl:template match="AName[following-sibling::AName]">
<xsl:value-of select="."/>
<xsl:text> and </xsl:text>
</xsl:template>
<xsl:template match="AName[following-sibling::AName[following-sibling::AName]]">
<xsl:value-of select="."/>
<xsl:text>, </xsl:text>
</xsl:template>
</xsl:stylesheet>
當應用於以下XML
<Authors>
<book>
<bName>HTML5</bName>
<AName>John</AName>
<AName>James</AName>
<AName>Jack</AName>
</book>
<book>
<bName>Java</bName>
<AName>Joe</AName>
</book>
<book>
<bName>XML</bName>
<AName>John</AName>
<AName>James</AName>
</book>
</Authors>
以下HTML輸出
<p><b>HTML5</b><br>John, James and Jack</p>
<p><b>Java</b><br>Joe</p>
<p><b>XML</b><br>John and James</p>
待辦事項的匹配模板的順序在XSLT中很重要。最具體的情況必須在更一般的情況之後出現。
很好...並且非常感謝... Hv還沒有嘗試過。會讓你知道結果。再次感謝.. :) - 約翰 – John