2017-03-22 69 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

此解決方案工作。謝謝 :) –