Re: [xsl] need to parse last 3 characters before .jpg

Subject: Re: [xsl] need to parse last 3 characters before .jpg
From: Christoph Naber <dio2000@xxxxxxx>
Date: Sun, 14 Sep 2008 09:54:14 +0200
Hello,

>> <xsl:output method="html" version="1.0" encoding="iso-8859-1"
Also the xsl:output/@method should be 'xhtml'.

I think html is correct, because the OP states xmlns:html="http://www.w3.org/TR/REC-html40"; in the stylesheet node (although he didn't use it)


<xsl:stylesheet version="2.0"

This is a basic transformation, you could also do it with XSLT 1.0, that would be usefull if its intended to run the transformation process on the browser.


<xsl:for-each select="items/item">

I suggest to use templates. It makes programming with XSLT much easier, because you leave the hard work to the processor. Dont think about "HOW to do it" but "WHAT has to be transformed".


<xsl:for-each select="images/image">

Same here


<img><xsl:attribute name="src"><xsl:value-of select="@src"
/></xsl:attribute><xsl:attribute
name="alt"><xsl:value-of select="parent::node()/parent::node()/title"
/></xsl:attribute></img>

I think you may be happy with the abbreviation, I can't see that the long form is usefull here:


<img src="{@src}" ...>

Instead of parent::node()/parent::node()/title maybe you want to use
ancestor::item[1]/title which is more specific and therefore less error-prone.
I made the changes I suggested to the code you supplied. Maybe you're able to use some parts of it.


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; v>
<xsl:output method="html" version="1.0" encoding="iso-8859-1" indent="yes"/>


<!-- Root template -->
<xsl:template match="feed/contest">
<html>
<head>
<title>Fine Woodworking - <xsl:value-of select="title" /> Most Recent Posts</title>


    <style type="text/css">
     <!-- [...] -->
    </style>
   </head>

   <!-- Body -->
   <body>
    <h1>Check out the most recent entries</h1>
    <a href="{link/text()}">View All Entries</a>

    <xsl:apply-templates select="items/item" />
   </body>

  </html>
 </xsl:template>

 <xsl:template match="item" >
  <div class="item">
   <xsl:apply-templates select="images/image[@size = '_sqm']" />
   <a href="{link/text()}"><xsl:value-of select="title" /></a>
   <br/>
  </div>
 </xsl:template>

 <xsl:template match="image">
  <img src="{@src}" alt="{ancestor::item[1]/title/text()}"/>
 </xsl:template>
</xsl:stylesheet>

java -jar C:\Programme\Saxon\saxon.jar feed.xml transform.xsl > output.html

<html >
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<title>Fine Woodworking - Win a Commodore 128! Most Recent Posts</title>
<style type="text/css">
[...]
</style>
</head>
<body>
<h1>Check out the most recent entries</h1>
<a href="http://dev.taunton.com/contest/win-a-commodore-128";>View All Entries</a>
<div class="item">
<img src="http://dev.taunton.com/assets/uploads/posts/3529/quilt_full1_lg_sqm.jpg"; alt="vote for me you chump">
<a href="http://dev.taunton.com/item/3529/vote-for-me-you-chump";>vote for me you chump</a><br>
</div>
<div class="item">
<img src="http://dev.taunton.com/assets/uploads/posts/3538/cs-v5-cat-sewing_sqm.JPG"; alt="my entry">
<a href="http://dev.taunton.com/item/3538/my-entry";>my entry</a><br>
</div>
</body>
</html>



Regards Christoph

Current Thread