2012-02-02 59 views
1

我怎樣才能DISPLY相同的水平元素的值後,如果conditonXSL如果conditon顯示相同的水平元素

例如

XML

<catalog> 
<cd> 
    <title>Empire Burlesque</title> 
    <artist>Bob Dylan</artist> 
    <country>USA</country> 
    <company>Columbia</company> 
    <price>10.90</price> 
    <year>1985</year> 
</cd> 
<cd> 
    <title>Hide your heart</title> 
    <artist>Bonnie Tyler</artist> 
    <country>UK</country> 
    <company>CBS Records</company> 
    <price>9.90</price> 
    <year>1988</year> 
</cd> 
<cd> 
    <title>Greatest Hits</title> 
    <artist>Dolly Parton</artist> 
    <country>USA</country> 
    <company>RCA</company> 
    <price>9.90</price> 
    <year>1982</year> 
</cd> 

XSLT

<xsl:if test="/catalog/cd/country='UK'"> 
    <xsl:value-of select="title"/> 
    <xsl:value-of select="artist"/> 
</xsl:if> 

這不會displying UK級元素的標題和藝術家

我知道,解決它的方法之一是使用每個循環,但我在尋找一種有效的方法

回答

3

這是可以做到只使用模板:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:template match="/catalog/cd[country='UK']" priority="1"> 
     <xsl:value-of select="title"/> 
     <xsl:value-of select="artist"/> 
    </xsl:template> 
    <xsl:template match="/catalog/cd"> 
     <!-- handle non-UK CDs here --> 
    </xsl:template> 
</xsl:stylesheet> 

還有很多其他方法 - 當然取決於您的要求 - 安排模板以生成您想要的輸出。例如,您最終可能會使用顯式模板來處理titleartist元素(或者明確隱藏每個cd的所有其他子項)。

所有這一切都取決於你的特定需求,但一般點我想在這裏顯示的是你獲得了大量的電能(與簡潔的代碼結束),當你適當地捕捉目標的元素在模板匹配 (而不是ad-hoc條件)。

3

此:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 

<xsl:template match="/catalog/cd"> 
    <xsl:if test="country = 'UK'"> 
     <xsl:value-of select="title"/> 
     <xsl:value-of select="artist"/> 
    </xsl:if> 
</xsl:template> 

</xsl:stylesheet> 

產地:

<?xml version="1.0" encoding="UTF-8"?> 

Hide your heartBonnie Tyler