如何使用XSLT创建超链接?

JAD*_*ADE 6 xslt

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

阅读我们的隐私政策.

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

有任何想法吗?:)

Dim*_*hev 12

这种转变:

<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>
Run Code Online (Sandbox Code Playgroud)

当应用于任何 XML文档(未使用)时,产生想要的结果:

<html><a href="www.privacy.com">Read our <b>privacy policy.</b></a></html>
Run Code Online (Sandbox Code Playgroud)

这将由浏览器显示为:

阅读我们的隐私政策.

现在假设在XSLT样式表中没有任何硬编码 - 而是数据在源XML文档中:

<link url="www.privacy.com">
 Read our <b>privacy policy.</b>
</link>
Run Code Online (Sandbox Code Playgroud)

然后这个转变:

<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>
Run Code Online (Sandbox Code Playgroud)

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

<a href="www.privacy.com">
 Read our <b>privacy policy.</b>
</a>
Run Code Online (Sandbox Code Playgroud)


rea*_*lPK 6

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

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

 <xsl:variable name="hyperlink"><xsl:value-of select="@href" /></xsl:variable>
 <a href="{$hyperlink}"> <xsl:value-of select="@href" /></a>
Run Code Online (Sandbox Code Playgroud)

  • 不需要`xsl:variable`.只需执行`<a href="{@href}"> <xsl:value-of select ="@ href"/> </a>`.有关详细信息,请参阅http://www.w3.org/TR/xslt#attribute-value-templates. (2认同)