Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • john calder 6 posts 26 karma points
    Nov 14, 2012 @ 00:28
    john calder
    0

    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-producer>Alex Wong, Getty Images</media-producer>
    </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-producer>Alex Wong, Getty Images</media-producer>
    </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-producer>Alex Wong, Getty Images</media-producer>
    </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-producer>Alex Wong, Getty Images</media-producer>
    </media>

     

     

  • Chriztian Steinmeier 2726 posts 8320 karma points MVP 4x admin c-trib
    Nov 14, 2012 @ 01:59
    Chriztian Steinmeier
    0

    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:

    <!-- Render all square images -->
    <xsl:apply-templates select="body.content/media[@image-specification = 'square']" />
    
    <xsl:template match="media[@media-type = 'image']">
        <xsl:variable name="ref" select="media-reference" />
        <img align="left" src="{$ref/@source}" alt="$ref/@alternate-text" width="{$ref/@width}" height="{$ref/@height}" />
    </xsl:template>

    /Chriztian

  • john calder 6 posts 26 karma points
    Nov 14, 2012 @ 17:19
    john calder
    0

    This should work, should it not?

     

    <xsl:if test="body.content/media[@image-specification = 'square']"/>
            
            <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>
  • Chriztian Steinmeier 2726 posts 8320 karma points MVP 4x admin c-trib
    Nov 14, 2012 @ 23:22
    Chriztian Steinmeier
    0

    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:

    <xsl:variable name="squareMedia" select="body.content/media[@image-specification = 'square']" />
    <xsl:if test="$squareMedia">        
        <img src="{$squareMedia/media-reference/@source}" alt="{$squareMedia/media-reference/@alternate-text}" align="left" />
    </xsl:if>
    

    /Chriztian

  • john calder 6 posts 26 karma points
    Nov 15, 2012 @ 03:34
    john calder
    0

    Im missing something. Just not working. Tried this, did not throw an error, but still picking up first image.

     

     

  • Chriztian Steinmeier 2726 posts 8320 karma points MVP 4x admin c-trib
    Nov 15, 2012 @ 10:05
    Chriztian Steinmeier
    0

    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

  • john calder 6 posts 26 karma points
    Nov 15, 2012 @ 22:41
    john calder
    0

    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.

  • john calder 6 posts 26 karma points
    Nov 16, 2012 @ 03:26
    john calder
    0

    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!

  • john calder 6 posts 26 karma points
    Nov 16, 2012 @ 03:26
    john calder
    0

    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!

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

Please Sign in or register to post replies