Вопрос

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!

Это было полезно?

Решение

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.)

Другие советы

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

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>
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top