目前我还是一个新手,但是我已经使用xsl格式化了一个xml提要,以便将其输出到我的站点的html中。但是,我想更进一步,将一些输出文本转换为html链接。
有没有可以帮助你的教程?
为了给出一个更好的背景,输出是一个足球排行表,我想让球队的名称自动链接到一个网址。因此,如果name =‘朴茨茅斯’,那么我会希望朴茨茅斯成为一个我会确定的链接。如何设置下表的格式,以便对所有可能不同的团队名称执行此操作?
<xsl:for-each select="team">
<tr>
<td><xsl:value-of select="position"/></td>
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="played"/></td>
<td><xsl:value-of select="won"/></td>
<td><xsl:value-of select="drawn"/></td>
<td><xsl:value-of select="lost"/></td>
<td><xsl:value-of select="for"/></td>
<td><xsl:value-of select="against"/></td>
<td><xsl:value-of select="goalDifference"/></td>
<td><xsl:value-of select="points"/></td>
</tr>`
发布于 2012-08-04 04:17:13
如果你想有条件地输出一个标签,你可以执行以下操作。
<xsl:template match="/">
<xsl:apply-templates select="//team"/>
</xsl:template>
<xsl:template match="team">
<td>
<xsl:value-of select="position"/>
</td>
<td>
<xsl:choose>
<xsl:when test="name='Portsmouth'">
<a>
<xsl:attribute name="href">
<xsl:value-of select="concat('someurl.com?name=',name)"/>
</xsl:attribute>
</a>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="name"/>
</xsl:otherwise>
</xsl:choose>
</td>
<td>
<xsl:value-of select="played"/>
</td>
<td>
<xsl:value-of select="won"/>
</td>
<td>
<xsl:value-of select="drawn"/>
</td>
<td>
<xsl:value-of select="lost"/>
</td>
<td>
<xsl:value-of select="for"/>
</td>
<td>
<xsl:value-of select="against"/>
</td>
<td>
<xsl:value-of select="goalDifference"/>
</td>
<td>
<xsl:value-of select="points"/>
</td>
</xsl:template>使用应用模板而不是foreach循环。
如果其中一支球队是朴次茅斯,则输出将为
<td><a href="someurl.com?name=Portsmouth"/></td>如果您希望每个团队都有一个url,那么只需删除choose语句并离开
<td>
<a>
<xsl:attribute name="href">
<xsl:value-of select="concat('someurl.com?name=',name)"/>
</xsl:attribute>
</a>
</td>https://stackoverflow.com/questions/11802535
复制相似问题