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? :)
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>
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With