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">
<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!
<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>
<xsl:value-of select="page/news/highlights/entry[position() = 1]/image" />
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>
精彩评论