2017-03-22 68 views
0

我有一个XML和XSL代码,产品具有图像和说明。我想在描述标签中添加这些图像。如何使用XSL获取XML元素的价值

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

回答

1

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

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

Howver,你应该避免使用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属性值的元素。

+0

此解决方案工作。谢谢 :) –