Using the xsl:element coupled with the xsl:copy-of elements will do the
trick...
This XML...
<?xml version="1.0"?>
<links>
<link href="sukbasics.xml">Perusteet</link>
<link href="sukbasics.xml" target="main">Perusteet</link>
</links>
Transformed by this XSL...
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="links">
<xsl:apply-templates select="link"/>
</xsl:template>
<xsl:template match="link">
<xsl:element name="a">
<xsl:copy-of select="@*"/>
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Gives you this output...
<a href="sukbasics.xml">Perusteet</a>
<a href="sukbasics.xml" target="main">Perusteet</a>
Best of luck!
<M:D/>
-----Original Message-----
From: Kaarle Kaila [mailto:kaarle(_dot_)kaila(_at_)iki(_dot_)fi]
Sent: Friday, March 26, 2004 1:23 AM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: [xsl] Conditional including of attributes
hi,
I have in my xml-file an element to make links
<link href="sukbasics.xml" target="main">Perusteet</link>
for html equivalent <a href="sukbasics.xml" target="main">Perusteet</a>
or
<link href="sukbasics.xml">Perusteet</link>
for html equivalent <a href="sukbasics.xml" >Perusteet</a>
The below xslt snippet makes the transformation OK but I don't like it
as it
has most it's content twice. Any good advice to make it cleaner would be
appreciated!
---------------
<xsl:template match="link">
<xsl:variable name="lhref" select="@href" />
<xsl:choose>
<xsl:when test="@target>
<a href="{$lhref}" >
<xsl:choose><xsl:when test=".=''"><xsl:value-of
select="@href"/></xsl:when>
<xsl:otherwise><xsl:value-of select="."/></xsl:otherwise></xsl:choose>
</a>
</xsl:when>
<xsl:otherwise>
<a href="{$lhref}" target="@target">
<xsl:choose><xsl:when test=".=''"><xsl:value-of
select="@href"/></xsl:when>
<xsl:otherwise><xsl:value-of select="."/></xsl:otherwise></xsl:choose>
</a>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
regards
Kaarle
--
Kaarle Kaila
email: kaarle dot kaila at iki dot fi
www.iki.fi/kaila
--+------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
You are subscribed as: m(_dot_)david(_at_)mdptws(_dot_)com
To unsubscribe, go to:
http://lists.mulberrytech.com/unsub.php/xsl-list/m(_dot_)david(_at_)mdptws(_dot_)com
or e-mail:
<mailto:xsl-list-unsubscribe-m(_dot_)david=mdptws(_dot_)com(_at_)lists(_dot_)mulberrytech(_dot_)com>
--+--