How to create a hyperlink using XSLT?

I am new to XSLT. I want to create a hyperlink using XSLT. It should look like this:

Check out our privacy policy .

"Privacy Policy" is a link, and when you click on it you should redirect the example "www.privacy.com"

Any ideas? :)

+5
source share
3 answers

This conversion is :

<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>

if applied to any XML document (not used), produces the desired result :

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

, and this is displayed by the browser as :

Check out our privacy policy .

Now imagine that nothing is hard-coded in the XSLT stylesheet — instead, the data is in the original XML document :

<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>
+11

XML, :

: href XML.

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

If you want to have hyperlinks in XSLT, you need to create HTML output using XSLT. In HTML, you can create a hyperlink like this

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

In this, all the text becomes a hyperlink pointing to www.yourwebsite.com

-1
source

All Articles