tags:

views:

83

answers:

3

I'm coding my first website using XML+XSL. The xml I am transforming have this section:

<page>
    <news>
        <highlights>
            <entry>
                <mini>x_thumb.jpg</mini>
                <title>title</title>
                <text>text</text>
                <image>x.jpg</image>
                <link>x.html</link>
            </entry>
            <entry>
                <mini>z_thumb.jpg</mini>
                <title>title</title>
                <text>text</text>
                <image>z.jpg</image>
                <link>z.html</link>
            </entry>
            <entry>
                <mini>y_thumb.jpg</mini>
                <title>title</title>
                <text>text</text>
                <image>y.jpg</image>
                <link>y.html</link>
            </entry>
        </highlights>
    </news>
</page>

In my .xsl file I want to select the first entry because I'm doing a jQuery image rotator and I need the "default" image to show it. So I coded:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;

    <xsl:template match="/">
        [...]

        <xsl:template match="page/news/highlights/entry[1]"> 
            <div class="main_image">
                <img>
                    <xsl:attribute name="src">
                        <xsl:value-of select="image" />
                    </xsl:attribute>
                    <xsl:attribute name="alt">
                        <xsl:value-of select="title" />
                    </xsl:attribute>
                </img> 
                <div class="desc"> 
                    <div class="block"> 
                        <p>
                            <xsl:value-of select="text" />
                        </p> 
                    </div> 
                </div> 
            </div>
        </xsl:template>
[...]

And I can't get it working. I've tried various ways, like:

<xsl:value-of select="page/news/highlights/entry[1]/image" />

An no way... How can I do it?

Thanks in advance!

+2  A: 
<xsl:value-of select="page/news/highlights/entry[position() = 1]/image" />
dejavu
It worked! Thanks dejavu
Isern Palaus
@Isem Palaus: If that worked, then you had something else wrong. See http://www.w3.org/TR/xpath/#path-abbrev and in section 2.4, "Thus a location path `para[3]` is equivalent to `para[position()=3]`"
Greg Hewgill
+1  A: 

Do you want something like this?

<xsl:foreach select="page/news/highlights/entry">
    <div>
        <xsl:if test="position() = 1">
            <xsl:attribute name="class">main_image</xsl:attribute>
        </xsl:if>
        <img>
            <xsl:attribute name="src">
                <xsl:value-of select="image" />
            </xsl:attribute>
            <xsl:attribute name="alt">
                <xsl:value-of select="title" />
            </xsl:attribute>
        </img> 
        <div class="desc"> 
            <div class="block"> 
                <p>
                    <xsl:value-of select="text()" />
                </p> 
            </div> 
        </div> 
    </div>
</xsl>
Eric
+2  A: 
<xsl:template match="/">
  <!-- [...] -->

  <!-- show first entry only -->
  <xsl:apply-templates select="page/news/highlights/entry[1]" />
</xsl:template>

<!-- generic template to handle <entry> elements -->
<xsl:template match="entry"> 
  <div class="main_image">
    <!-- Attribute Value Templates save many lines of code --> 
    <img src="{image}" alt="{title}" />
    <div class="desc"> 
      <div class="block"> 
        <p><xsl:value-of select="text" /></p> 
      </div> 
    </div> 
  </div>
</xsl:template>
Tomalak
Sorry Tomalak for my bad expresion. As you can see English is not my main language. I'm sorry for the confusion I've caused. I've to say that your answer helped me a lot and it's what I'm using. Thank you!
Isern Palaus
@Isem: No need to not apologize. You should have fixed your question instead.
Tomalak
Also you could match first `entry` like `match="entry[1]"` and overwrite text nodes' built-inrule with an empty rule for compacting code.
Alejandro
@Alejandro: I consider writing a distinct template for a single node a code smell in XLST. I deliberately did not choose do that.
Tomalak