2016-08-30 61 views
0

我對使用組有所不同,希望能夠使用position()選擇組的子集。XSLT2選擇一個組的子集

我已經有這個代碼的工作,因爲我想(根據需要創建組和正確的輸出)。

<xsl:template match="Locations"> 
    <table> 
     <tbody> 
     <xsl:for-each-group select="Location" group-by="Country"> 
      <xsl:sort data-type="text" order="ascending" select="."/> 
      <xsl:for-each-group select="current-group()" group-by="current-grouping-key()"> 
      </xsl:for-each-group> 
      <tr> 
       <td class="country"><xsl:value-of select="current-grouping-key()"/></td> 
      </tr> 
      <xsl:for-each-group select="current-group()" group-by="City"> 
       <xsl:sort data-type="text" order="ascending" select="current-grouping-key()"/> 
       <tr> 
        <td class="city"><xsl:value-of select="fn:current-grouping-key()"/></td> 
       </tr> 
      </xsl:for-each-group> 
     </xsl:for-each-group> 
     </tbody> 
    </table> 
</xsl:template> 

如果我想只是打通第二第四國家標籤,我會怎麼做呢?我嘗試了position(),但不是獲取一個數字,而是得到了分組鍵值,例如CA.

這是源XML:

<Locations> <Location> <Country>US</Country> <City>New York</City> </Location> <Location> <Country>US</Country> <City>Houston</City> </Location> <Location> <Country>MX</Country> <City>Cancun</City> </Location> <Location> <Country>CH</Country> <City>Zurich</City> </Location> <Location> <Country>CA</Country> <City>Toronto</City> </Location> <Location> <Country>DE</Country> <City>Berlin</City> </Location> <Location> <Country>JP</Country> <City>Tokyo</City> </Location> <Location> <Country>GB</Country> <City>London</City> </Location> <Location> <Country>CA</Country> <City>Edmonton</City> </Location> </Locations>

任何幫助將不勝感激。提前致謝。

+1

你能告訴你在這種情況下,期待輸出?如果你嘗試使用'position()',它可能也會有所幫助。謝謝! –

回答

1

如果我只想得到第2到第4個國家/地區代碼,我怎麼會這樣做呢?

我猜你會做:

<xsl:template match="Locations"> 
    <table border="1"> 
     <tbody> 
      <xsl:for-each-group select="Location" group-by="Country"> 
       <xsl:sort select="." data-type="text" order="ascending"/> 
       <xsl:if test="position() ge 2 and position() le 4"> 
        <tr> 
         <td class="country"> 
          <xsl:value-of select="current-grouping-key()"/> 
         </td> 
        </tr> 
        <xsl:for-each-group select="current-group()" group-by="City"> 
         <xsl:sort select="." data-type="text" order="ascending"/> 
         <tr> 
          <td class="city"> 
           <xsl:value-of select="current-grouping-key()"/> 
          </td> 
         </tr> 
        </xsl:for-each-group> 
       </xsl:if> 
      </xsl:for-each-group> 
     </tbody> 
    </table> 
</xsl:template> 
+0

謝謝!我想我必須在分組中選擇一個分隔符,如下所示:group-by =「Country [position()ge 2 and position()le 4]」 –

+0

實際的語法放在一邊,如果你分組通過'position()ge 2和position()le 4',你將得到2個組:一個表達式爲真,另一個爲假。這兩個小組一起將包含所有**原始位置。還要注意的是,排序前將計算位置。 –

+0

P.S.如果您的問題得到解答,請通過接受答案關閉它。 –

相關問題