Xslt 生成 Html <IMG> 标签。如何使用 XML 节点值作为 <IMG> 标签的 src 属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3625820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 04:21:24  来源:igfitidea点击:

Xslt generating Html <IMG> tags. How to use an XML node value as an src-attribute for the <IMG> Tags

htmlxslt

提问by Stephane Rolland

I'm still searching, but I haven't found yet the way to perform something like this:

我仍在寻找,但我还没有找到执行这样的事情的方法:

xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <!-- some other templates -->
    <xsl:template match="IMAGE">
        <img src="src_attribute_to_be_read_from_the_xml_file.jpg"/>
    </xsl:template>     
</xsl:stylesheet>

In my Xml <IMAGE>tags, the text value is the filename that should be inserted in the string src_attribute_to_be_read_from_the_xml_file.jpgwhen processed by this Xslt file.

在我的 Xml<IMAGE>标签中,文本值是src_attribute_to_be_read_from_the_xml_file.jpg由这个 Xslt 文件处理时应该插入到字符串中的文件名。

Have you any idea to perform this ?

你有什么想法来执行这个吗?

回答by musiKk

You use xsl:attribute:

你使用xsl:attribute

<img>
    <xsl:attribute name="src">
        <xsl:value-of select="you path here"/>
    </xsl:attribute>
</img>

It should also be possible to use

也应该可以使用

<img src="{some expression here}" />

According to the specificationthis is called a attribute value templateand should always work (i.e. both XSLT 1.0 and 2.0). Nice. Now I learned something too.

根据规范,这称为属性值模板,并且应该始终有效(即 XSLT 1.0 和 2.0)。好的。现在我也学到了一些东西。

回答by Piotr Nawrot

Alternatively you can use XSL template:

或者,您可以使用 XSL 模板:

<xsl:template match="image">
<xsl:element name="IMG">
  <xsl:attribute name="src">
    <xsl:value-of select="your_path"/>
  </xsl:attribute>
  <xsl:attribute name="title">
    <xsl:value-of select="your_title"/>
   </xsl:attribute >
</xsl:element>

回答by Pathros

And if you want to add the height, width, and altattributes, then you can do it like the following:

如果你想添加的heightwidthalt属性,那么你可以做如下所示:

         <img>
             <xsl:attribute name="src">
                 <xsl:value-of select="picture"/>
              </xsl:attribute>
              <xsl:attribute name="title">
                 <xsl:value-of select="pictureTitle"/>
              </xsl:attribute >
              <xsl:attribute name="alt">
                 <xsl:value-of select="pictureTitle"/>
              </xsl:attribute >
              <xsl:attribute name="height">
                 20
              </xsl:attribute >
              <xsl:attribute name="width">
                 30
              </xsl:attribute >
         </img>