RE: [xsl] Omit Data While Using Copy

Subject: RE: [xsl] Omit Data While Using Copy
From: "Michael Kay" <mhk@xxxxxxxxx>
Date: Thu, 12 Aug 2004 17:37:24 +0100
> <?xml version='1.0' encoding='UTF-8' ?>
> <xsl:stylesheet version="1.0" 
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
> 
> <xsl:template match="kapitel">
> 
>   <h2><xsl:value-of select="titel"/></h2>
> 
> 	<xsl:for-each select="hanvisning">
> 
> 	<br><a><xsl:attribute name="href"><xsl:value-of
> 
> select="substring-before(substring-after(@from,'('),')')"/>.xm
> l</xsl:attribute><xsl:value-of
> 
> select="node()"/></a></br>
> 
>   	</xsl:for-each>
> 
>    <br></br><br></br><a><xsl:attribute 
> name="href">2.xml</xsl:attribute>Return to Home Page</a>
> 
>   </xsl:template>
> </xsl:stylesheet>
> 
> 
> I suspect this wasn't the best way to accomplish this, but it 
> does work.  
> Since I am just starting to use xsl, I'd prefer to learn to 
> do things the 
> correct way, rather than modifying code to make it do what I 
> need it to.
> 
> Any suggestions to improve this code would be greatly appreciated.

Two suggestions:

1. It's usually easier to use attribute value templates. Replace:

<a><xsl:attribute name="href">2.xml</xsl:attribute>Return to Home Page</a>

with

<a href="2.xml">Return to Home Page</a>

and

<a><xsl:attribute name="href">
 <xsl:value-of
select="substring-before(substring-after(@from,'('),')')"/>.xml</xsl:attribu
te>
  <xsl:value-of select="node()"/></a>

with

<a href="{substring-before(substring-after(@from,'('),')')}.xml">
  <xsl:value-of select="node()"/>
</a>

2. Use apply-templates and template rules rather than xsl:for-each and
xsl:value-of.

Beginners tend to reach for for-each and value-of because they seem simpler
and more familiar. But using "push" processing with apply-templates and
template rules isn't difficult to master and leads to much more easily
maintainable code.

Michael Kay



> 
> Thanks again to all those who helped.
> 
> Trevor
> P.S.
>         Hopefully, it won't be long before I am able to offer 
> some help to 
> others!  :)
> 
> _________________________________________________________________
> MSNR Calendar keeps you organized and takes the effort out of 
> scheduling 
> get-togethers. 
> http://join.msn.com/?pgmarket=en-ca&page=byoa/prem&xAPID=1994&;
> DI=1034&SU=http://hotmail.com/enca&HL=Market_MSNIS_Taglines 
>   Start enjoying all the benefits of MSNR Premium right now 
> and get the 
> first two months FREE*.

Current Thread