RE: [xsl] Conditional including of attributes

Subject: RE: [xsl] Conditional including of attributes
From: "M. David Peterson" <m.david@xxxxxxxxxx>
Date: Fri, 26 Mar 2004 01:34:24 -0700
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: [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

Current Thread