The best way to deal with this is to create a separate template for building an <img> with from a <media> chunk, and then use <apply-templates /> to "call" it by selecting the appropriate chunk:
Because there are more than one <media> element, we add the predicate (square-brackets) - so the if statement will successfully detect if there's a media element with the 'square' value. But then when you generate the <img> tag, you have to use the predicate again - for every value you need, which is why I suggested the template.
To make this work without, you can do this instead:
Parsing multiple media sizes
USAToday added multiple media sizes. My code just pics up the firs, the largest naturally!
How do I parse the resolution I want?
My code:
<xsl:if test="body.content/media"> -- (I'm assuming I have to add something here)
<xsl:element name="img">
<xsl:attribute name="src"><xsl:value-of select="body.content/media/media-reference/@source"/>
</xsl:attribute>
<xsl:attribute name="alt"><xsl:value-of select="media/media-reference/@alternate-text"/>
</xsl:attribute>
<xsl:attribute name="align">left</xsl:attribute>
</xsl:element>
</xsl:if>
XMl from USA Today:
<body.content>
<media media-type="image" image-specification="1:1">
<media-reference mime-type="image/jpeg" source="http://www.gannett-cdn.com/media/USATODAY/USATODAY/2012/11/13/gty-156328633-1_1.jpg" alternate-text="" height="2068" width="2070"/>
<media-caption>
AFL-CIO President Richard Trumka met with President Obama on Tuesday.
</media-caption>
</media>
<media media-type="image" image-specification="supersquare">
<media-reference mime-type="image/jpeg" source="http://www.gannett-cdn.com/media/USATODAY/USATODAY/2012/11/13/gty-156328633-x-supersquare.jpg" alternate-text="" height="1024" width="1024"/>
<media-caption>
AFL-CIO President Richard Trumka met with President Obama on Tuesday.
</media-caption>
</media>
<media media-type="image" image-specification="square">
<media-reference mime-type="image/jpeg" source="http://www.gannett-cdn.com/media/USATODAY/USATODAY/2012/11/13/gty-156328633-x-square.jpg" alternate-text="" height="98" width="98"/>
<media-caption>
AFL-CIO President Richard Trumka met with President Obama on Tuesday.
</media-caption>
</media>
<media media-type="image" image-specification="x48">
<media-reference mime-type="image/jpeg" source="http://www.gannett-cdn.com/media/USATODAY/USATODAY/2012/11/13/gty-156328633-x48.jpg" alternate-text="" height="48" width="48"/>
<media-caption>
AFL-CIO President Richard Trumka met with President Obama on Tuesday.
</media-caption>
</media>
Hi John,
The best way to deal with this is to create a separate template for building an <img> with from a <media> chunk, and then use <apply-templates /> to "call" it by selecting the appropriate chunk:
/Chriztian
This should work, should it not?
Almost,
Because there are more than one <media> element, we add the predicate (square-brackets) - so the if statement will successfully detect if there's a media element with the 'square' value. But then when you generate the <img> tag, you have to use the predicate again - for every value you need, which is why I suggested the template.
To make this work without, you can do this instead:
/Chriztian
Im missing something. Just not working. Tried this, did not throw an error, but still picking up first image.
Hi John,
Could you post the XSLT file you're using? And if you're not fetching the XML from whithin that file, then maybe explain how you match them up.
/Chriztian
Chriztian,
I got the second code sample working. Later in the code we have this:
<xsl:template match="body.content">
<div class="storyContent">
<xsl:if test="media">
<div class="photobox">
<div class="photo">
<xsl:element name="img">
<xsl:attribute name="src">
<xsl:value-of select="media/media-reference/@source"/>
</xsl:attribute>
<xsl:attribute name="alt">
<xsl:value-of select="media/media-reference/@alternate-text"/>
</xsl:attribute>
</xsl:element>
</div>
<div class="photocaption"><xsl:value-of select="media/media-caption"/></div>
</div>
</xsl:if>
<xsl:apply-templates />
Same problem. USAToday used to only give one media image for both thumb and story.
Fixed it1
<xsl:template match="body.content">
<div class="storyContent">
<xsl:variable name="StoryMedia" select="media[@image-specification = 'x-large']" />
<xsl:if test="$StoryMedia">
<div class="photobox">
<div class="photo">
<xsl:element name="img">
<xsl:attribute name="src">
<xsl:value-of select="$StoryMedia/media-reference/@source"/>
</xsl:attribute>
<xsl:attribute name="alt">
<xsl:value-of select="$StoryMedia/media-reference/@alternate-text"/>
</xsl:attribute>
</xsl:element>
</div>
<div class="photocaption"><xsl:value-of select="media/media-caption"/></div>
</div>
</xsl:if>
<xsl:apply-templates />
</div>
</xsl:template>
Using different sizes now for thumbs and stories. Thanks for the help!
Fixed it!
<xsl:template match="body.content">
<div class="storyContent">
<xsl:variable name="StoryMedia" select="media[@image-specification = 'x-large']" />
<xsl:if test="$StoryMedia">
<div class="photobox">
<div class="photo">
<xsl:element name="img">
<xsl:attribute name="src">
<xsl:value-of select="$StoryMedia/media-reference/@source"/>
</xsl:attribute>
<xsl:attribute name="alt">
<xsl:value-of select="$StoryMedia/media-reference/@alternate-text"/>
</xsl:attribute>
</xsl:element>
</div>
<div class="photocaption"><xsl:value-of select="media/media-caption"/></div>
</div>
</xsl:if>
<xsl:apply-templates />
</div>
</xsl:template>
Using different sizes now for thumbs and stories. Thanks for the help!
is working on a reply...
This forum is in read-only mode while we transition to the new forum.
You can continue this topic on the new forum by tapping the "Continue discussion" link below.