2009-11-08 42 views
0

對於每個循環的新聞項目節點,我都有一個值。在其他屬性中,這些新聞項目具有創建日期的兩個屬性。系統添加日期和用戶輸入創建日期(覆蓋系統日期)。我希望根據用戶輸入日期的偏好,按創建日期排序。XSLT如果第一個爲空,則對第二個值應用排序

下面是我謙虛無效的嘗試!

<xsl:for-each select="$currentPage/ancestor-or-self::node /node [@nodeTypeAlias = $documentTypeAlias and string(data [@alias='umbracoNaviHide']) != '1']"> 

<xsl:choose> 
<xsl:when test="data [@alias = 'createdDate'] != ''"> 
    <xsl:variable name="sort" select="string(data [@alias = 'createdDate'])"/> 
</xsl:when> 
<xsl:otherwise> 
    <xsl:variable name="sort" select="string(@createDate)"/> 
</xsl:otherwise> 
</xsl:choose> 

<xsl:sort select="$sort" order="descending"/> 

非常感謝

+0

umbraco ftw。我有很多戲劇在xslt中進行條件排序。 – ChadT 2009-11-09 09:02:35

回答

7
<xsl:sort select="(data[@alias='createdDate' and normalize-space() != '']|@createDate)[last()]" order="descending" /> 

此語句創建與含有日期的兩個節點的節點集,並根據文件順序獲取最後一個進行排序。如果數據節點存在並且不爲空,則它將用於排序,因爲元素的子元素出現在其屬性節點之後。

concat()只能工作,在少數情況下,如果使用文本排序;它會通過數字排序失敗。

+0

我認爲這工作,但仔細檢查它不完全。如果存在具有createdDate別名的數據節點,則它會使用它,但如果不存在,則不使用@createDate。對此有何想法?似乎更強大的使用concat,所以我想堅持下去。 – Max 2009-11-11 23:27:34

+0

一點點的信息。我認爲數據節點確實存在,但它是空的。在這種情況下,我想使用@createDate。 – Max 2009-11-12 21:41:24

+0

好的,你只是沒有暴露一切。 ;) 如果不能選擇空數據節點進行排序,則必須修改謂詞。我編輯了我的答案。 – Erlock 2009-11-13 09:20:26

0

權,似乎是一個黑客,但我已經能夠通過使用CONCAT進行排序,以實現這一目標。

實施例下面

<xsl:for-each select="$currentPage/ancestor-or-self::node /node [@nodeTypeAlias = $documentTypeAlias and string(data [@alias='umbracoNaviHide']) != '1']"> 
<xsl:sort select="concat(data [@alias = 'createdDate'],@createDate)" order="descending"/> 
0

爲了在XSLT測試一個節點是空的(或忽略):

<xsl:when test="not(string(name))">...</xsl:when> 
<!-- or --> 
<xsl:when test="not(name)">...</xsl:when> 
0

非常感謝Erlock的解決方案。由於對Umbraco XSLT語法進行了更改,我在一段時間內努力爭取在我的Umbraco版本(4.7.1)中工作。

對於任何有興趣的人來說,我的工作示例會更改Erlock的代碼;

<xsl:sort select="(current()/createdDate[normalize-space() != '']|@createDate)[last()]" order="descending" /> 
相關問題