2016-01-28 30 views
1

我有以下XSL腳本:在for-each循環中選擇特定條目?

<xsl:for-each select="$AssignHistory/AssignmentHistory/*[name()=$week]/StudentItems/Item"> 
    Student (or assistant): <xsl:value-of select="Name"/><br /> 
</xsl:for-each> 

實際的XML StudentItems將在它的項目數目不同。或者:

1 
7 
14 
21 

如果有7個,然後我要顯示的列表內容是這樣的:

如果有14:

1 
2/3 
4/5 
6/7 
8 
9/10 
11/12 
13/14 

最後,如果有21 :

1 
2/3 
4/5 
6/7 
8 
9/10 
11/12 
13/14 
15 
16/17 
18/19 
20/21 

此刻我只是開始g不同的名單(可以理解),但我可以做到以上嗎?例如XML內容:

<StudentItems> 
     <Item> 
      <Name Counsel="10">Matthew 1</Name> 
      <Type>Bible Reading (Main)</Type> 
     </Item> 
     <Item> 
      <Name Counsel="44">John 2</Name> 
      <Type>#1 Student (Main)</Type> 
     </Item> 
     <Item> 
      <Name>Robert 3</Name> 
      <Type>Assistant</Type> 
     </Item> 
     <Item> 
      <Name Counsel="38">Rachel 4</Name> 
      <Type>#2 Student (Main)</Type> 
     </Item> 
     <Item> 
      <Name>Aimie 5</Name> 
      <Type>Assistant</Type> 
     </Item> 
     <Item> 
      <Name Counsel="48">Julie 6</Name> 
      <Type>#3 Student (Main)</Type> 
     </Item> 
     <Item> 
      <Name>Diana 7</Name> 
      <Type>Assistant</Type> 
     </Item> 
     <Item> 
      <Name Counsel="5">Gordon 8</Name> 
      <Type>Bible Reading (Aux)</Type> 
     </Item> 
     <Item> 
      <Name Counsel="39">Sadie 9</Name> 
      <Type>#1 Student (Aux)</Type> 
     </Item> 
     <Item> 
      <Name>Bethany 1</Name> 
      <Type>Assistant</Type> 
     </Item> 
     <Item> 
      <Name Counsel="38">Zoe 2</Name> 
      <Type>#2 Student (Aux)</Type> 
     </Item> 
     <Item> 
      <Name>Angela 3</Name> 
      <Type>Assistant</Type> 
     </Item> 
     <Item> 
      <Name Counsel="37">Mary 4</Name> 
      <Type>#3 Student (Aux)</Type> 
     </Item> 
     <Item> 
      <Name>Kate 5</Name> 
      <Type>Assistant</Type> 
     </Item> 
    </StudentItems> 

那麼,我提到了一些,我指的是在列表中項目/名稱。感謝您的幫助。

+0

你可以張貼的預期輸出**的**代碼?不確定你的符號是什麼意思。 –

+0

我已經修改它以顯示代碼輸出。每個數字表示「項目/名稱」列表中的索引位置。 –

回答

1

我會建議,而不是:

<xsl:for-each select="$AssignHistory/AssignmentHistory/*[name()=$week]/StudentItems/Item"> 
    Student (or assistant): <xsl:value-of select="Name"/><br /> 
</xsl:for-each> 

你這樣做:

<xsl:apply-templates select="$AssignHistory/AssignmentHistory/*[name()=$week]/StudentItems"/> 

然後將這些模板:

<xsl:template match="StudentItems"> 
    <xsl:apply-templates select="Item[not((position() - 1) mod 7) or ((position() - 1) mod 7) mod 2] "/> 
</xsl:template> 

<xsl:template match="Item"> 
    <xsl:value-of select="Name"/> 
    <xsl:if test="(position() - 1) mod 4"> 
     <xsl:apply-templates select="following-sibling::Item[1]" mode="follower"/> 
    </xsl:if> 
    <br/> 
</xsl:template> 

<xsl:template match="Item" mode="follower"> 
    <xsl:text>/</xsl:text> 
    <xsl:value-of select="Name"/> 
</xsl:template> 

是否有很多工作來調整它,以便在1之前它表示 「Main」,並且在8之前表示「輔助類別1」,在 之前的行表示「輔助類別2" ?

如何:

<xsl:template match="StudentItems"> 
    <xsl:apply-templates select="Item[position() mod 7 = 1]" mode="leader"/> 
</xsl:template> 

<xsl:template match="Item" mode="leader"> 
    <xsl:choose> 
     <xsl:when test="position() = 1">Main </xsl:when> 
     <xsl:when test="position() = 2">Auxiliary Class 1 </xsl:when> 
     <xsl:otherwise>Auxiliary Class 2 </xsl:otherwise> 
    </xsl:choose> 
    <xsl:value-of select="Name"/> 
    <br/> 
    <xsl:apply-templates select="following-sibling::Item[position() &lt;= 6 and position() mod 2]"/> 
</xsl:template> 

<xsl:template match="Item"> 
    <xsl:value-of select="Name"/> 
    <xsl:apply-templates select="following-sibling::Item[1]" mode="follower"/> 
    <br/> 
</xsl:template> 

<xsl:template match="Item" mode="follower"> 
    <xsl:text>/</xsl:text> 
    <xsl:value-of select="Name"/> 
</xsl:template> 
+0

這太棒了。你很聰明!是否需要做很多工作來調整它,以便在1之前它表示「Main」,在8之前表示「輔助類1」,並且在15之前表示「輔助類2」? –

+1

@AndrewTruckle請參閱我的回答。 –

+0

謝謝!工作過一種享受! –

0

如果我的理解正確,如果有助理,那麼你希望旁邊的學生的名字。如果沒有助理,那麼你只需要學生。你爲什麼不圍繞<br/>關於類型的if語句?例如,像這樣:

<xsl-if test="Type='Assistant"> 
    <xsl:value-of select="Name"/> 
</xsl:if> 
<xsl-if test="Type='Student'"><br/> 
    <xsl:value-of select="Name"/> 
</xsl:if> 
+0

由於最終文檔是用多種語言編寫的,因此類型會發生變化。在所有30種左右的語言中,一個常數是相同的,即列表中的位置。 –