代码之家  ›  专栏  ›  技术社区  ›  Stephane Rolland

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

  •  9
  • Stephane Rolland  · 技术社区  · 14 年前

    我还在寻找,但我还没有找到这样的方法:

    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>
    

    在我的Xml中 <IMAGE> src_attribute_to_be_read_from_the_xml_file.jpg 在此Xslt文件处理时。

    3 回复  |  直到 12 年前
        1
  •  31
  •   musiKk    14 年前

    你用 xsl:attribute :

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

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

    根据 specification 属性值模板

        2
  •  2
  •   Piotr Nawrot    10 年前

    或者,您可以使用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>
    

        3
  •  0
  •   Pathros    7 年前

    如果你想添加 height width ,和 alt 属性,则可以按以下方式执行:

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