JADE
JADE

Reputation: 523

How to create hyperlink using XSLT?

I'm new at XSLT. I want to create a hyperlink using XSLT. Should look like this:

Read our privacy policy.

"privacy policy" is the link and upon clicking this, should redirect to example "www.privacy.com"

Any ideas? :)

Upvotes: 7

Views: 54781

Answers (3)

realPK
realPK

Reputation: 2951

If you want to read hyperlink value from a XML file, this should work:

Assumption: href is an attribute on specific element of your XML.

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

Upvotes: 9

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243459

This transformation:

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

when applied on any XML document (not used), produces the wanted result:

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

and this is displayed by the browser as:

Read our privacy policy.

Now imagine that nothing is hardcoded in the XSLT stylesheet -- instead the data is in the source XML document:

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

Then this transformation:

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

when applied on the above XML document, produces the wanted, correct result:

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

Upvotes: 13

Raghuram
Raghuram

Reputation: 3967

If you want to have hyperlinks in XSLT, then 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 the whole text becomes a hyperlink pointing to www.yourwebsite.com

Upvotes: -2

Related Questions