在 for-each 循环中选择特定条目? [英] Selecting specific entries in a for-each loop?

查看:23
本文介绍了在 for-each 循环中选择特定条目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 XSL 脚本:

学生(或助理):<xsl:value-of select="Name"/><br/></xsl:for-each>

实际的 XML StudentItems 中将包含不同数量的项目.要么:

171421

如果有7个,那么我想显示这样的列表内容:

12/34/56/7

如果有 14 个:

12/34/56/789/1011/1213/14

最后,如果有 21 个:

12/34/56/789/1011/1213/141516/1718/1920/21

目前我只是得到了各种各样的名字列表"(可以理解),但我可以做到以上几点吗?XML 内容示例:

 <物品><Name Counsel="10">Matthew 1</Name><Type>圣经阅读(主要)</Type></项目><物品><Name Counsel="44">John 2</Name><Type>#1 Student (Main)</Type></项目><物品><姓名>罗伯特 3</姓名><类型>助手</类型></项目><物品><Name Counsel="38">Rachel 4</Name><Type>#2 Student (Main)</Type></项目><物品><姓名>艾米 5</姓名><类型>助手</类型></项目><物品><Name Counsel="48">Julie 6</Name><Type>#3 Student (Main)</Type></项目><物品><姓名>戴安娜 7</姓名><类型>助手</类型></项目><物品><Name Counsel="5">Gordon 8</Name><Type>圣经阅读(辅助)</Type></项目><物品><Name Counsel="39">Sadie 9</Name><Type>#1 Student (Aux)</Type></项目><物品><姓名>伯大尼 1</姓名><类型>助手</类型></项目><物品><Name Counsel="38">Zoe 2</Name><Type>#2 Student (Aux)</Type></项目><物品><姓名>安吉拉 3</姓名><类型>助手</类型></项目><物品><Name Counsel="37">Mary 4</Name><Type>#3 Student (Aux)</Type></项目><物品><姓名>凯特 5</姓名><类型>助手</类型></项目></StudentItems>

所以我提到的数字是指列表中的项目/名称.感谢您的帮助.

解决方案

我建议改为:

学生(或助理):<xsl:value-of select="Name"/><br/></xsl:for-each>

你愿意:

然后添加这些模板:

<xsl:apply-templates select="Item[not((position() - 1) mod 7) or ((position() - 1) mod 7) mod 2] "/></xsl:模板><xsl:template match="项目"><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:模板><xsl:template match="Item" mode="follower"><xsl:text>/</xsl:text><xsl:value-of select="Name"/></xsl:模板>

<小时><块引用>

调整它是否需要做很多工作,以便在 1 之前它说Main",在 8 点之前的那一行上写着Auxiliary Class 1",在15 行之前是辅助类 2"吗?

怎么样:

<xsl:apply-templates select="Item[position() mod 7 = 1]" mode="leader"/></xsl:模板><xsl:template match="Item" mode="leader"><xsl:when test="position() = 1">Main </xsl:when><xsl:when test="position() = 2">辅助类1 </xsl:when><xsl:otherwise>辅助类 2 </xsl:otherwise></xsl:选择><xsl:value-of select="Name"/><br/><xsl:apply-templates select="following-sibling::Item[position() &lt;= 6 and position() mod 2]"/></xsl:模板><xsl:template match="项目"><xsl:value-of select="Name"/><xsl:apply-templates select="following-sibling::Item[1]" mode="follower"/><br/></xsl:模板><xsl:template match="Item" mode="follower"><xsl:text>/</xsl:text><xsl:value-of select="Name"/></xsl:模板>

I have the following XSL script:

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

The actual XML StudentItems will have a varied number of items in it. Either:

1
7
14
21

If there are 7, then I want to show the list content like this:

1
2 / 3
4 / 5
6 / 7

If there are 14:

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

Finally, if there are 21:

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

At the moment I am just getting the varied "list" of names (understandably) but can I do the above? Example XML content:

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

So where I mentioned a number, I am referring to the Item/Name in the list. Thank you for any assistance.

解决方案

I would suggest that instead of:

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

you do:

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

Then add these templates:

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

How about:

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

这篇关于在 for-each 循环中选择特定条目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆