2016-08-30 4 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> 

난 그냥 통해 2를 얻기 위해 원하는 경우 제 4 국가 태그, 그 일을 어떻게 하죠? 나는 position()을 시도했지만 숫자를 얻는 대신 그룹 키 값 (예 : CA)을 얻고 있습니다. 어떤 도움을 크게 감상 할 수

<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>

:

는 소스 XML이다. 미리 감사드립니다.

+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 = "국가 [위치() ge 2 및 위치() le 4]" –

+0

그룹화 기준에 따라 구분 기호를 넣어야한다고 생각했습니다. 'position() ge 2와 position() le 4'에 의해 당신은 두 개의 그룹을 얻을 것입니다 : 하나는 표현식이 참이고 하나는 거짓입니다. 함께 두 그룹에는 ** 모든 ** 원래 위치가 포함됩니다. 정렬 전에 위치가 계산된다는 점에 유의하십시오. –

+0

P. 질문에 대한 답변이 있으면 대답을 수락하여 질문을 닫으십시오. –