一种方法是对的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