Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create hyperlink using XSLT?

Tags:

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

like image 951
JADE Avatar asked Apr 17 '12 03:04

JADE


2 Answers

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>
like image 65
Dimitre Novatchev Avatar answered Sep 20 '22 15:09

Dimitre Novatchev


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>
like image 27
realPK Avatar answered Sep 21 '22 15:09

realPK