Page 1 of 1

how to create link??

Posted: Sat Apr 08, 2006 7:32 pm
by guastoboy
hello everyone

that's my problem... i know this could be really easy for some of you.. i don't know how to create link using XSL

that's the code in XML

Code: Select all

<back>
<link type="left">pre.xml</link>
<link type="center">home.html</link>
<link type="right">ne.xml</link>
</back>

and this is the output in HTML in wanna have after XSL transformation

Code: Select all

<a href="pre.xml"><img src="aa.jpg"></a>
<a href="home.html"><img src="ab.jpg"></a>
<a href="ne.xml"><img src="ac.jpg"></a>
can someone help me? thank u so much!

Posted: Mon Apr 10, 2006 8:48 am
by Radu
Hi,

Here are two ways in which you can do this:

1) Use "xsl:element"

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output indent="yes"/>
<xsl:template match="back">
<xsl:for-each select="link">
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:value-of select="text()"/>
</xsl:attribute>
<xsl:element name="img">
<xsl:attribute name="src">
<xsl:value-of select="@type"/>
</xsl:attribute>
</xsl:element>
</xsl:element>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
2)Use XPath expressions directly in attribute values.

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output indent="yes"/>
<xsl:template match="back">
<xsl:for-each select="link">
<a href="{text()}">
<img src="{@type}"/>
</a>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
As you can see the transformer will evaluate what is inside curly brackets in attribute values as XPath expressions.
Here is a good XSLT tutorial: http://www.zvon.org/xxl/XSLTutorial/Boo ... tents.html

Hope this helps,
Regards, Radu.