|
[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] RE: Conditional including of attributes
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.kaila@xxxxxx]
Sent: Friday, March 26, 2004 1:23 AM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: 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
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|

Cart








