1

我有一个 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 的价值(我怎样才能得到这些链接。)

4

1 回答 1

2

您没有获得价值的原因是因为已经定位在 上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>

注意使用属性值模板来写出src属性值。

于 2017-03-22T11:40:37.693 回答