2013-01-22 11 views
0

我有以下的下拉XSLT排序列表,如果倍數僅打印字一次

<xsl:if test="bankguarantee!=0"> 
      <li style="font-family:Arial,Arial MT,Luxi Sans,Verdana; font-size:10pt; margin:0cm 0cm 0pt; "> 
       <p style="font-family:Arial,Arial MT,Luxi Sans,Verdana; font-size:10pt; margin:0cm 0cm 0pt; "> 
        <span style="font-family:Arial,Arial MT,Luxi Sans,Verdana; font-size:11.0pt; ">The terms and conditions of <xsl:if test="bankguarantee='1'">the </xsl:if> 
      <xsl:if test="bankguarantee!='1'">each </xsl:if> 
    <xsl:for-each select="bankguarantees/bankguaranteedata"> 
    <xsl:if test="producttypes/option[@id='A']='selected'">A</xsl:if> 
    <xsl:if test="producttypes/option[@id='B']='selected'">B</xsl:if> 
    <xsl:if test="producttypes/option[@id='C']='selected'">C</xsl:if> 
    <xsl:if test="producttypes/option[@id='D']='selected'">D</xsl:if> 
    <xsl:if test="producttypes/option[@id='E']='selected'">E</xsl:if> 
    <xsl:if test="producttypes/option[@id='F']='selected'">F</xsl:if> 
    <xsl:if test="producttypes/option[@id='G']='selected'">G</xsl:if> 
    <xsl:if test="producttypes/option[@id='H']='selected'"><xsl:value-of select="otherprodtypebox"/></xsl:if> 
    <xsl:if test="position()!=last() and position()!=last()-1"> 
    <xsl:text>, </xsl:text> 
    </xsl:if> 
    <xsl:if test="position()=last()-1"> 
    <xsl:text> and </xsl:text> 
    </xsl:if> 
    <xsl:if test="position()=last()"> 
    <xsl:text></xsl:text> 
    </xsl:if></xsl:for-each> 
</xsl:if> 

和下面的XML

<producttypes> 
       <option id="A">selected</option> 
       <option id="B"/> 
       <option id="C"/> 
       <option id="D"/> 
       <option id="E"/> 
       <option id="F"/> 
       <option id="G"/> 
       <option id="H"/> 
      </producttypes> 
     <otherprodtypebox/> 

基本上所有工作在此刻確定,但我想避免多次發生並把所有其他的盒子放在最後。這些屏幕可以有99個。

此刻說我選擇5個選項,我能得到A,B,B,otherbox和otherbox

我想如果A比一次只能顯示一個選擇更與同爲全信它有摹

問題帶有爲H另一盒可以有數倍

所以你可以有

A,B,C,d,E,F,G,H,H,H, H AND H

我希望這是有道理的,任何解決方案都非常感謝。

N.B.重要的是要注意,XML是爲每個屏幕創建的,所以我可以有99個不同的XML,每次都選擇不同的東西。這是每個部分都很棘手的地方。也許它可以檢測是否選擇了多於1個A.感謝

+1

你能告訴什麼是之前和之後的'的'?也許整個''包含它? XML是否包含多個'bankguarantees'或多個'bankguaranteedata's?你提供的信息相當缺乏。您是否產生了一種情況,即您當前的方法在輸出中生成重複的字母? – JLRishe

+0

@JLRishe我現在添加了這個。整個模板是20000行代碼恐怕:( – topcat3

回答

1

如何增加這個附近的XSLT文件的頂部:本for-each循環

<xsl:key name="productOption" match="producttypes/option[. = 'selected']" use="@id"/> 

和更換您的for-each循環:

<xsl:for-each 
    select="bankguarantees/bankguaranteedata/producttypes/option[generate-id(.) = generate-id(key('productOption', @id)[1]) or @id = 'H']"> 
    <xsl:sort select="count(preceding-sibling::option)" data-type="number" /> 
    <xsl:if test="@id = 'A'">A</xsl:if> 
    <xsl:if test="@id = 'B'">B</xsl:if> 
    <xsl:if test="@id = 'C'">C</xsl:if> 
    <xsl:if test="@id = 'D'">D</xsl:if> 
    <xsl:if test="@id = 'E'">E</xsl:if> 
    <xsl:if test="@id = 'F'">F</xsl:if> 
    <xsl:if test="@id = 'G'">G</xsl:if> 
    <xsl:if test="@id = 'H'"> 
     <xsl:value-of select="../../otherprodtypebox"/> 
    </xsl:if> 
    <xsl:if test="position()!=last() and position()!=last()-1"> 
     <xsl:text>, </xsl:text> 
    </xsl:if> 
    <xsl:if test="position() = last()-1"> 
     <xsl:text> and </xsl:text> 
    </xsl:if> 
    </xsl:for-each> 

當此輸入運行:

<bankguarantees> 
    <bankguaranteedata> 
    <producttypes> 
     <option id="A">selected</option> 
     <option id="B"/> 
     <option id="C"/> 
     <option id="D"/> 
     <option id="E"/> 
     <option id="F"/> 
     <option id="G"/> 
     <option id="H"/> 
    </producttypes> 
    <otherprodtypebox>sprockets</otherprodtypebox> 
    </bankguaranteedata> 
    <bankguaranteedata> 
    <producttypes> 
     <option id="A">selected</option> 
     <option id="B"/> 
     <option id="C"/> 
     <option id="D">selected</option> 
     <option id="E"/> 
     <option id="F"/> 
     <option id="G"/> 
     <option id="H">selected</option> 
    </producttypes> 
    <otherprodtypebox>widgets</otherprodtypebox> 
    </bankguaranteedata> 
    <bankguaranteedata> 
    <producttypes> 
     <option id="A">selected</option> 
     <option id="B">selected</option> 
     <option id="C"/> 
     <option id="D">selected</option> 
     <option id="E"/> 
     <option id="F"/> 
     <option id="G"/> 
     <option id="H">selected</option> 
    </producttypes> 
    <otherprodtypebox>cogs</otherprodtypebox> 
    </bankguaranteedata> 

</bankguarantees> 

,輸出結果:

A, B, D, widgets and cogs 
+0

感謝@JLRishe我明天早上就嘗試了這一點,並讓你知道。非常感謝 – topcat3

+0

脫帽致敬。作品的魅力和我學到了很多太。這輝煌解釋 – topcat3

+0

我可以有另一個XSL鍵例如?的的一個頁面上,因爲我需要爲另一個下拉菜單做這個,它說我的xml格式不正確:(。謝謝 – topcat3

0

刪除重複項通常以名稱「分組」的形式出現,如果您在您最喜愛的XSLT教科書的索引中查找分組,您會發現很多信息。

有在XSLT 2.0這個漂亮的解決方案,並在XSLT 1.0醜陋的解決方案,所以答案取決於你使用的是哪個版本,你還沒說非常多。

+0

XSLT 1.0我很害怕 – topcat3