如何使用XSLT创建超链接?

时间:2012-04-17 03:35:33

标签: xslt

我是XSLT的新手。我想使用XSLT创建一个超链接。 应该是这样的:

阅读我们的隐私权政策。

“隐私政策”是链接,点击此链接后,应重定向到示例“www.privacy.com”

有什么想法吗? :)

3 个答案:

答案 0 :(得分:11)

此转化

<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="/">
  <html>
   <a href="www.privacy.com">Read our <b>privacy policy.</b></a>
  </html>
 </xsl:template>
</xsl:stylesheet>

应用于任何 XML文档(未使用)时,会生成所需结果

<html><a href="www.privacy.com">Read our <b>privacy policy.</b></a></html>

,浏览器将其显示为

阅读我们的隐私政策。

现在想象一下,在XSLT样式表中没有任何硬编码 - 而是数据在源XML文档中

<link url="www.privacy.com">
 Read our <b>privacy policy.</b>
</link>

然后进行此转换

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="link">
  <a href="{@url}"><xsl:apply-templates/></a>
 </xsl:template>
</xsl:stylesheet>

应用于上述XML文档时,会生成所需的正确结果

<a href="www.privacy.com">
 Read our <b>privacy policy.</b>
</a>

答案 1 :(得分:6)

如果要从XML文件中读取超链接值,这应该有效:

假设: href 是XML特定元素的属性。

 <xsl:variable name="hyperlink"><xsl:value-of select="@href" /></xsl:variable>
 <a href="{$hyperlink}"> <xsl:value-of select="@href" /></a>

答案 2 :(得分:-1)

如果要在XSLT中使用超链接,则需要使用XSLT创建HTML输出。 在HTML中,您可以创建像这样的超链接

<a href="http://www.yourwebsite.com/" target="_blank">Read our privacy policy.</a>

在此,整个文本成为指向www.yourwebsite.com的超链接