【问题标题】:How to get value of XML element using XSL如何使用 XSL 获取 XML 元素的值
【发布时间】:2017-03-22 11:36:39
【问题描述】:

我有一个 XML 和 XSL 代码,产品有图片和描述。我想将此图像附加到描述标签中。

<images>
  <img_item type_name="">http://www.example.com.tr/ExampleData/example1.jpg</img_item>
  <img_item type_name="">http://www.example.com.tr/ExampleData/example2.jpg</img_item>
  <img_item type_name="">http://www.example.com.tr/ExampleData/example3.jpg</img_item>
</images>

我这样写 XSL 代码(但它没有得到 img_type 的值):

      <Description>
        <xsl:for-each select="images/img_item">
          <xsl:text><![CDATA[<br/><img src="]]></xsl:text>
          <xsl:value-of select="images/img_item"/>
          <xsl:text><![CDATA[" />]]></xsl:text>
        </xsl:for-each>
      </Description>

我的代码不起作用。如何获取 img_type 的值(如何获取这些链接。)

【问题讨论】:

    标签: xml xslt value-of


    【解决方案1】:

    你没有得到和价值的原因是因为已经定位在img_item上,你的xsl:value-of选择将是相对的。所以你只需要这样做......

    <xsl:value-of select="." />
    

    但是,您应该避免使用 CDATA 来写出标签(除非您真的希望它们被转义)。直接写出你想要的元素就行了

    <xsl:template match="/">
      <Description>
        <xsl:for-each select="images/img_item">
          <br />
          <img src="{.}" />
        </xsl:for-each>
      </Description>
    </xsl:template>
    

    注意使用Attribute Value Templates写出src属性值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-09
      相关资源
      最近更新 更多