2013-03-26 35 views
0

我有一個XML(即我從外部部分獲得):顯示不同的圖像根據XML輸出使用輸出XSL

...<prop name="day">monday</prop> 
<prop name="week">2</prop>... 

我想知道如果它是在所有可能的使用XSL來顯示圖像而不是當天的名字?當天的名稱將隨着7個可能的變量而改變,我需要爲每週的每一天顯示不同的圖像。

所以我希望的結果是這樣的:

<img src="mondayimage.jpg"> 
<p>Week number 2</p> 

回答

0

在XSLT 2.0您麪包車使用索引的XPath函數產生一個有效的查找:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 
    <xsl:output indent="yes"/> 

    <xsl:param name="days" select="('monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday')"/> 
    <xsl:param name="day-image" select="('image1.png','image2.png','image3.png','image4.png','image5.png','image6.png','image7.png')"/> 

    <xsl:template match="/root"> 
     <root> 
      <xsl:apply-templates/> 
     </root> 
    </xsl:template> 

    <xsl:template match="prop[@name='day']"> 
     <img src="{$day-image[index-of($days, current())]}"/> 
    </xsl:template> 

    <xsl:template match="prop[@name='week']"> 
     <p>Week number <xsl:value-of select="."/></p> 
    </xsl:template> 
</xsl:transform> 

Working example

0

,您可以利用的xsl:選擇處理您的方案。

下面我包括代碼片段,您可以利用

<xsl:template match="prop"> 
    <xsl:variable name="day" select="."/> 
    <xsl:choose> 
     <xsl:when test="$day='monday'"> 
      <img src="mondayimage.jpg"/> 
     </xsl:when> 
     <!-- repeat condition for all the days --> 
     <xsl:otherwise> 
      <p><xsl:value-of select="@name"/> number <xsl:value-of select="."/> </p> 
     </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 
0

我會建議使用不同的模板爲每種類型的道具,使得更容易改變以後。您還可以使用內的「{}」在字符串中有一個模板,看起來像下面的XSL代碼:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:template match="root/prop[@name='day']"> 
     <img src="{.}image.jpg"/> 
    </xsl:template> 
    <xsl:template match="root/prop[@name='week']"> 
     <p><xsl:value-of select="concat('Week number ',.)"/></p> 
    </xsl:template> 
</xsl:stylesheet>