2016-03-08 59 views
0

我有可能重複值的列表,例如創建不同的列表:如何從2場與XSLT

<dates> 
    <date> 
     <month>January</month> 
     <year>2015</year> 
    </date> 

    <date> 
     <month>January</month> 
     <year>2016</year> 
    </date> 

    <date> 
     <month>February</month> 
     <year>2015</year> 
    </date> 

    <date> 
     <month>January</month> 
     <year>2016</year> 
    </date> 
</dates> 

想收到不同的名單列表:

2015年
一月2015年2月
2016年1月

不知道是可能的,我可以編輯下面的代碼用foreach:

<li> 
     <xsl:value-of select="distinct-values(.)"/> 
    </li>   

或:

<li> 
     <xsl:foreach select="//dates/date[not(.preceding::*)]"? 
    </li> 

回答

0

這應做到:

distinct-values(/dates/date/concat(month, ' ', year))

的XPath是選擇的monthyear串聯,然後選擇其中的distinct-values

1

使用以下XSLT 1.0模板可以實現對實體的排序。它通過year小號消除重複和排序:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

    <xsl:key name="someDate" match="date" use="concat(month/text(),' ',year/text())" /> 

    <xsl:template match="/dates"> 
    <xsl:for-each select="date[generate-id() = generate-id(key('someDate',concat(month/text(),' ',year/text()))[1])]"> 
     <xsl:sort select="year/text()" />  
     <xsl:value-of select="concat(month/text(),' ',year/text())" /><xsl:text>&#10;</xsl:text> 
    </xsl:for-each> 
    </xsl:template> 

</xsl:stylesheet> 

結果是

<?xml version="1.0"?> 
January 2015 
February 2015 
January 2016 

但是,它不會在一個月的名稱進行排序,它只是排序年。

如果你喜歡與周圍的元素li結果,更換xsl:value-of

<li> 
    <xsl:value-of select="concat(month/text(),' ',year/text())" /> 
</li>