【发布时间】:2016-01-30 14:57:07
【问题描述】:
对于昨天提供给我的一个问题,我有一个很好的答案:
Selecting specific entries in a for-each loop?
由于打印页面的空间限制,我想将数据布局在表格中。这是一个在 Microsoft Expression Web 中修改的示例:
所以,我想采用我正在使用的当前 XSL 代码:
<xsl:variable name="AssignHistory" select="document('AssignHistory.xml')"/>
<xsl:variable name="week" select="Date/@NextWeek"/>
<xsl:apply-templates select="$AssignHistory/AssignmentHistory/*[name()=$week]/StudentItems"/>
以及模板功能:
<xsl:template match="StudentItems">
<xsl:apply-templates select="Item[not((position() - 1) mod 7)]" mode="leader"/>
</xsl:template>
<xsl:template match="Item" mode="leader">
<xsl:value-of select="Description"/>
: 
<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="Description"/>
: 
<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>
因此,最终表格将有 1、2 或 3 列。
如果列表中有 7 个学生项目,则它有 1 列内容。 如果有 14 个项目,则项目 7 到 14 显示在第二列中。 如果有 21 项,则第 15 至 21 项显示在第三列中。
唯一的其他区别是:
- 第一列将包含以“描述”值为前缀的行。
- 会有一个标题行说明“学校”(硬编码文本)。除非只有 1 列,否则不需要此行。
是否可以通过任何方式调整这些模板以创建此修改后的输出?
这里有一些修改过的 XML 数据可供使用:
<StudentItems>
<Item>
<Name Counsel="9">1</Name>
<Type>Bible Reading (Main)</Type>
<Description>Bible Reading</Description>
</Item>
<Item Description="Initial Call">
<Name Counsel="37">2</Name>
<Type>#1 Student (Main)</Type>
<Description>Initial Call</Description>
</Item>
<Item>
<Name>3</Name>
<Type>Assistant</Type>
<Description>Initial Call</Description>
</Item>
<Item>
<Name Counsel="48">4</Name>
<Type>#2 Student (Main)</Type>
<Description>Return Visit</Description>
</Item>
<Item>
<Name>5</Name>
<Type>Assistant</Type>
<Description>Return Visit</Description>
</Item>
<Item>
<Name Counsel="27">6</Name>
<Type>#3 Student (Main)</Type>
<Description>Bible Study</Description>
</Item>
<Item>
<Name>7</Name>
<Type>Assistant</Type>
<Description>Bible Study</Description>
</Item>
</StudentItems>
非常感谢您的热心帮助。我很感激。
我进步很大。不知道是不是最好的办法:
<table cellpadding="2">
<xsl:apply-templates select="$AssignHistory/AssignmentHistory/*[name()=$week]/StudentItems"/>
</table>
和
<xsl:template match="StudentItems">
<xsl:if test="Item[position() = 1]">
<xsl:if test="count(Item) = 14">
<tr>
<td>
<b>Main</b>
</td>
<td>
<b>Auxiliary Class 1</b>
</td>
</tr>
</xsl:if>
<xsl:if test="count(Item) = 21">
<tr>
<td>
<b>Main</b>
</td>
<td>
<b>Auxiliary Class 1</b>
</td>
<td>
<b>Auxiliary Class 2</b>
</td>
</tr>
</xsl:if>
</xsl:if>
<tr>
<xsl:apply-templates select="Item[not((position() - 1) mod 7)]" mode="leader"/>
</tr>
</xsl:template>
<xsl:template match="Item" mode="leader">
<td>
<b><xsl:value-of select="Description"/>: </b>
<xsl:value-of select="Name"/>
<br/>
<xsl:apply-templates select="following-sibling::Item[position() <= 6 and position() mod 2]"/>
</td>
</xsl:template>
<xsl:template match="Item">
<b><xsl:value-of select="Description"/>: </b>
<xsl:value-of select="Name"/>
<xsl:apply-templates select="following-sibling::Item[1]" mode="follower"/>
<xsl:if test="position() != 3">
<br/>
</xsl:if>
</xsl:template>
<xsl:template match="Item" mode="follower">
<xsl:text> / </xsl:text>
<xsl:value-of select="Name"/>
</xsl:template>
这似乎运作良好。但我只想要第一列上的“描述”标签,我不知道如何从其他列中删除它:
我继续尝试应用建议的内容(内联表)并且代码更简单:
<xsl:template match="StudentItems">
<xsl:apply-templates select="Item[not((position() - 1) mod 7)]" mode="leader"/>
</xsl:template>
<xsl:template match="Item" mode="leader">
<table cellpadding="2" style="float:left;">
<tr>
<td>
<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>
</td>
</tr>
<tr>
<td>
<b>
<xsl:value-of select="Description"/>: 
</b>
<xsl:value-of select="Name"/>
</td>
</tr>
<xsl:apply-templates select="following-sibling::Item[position() <= 6 and position() mod 2]"/>
</table>
</xsl:template>
<xsl:template match="Item">
<tr>
<td>
<b>
<xsl:value-of select="Description"/>: 
</b>
<xsl:value-of select="Name"/>
<xsl:apply-templates select="following-sibling::Item[1]" mode="follower"/>
</td>
</tr>
</xsl:template>
<xsl:template match="Item" mode="follower">
<xsl:text> / </xsl:text>
<xsl:value-of select="Name"/>
</xsl:template>
这似乎更简单。谢谢。但我还有两个问题:
如果 StudentItems 列表中只有 7 项(或更少),我不想要标题行。
我不想为第二个或第三个表添加“描述”。
这个可以吗?
【问题讨论】:
-
我建议您尝试为每组 7 人创建一个表格,并将它们内联显示。我认为这会更容易实现。
-
我不确定你对内联表的意思。对不起。
-
好的,我现在可以看到如何使用
style="float: left;"语法并排(或三个)放置两个表。谢谢。但是您能就模板脚本的更改给我建议吗?我不清楚如何修改它。对不起。 :( -
我对模板进行了更改。主题中的新代码。仍然有几个问题。