7

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

阅读我们的隐私政策。

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

有任何想法吗?:)

4

3 回答 3

13

这种转变

<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>
于 2012-04-17T04:02:44.627 回答
8

如果您想从 XML 文件中读取超链接值,这应该可以:

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

 <xsl:variable name="hyperlink"><xsl:value-of select="@href" /></xsl:variable>
 <a href="{$hyperlink}"> <xsl:value-of select="@href" /></a>
于 2013-02-15T20:04:18.290 回答
-2

如果您想在 XSLT 中拥有超链接,那么您需要使用 XSLT 创建 HTML 输出。在 HTML 中,您可以像这样创建超链接

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

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

于 2012-04-17T03:41:36.670 回答