2010-04-20 6 views
1

테이블 (웹샵 시스템의 옷)에 표시해야하는 RSS 피드가 있습니다. RSS의 이미지는 너비와 높이가 서로 다르기 때문에 모두 보여주는 테이블을 만들고 싶습니다. 첫째로 나는 3 개의 열에있는 모든 항목을 표시하는 것이 행복 할 것이지만 길 아래로 나아가서는 매개 변수를 통해 내 테이블에있는 열의 양을 지정할 수 있어야합니다. 나는 그럴 태그를 표시하고, 오른쪽이 지금까지 내 코드입니다 만드는 문제로 실행 : 모든 "항목"태그가 XML에서 같은 수준에있는 RSS에서다양한 양의 열이있는 테이블을 만드는 XSLT

<xsl:template match="item"> 
    <xsl:choose>  
     <xsl:when test="position() mod 3 = 0 or position()=1"> 
     <tr> 
      <td> 
      <xsl:value-of select="title"/> 
      </td> 
     </tr> 
     </xsl:when> 
     <xsl:otherwise> 
     <td> 
      <xsl:value-of select="title"/> 
     </td> 
     </xsl:otherwise> 
    </xsl:choose>  
    </xsl:template> 

, 그리고 지금까지 내가 필요 제목 만 보여줍니다. Thr 문제는 내가 tr 요소의 끝 태그뿐만 아니라 시작 태그를 지정해야합니다, 그리고 그것에 모든 3 요소를 얻을 수 없다, 아무도 이것을 어떻게 알 수 있습니까?

답변

2

조금 뒤로 물러 설 때. 문제를 작은 부분으로 나누십시오.

<xsl:param name="numColumns" select="3" /> 

<xsl:template match="channel"> 
    <table> 
    <!-- all items that start a column have position() mod x = 1 --> 
    <xsl:apply-templates 
     select="item[position() mod $numColumns = 1]" 
     mode="tr" 
    /> 
    </table> 
</xsl:template> 

<xsl:template match="item" mode="tr"> 
    <tr> 
    <!-- all items make a column: this one (.) and the following x - 1 --> 
    <xsl:apply-templates 
     select=".|following-sibling::item[position() &lt; $numColumns]" 
     mode="td" 
    /> 
    </tr> 
</xsl:template> 

<xsl:template match="item" mode="td"> 
    <td> 
    <!-- calculate optional colspan for the last td --> 
    <xsl:if test="position() = last() and position() &lt; $numColumns"> 
     <xsl:attribute name="colspan"> 
     <xsl:value-of select="$numColumns - position() + 1" /> 
     </xsl:attribute> 
    </xsl:if> 
    <xsl:value-of select="title"/> 
    </td> 
</xsl:template> 
+0

우수. 나는 그것을하는 방법을 찾기 위해 반감기를 보냈다. 고마워요! – Claudix

+0

@Claudix 대단히 환영합니다. 오래된 재료가 여전히 사람들을 돕는 것을 보니 반갑습니다. – Tomalak

+0

아무것도 인터넷에서 죽지 않습니다 :-) – Claudix

관련 문제