7

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? :)

2
  • XSLT doesn't do hyperlinks. Rethink your question. Commented Apr 17, 2012 at 3:37
  • 3
    When thinking about how to achieve something like this in XSLT, split the task into two: (a) decide what HTML you want to generate, and (b) decide what XSLT code you need in order to generate it. The way you phrased the question suggests that you haven't grasped this separation of concerns. Commented Apr 17, 2012 at 7:55

3 Answers 3

13

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> 
Sign up to request clarification or add additional context in comments.

Comments

9

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> 

1 Comment

The xsl:variable isn't needed. Just do <a href="{@href}"><xsl:value-of select="@href"/></a>. See w3.org/TR/xslt#attribute-value-templates for more info.
-2

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.