9

My XML provides me with multiple images assigned to different mmids:

<Mediendaten>
    <Mediendaten mmid="22404">
        <url size="original">A 22404 FILE</url>
        <url size="thumb">ANOTHER 22404 FILE</url>
    </Mediendaten>
    <Mediendaten mmid="22405">
        <url size="original">A 22405 FILE</url>
        <url size="thumb">ANOTHER 22405 FILE</url>
    </Mediendaten>
<Mediendaten>

My XSLT selects only the urls where size=thumb:

<xsl:template match="/Mediendaten">
<xsl:apply-templates select="Mediendaten/url">
</xsl:apply-templates>
</xsl:template>

<xsl:template match="Mediendaten/url">
<xsl:if test="@size = 'thumb'">
<img width="280" border="0" align="left">
<xsl:attribute name="src"><xsl:value-of select="."/></xsl:attribute>
</img>
</xsl:if>
</xsl:template>

HOWEVER, I only need the thumbnail from the first mmid (in this case 22404). I have NO control over the mmid value.

How do I stop my template so it only outputs the thumb file of the first mmid?

Many thanks for any help!

C. M. Sperberg-McQueen
  • 24,596
  • 5
  • 38
  • 65
Richard Tinkler
  • 1,635
  • 3
  • 21
  • 41
  • Rather than using an `` inside of your template, you can put that criteria inside of your template match: ``. It will give the XSLT processor a chance to optimize, and simplifies the stylesheet. – Mads Hansen Jun 05 '13 at 00:38

3 Answers3

14

The simplest way is to change the template for /Mediendaten:

<xsl:template match="/Mediendaten">
  <xsl:apply-templates select="Mediendaten[@mmid][1]/url"/>
</xsl:template>

The [@mmid] constrains the selection to child Mediendaten elements that carry the mmid attribute, the [1] constrains the selection to the first one of these.

P.S. Whoever designed the XML you are using hates you. (Using the same name for both kinds of element now labeled Mediendaten is a dirty rotten trick; it makes everything you do with the data harder. Try to figure out what you did to piss them off so badly, and make amends to them. Just a word to the wise.)

C. M. Sperberg-McQueen
  • 24,596
  • 5
  • 38
  • 65
1
<xsl:apply-templates select="Mediendaten[1]/url" />
Rubens Farias
  • 57,174
  • 8
  • 131
  • 162
1

Somme commends.

First of all follow the suggestion from Mads Hansen. Have a template which now how to handle "thumb" images.

<xsl:template match="Mediendaten/url[@size = 'thumb']" >
    <img width="280" border="0" align="left" src="{.}" />
</xsl:template>

Then if you like to output only the first thump image (from Mediendaten in document order) use:

<xsl:template match="/Mediendaten">
    <xsl:apply-templates select="Mediendaten[1]/url[@size = 'thumb']" />
</xsl:template>

But if the meaning of
"HOWEVER, I only need the thumbnail from the first mmid" is not Mediendaten (with mmid) in document order, but the from Mediendaten with the smallest mmid. Try this:

<xsl:template match="/Mediendaten">
    <xsl:for-each select="Mediendaten">
        <xsl:sort select="@mmid"/>
        <xsl:if test="position()=1">
            <xsl:apply-templates select="url[@size = 'thumb']" />
        </xsl:if>
    </xsl:for-each>
</xsl:template>
Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
hr_117
  • 9,589
  • 1
  • 18
  • 23