简体   繁体   English

获得XSL,XML相同名称的价值

[英]Get value-of in XSL, XML same names

Been toying with this for some time... I have an xml file with the following lines 玩了一段时间...我有一个带有以下几行的xml文件

<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: 您的问题的定义不是很好,因为我们没有整个XSLT或所需的输出,但是我将尝试一下:

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. 如果使用for循环遍历不同的图像,则需要在循环本身而不是循环的内容上设置约束。

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. 如果将示例代码放在xsl:for-each ,它将遍历每张图像,但仅当具有@size="medium"image是当前节点时才填充@src属性。 That would give you three table rows with three images but only one image with a valid @src attribute. 这将为您提供具有三张图像的三个表行,但只有一个具有有效@src属性的图像。

Instead change your xsl:for-each ( or use xsl:apply-templates ) to only iterate over your images with @size="medium" : 而是更改xsl:for-each或使用xsl:apply-templates )以仅使用@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 . 另一个技巧:代替使用xsl:attribute使用属性值模板

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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