在 for-each 循环中选择特定条目?
Selecting specific entries in a for-each loop?
我有以下 XSL 脚本:
<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个,那么我想这样显示列表内容:
1
2 / 3
4 / 5
6 / 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
目前我刚刚获得了不同的 "list" 名称(可以理解),但我可以执行上述操作吗?示例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>
所以我提到数字的地方是指列表中的 Item/Name。感谢您的帮助。
如果我没理解错的话,如果有助手,那么你想要学生旁边的名字。如果没有助手,那么你只需要学生。为什么不用关于类型的 if 语句将 <br/>
括起来?例如,像这样:
<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>
我建议,而不是:
<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>
Would it be much work to tweak it so that on the line before 1 it says
"Main" and on the line before 8 it says "Auxiliary Class 1" and on the
line before 15 it says "Auxiliary Class 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() <= 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>
我有以下 XSL 脚本:
<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个,那么我想这样显示列表内容:
1
2 / 3
4 / 5
6 / 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
目前我刚刚获得了不同的 "list" 名称(可以理解),但我可以执行上述操作吗?示例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>
所以我提到数字的地方是指列表中的 Item/Name。感谢您的帮助。
如果我没理解错的话,如果有助手,那么你想要学生旁边的名字。如果没有助手,那么你只需要学生。为什么不用关于类型的 if 语句将 <br/>
括起来?例如,像这样:
<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>
我建议,而不是:
<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>
Would it be much work to tweak it so that on the line before 1 it says "Main" and on the line before 8 it says "Auxiliary Class 1" and on the line before 15 it says "Auxiliary Class 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() <= 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>