开发者

Embedding XSL code inside an <a> tag

I have some XML data which includes URIs. I want to list the URIs on an ASP page, but also to make them clickable using <a> tags. However XSLT does not allow you to embed an XSL comm开发者_如何学编程and within a tag, eg:

<xsl:for-each select="//uri">
  <tr>
    <td class="tdDetail">
      More information
    </td>
    <td>
      <a href="<xsl:value-of select="." />" alt="More information" target=_blank><xsl:value-of select="." /></a>
    </td>
  </tr>
</xsl:for-each>

How can I include the URL in the <uri> tag after the <a href=" code?


Use the <xsl:attribute> element to have non-fixed attribute.

<a alt="More information" target="_blank">
  <xsl:attribute name="href">
    <xsl:value-of select="." />
  </xsl:attribute>
  <xsl:value-of select="." />
</a>

Edit: As others have mentioned, it is also possible to use attribute value templates:

<a href="{.}" alt="More information" target="_blank">
  <xsl:value-of select="." />
</a>


Use:

<a href="{.}" alt="More information" target="_blank"> 
  <xsl:value-of select="." /> 
</a>

Try to use AVTs (Attribute-Value-Templates) whenever possible (on all attributes except a select attribute). This makes the code shorter and much more readable.


In addition to using <xsl:attribute> (mentioned in KennyTM's answer) it is also possible to use the "{}" shorthand notation when working with attributes:

<a href="{.}"><xsl:value-of select="." /></a>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜