简体   繁体   中英

Get value-of in XSL, XML same names

Been toying with this for some time... I have an xml file with the following lines

<image size="small">image_small.jpg</image>
<image size="medium">image_medium.jpg</image>
<image size="large">image_large.jpg</image>

ANy ideas on how I can just get the medium url for example.

currently I have... but its not doing the trick

<tr>
<td><img><xsl:attribute name="src">
          <xsl:value-of select="image/@size[@size=medium]" />
         </xsl:attribute>
    </img>
</td>
</tr>

Thanks in advance

Your question isn't very well defined since we don't have your whole XSLT nor the desired output, but I'll give it a try:

If you are using a for-loop to iterate over the different images you need to set your constraints on the loop itself, not the content of the loop.

If you put your example code inside a xsl:for-each it will iterate over every image but only fill the @src attribute when the image with @size="medium" is the current node. That would give you three table rows with three images but only one image with a valid @src attribute.

Instead change your xsl:for-each ( or use xsl:apply-templates ) to only iterate over your images with @size="medium" :

<!-- This will only iterate over the medium sized images. -->
<xsl:for-each select="image[@size='medium']">
  <tr>
    <td>
      <img src="{.}"/>
    </td>
  </tr>
</xsl:for-each>

Another tip: instead of using xsl:attribute use an attribute value template .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM