简体   繁体   English

如何使用xslt转换为XML元素添加ID属性

[英]How to add an ID attribut to xml elements using xslt transformation

I am very new to xml and xslt transformation. 我对xml和xslt转换非常陌生。 I need some advice. 我需要一些建议。

Basically I have managed to display a range of images from my XML page using XLTR Transformation. 基本上,我已经使用XLTR Transformation从XML页面显示了一系列图像。

The original xml has no ID's associated with each image, I cant alter the xml. 原始xml没有与每个图像关联的ID,我无法更改xml。 Therefore I was wondering if there was a way in xslt to add an ID attribute to each image before it is outputted to the browser? 因此,我想知道xslt中是否有一种方法可以在将每个图像输出到浏览器之前为其添加ID属性? As I want to be able to control each element/image individually using CSS. 因为我希望能够使用CSS分别控制每个元素/图像。

I hope that makes sense? 我希望这是有道理的?

The xslt is as follows. xslt如下。

    <xsl:for-each select=" cars/car_type">

   <!-- <h5> <xsl:value-of select="car-type"/> </h5> -->

 <div class ="images">   
 <img>
  <xsl:attribute name="src">
    <xsl:value-of select="image"/>

  </xsl:attribute>
</img>
 </div>
    </xsl:for-each>

Thanks again. 再次感谢。

You can use generate-id ( http://www.w3.org/TR/xslt#function-generate-id ) function to generate an id for an input node eg 您可以使用generate-idhttp://www.w3.org/TR/xslt#function-generate-id )函数为输入节点生成ID,例如

<xsl:for-each select="cars/car_type">
  <div class="images">
    <img src="{image}" id="{generate-id(image)}"/>
  </div>
</xsl:for-each>

When you create the CSS elsewhere in your stylesheet you simply need to make sure you process the image elements again and use generate-id again. 在样式表中的其他位置创建CSS时,只需确保再次处理image元素并再次使用generate-id

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

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