2013-06-19 65 views
0

我正在製作單選按鈕列表,查看包含單個國家和相關區域的項目列表。下面是我的單選按鈕XSLT或JavaScript - 刪除重複

<xsl:template match="/"> 
    <div id="filter"> 
     <xsl:call-template name="container" /> 
    </div> 
</xsl:template> 


<xsl:template name="container"> 
    <xsl:variable name="Rows" select="/dsQueryResponse/Rows/Row"/>  
     <xsl:for-each select="$Rows"> 
     <xsl:call-template name="group-items"/> 
     </xsl:for-each>  
</xsl:template> 

<xsl:template name="group-items">  
    <label class="region-filter-item" region="{@Region}"> 
    <input type="radio" name="region" class="regionbox"> 
     <xsl:value-of select="@Region"/> 
    </input> 
    </label>  

這將產生正確的數據,但它也複製該地區許多,因爲如果有15個國家在一個區域中的XSL,一個區域將顯示15倍。

什麼是最好的方式只顯示每個地區一次?

+0

我們需要看到您的XML看起來像什麼,和一些你的XSLT的。 – JLRishe

+0

嗨JLRishe - 沒有可用的XML。我正在使用包含列表的內容管理系統。該列表包含兩列,一個國家和一個地區。每個國家都有相關的地區。我使用XSL來表示這些信息。我更新了XSL以顯示我的完整代碼。 – MegaTron

+0

嗯,我猜你必須知道XML的結構,否則你將無法在第一時間編寫任何XSLT;)。但無論如何,這聽起來很像分組問題。你在這裏使用XSLT1.0還是XSLT2.0? –

回答

0

我寫了一個JavaScript函數,將每個獨特的項目推入一個數組,然後使用該數組來填充我的容器。全部解決。感謝所有幫助傢伙:)

function removeDuplicates(){ 
var Array = new Array(); 
$('#filter > .filter-item').each(function(){ 
    var country = $(this).attr('country ');   
    if ($.inArray(region, Array) == -1) { 
     Array .push(region); 
    } 
}); 
Array .sort(); 

var length = Array .length; 
for (var i = 0; i < length; i++) { 
    $('#filter-container').find('.container').append('<label class="filter-item" country="' + Array [i] + '">' 
               + '<input type="radio" name="country" class="checkbox">' 
                + Array [i] + 
               '</input>' + 
             '</label> '); 
    } 

}