英文:
Building a conditional text string based on the attribute values not found in the for-each loop
问题
这个for-each
代码段显示输出(包括格式化)如下所示:
所有辅导员编号将在1到20的范围内。我想在我的表格之后显示一个逗号分隔的列表,列出学生没有完成的辅导员编号。在上面的示例中,我想看到:
未分配:1, 3, 4, 6, 9, 11, 13, 14, 15, 16, 17, 18, 19, 20
我支持我的XSL可能可以简化为一个简单的文本案例。但是,我想要做的事情可以通过XSLT-1来实现吗?
我正在尝试采用建议的答案,代码如下:
<xsl:variable name="missing-items">
<xsl:call-template name="list-missing-items">
<xsl:with-param name="items" select="$HistDB//AssignmentHistory/*/StudentItems/Item/Name[@Counsel and .=$strName]"/>
</xsl:call-template>
</xsl:variable>
<missing-items>
<xsl:value-of select="translate(normalize-space($missing-items), ' ', ',')"/>
</missing-items>
但是所有20个数字都被列出。
英文:
This this for-each
snippet:
<xsl:for-each select="$HistDB//AssignmentHistory/*/StudentItems/Item/Name[@Counsel and .=$strName]">
<!-- Sort by counsel number (ascending) -->
<xsl:sort select="@Counsel" data-type="number"/>
<!-- Then sort by date (ascending) -->
<xsl:sort select="local-name(../../..)"/>
<tr>
<!-- Counsel -->
<td class="counsel">
<xsl:value-of select="@Counsel"/>
</td>
<!-- Date DD/MM/YYYY -->
<td class="counsel">
<xsl:variable name="strWeek" select="local-name(../../..)"/>
<xsl:value-of select="substring($strWeek, 8, 2)"/>
<xsl:text>/</xsl:text>
<xsl:value-of select="substring($strWeek, 5, 2)"/>
<xsl:text>/</xsl:text>
<xsl:value-of select="substring($strWeek, 2, 4)"/>
</td>
</tr>
</xsl:for-each>
It displays output (with formatting) like this:
All the counsel numbers will be in the range 1 - 20. I would like to show a comma separated list after my table for the counsel numbers that the student has not done. In the above example I would like to see:
> Not assigned: 1, 3, 4, 6, 9, 11, 13, 14, 15, 16, 17, 18, 19, 20
I support my XSL here could be stripped down to a simple text case. But can what I want to do be achieved with XSLT-1?
I am trying to adopt the suggested answer with:
<xsl:variable name="missing-items">
<xsl:call-template name="list-missing-items">
<xsl:with-param name="items" select ="$HistDB//AssignmentHistory/*/StudentItems/Item/Name[@Counsel and .=$strName]"/>
</xsl:call-template>
</xsl:variable>
<missing-items>
<xsl:value-of select="translate(normalize-space($missing-items), ' ', ',')" />
</missing-items>
But all 20 numbers are being listed.
答案1
得分: 1
以下是翻译好的部分:
考虑以下简化示例:
XML
<root>
<item>2</item>
<item>2</item>
<item>5</item>
<item>7</item>
<item>10</item>
<item>10</item>
<item>10</item>
<item>10</item>
<item>12</item>
</root>
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/root">
<xsl:variable name="missing-items">
<xsl:call-template name="list-missing-items">
<xsl:with-param name="items" select="item"/>
</xsl:call-template>
</xsl:variable>
<missing-items>
<xsl:value-of select="translate(normalize-space($missing-items), ' ', ',')"/>
</missing-items>
</xsl:template>
<xsl:template name="list-missing-items">
<xsl:param name="items"/>
<xsl:param name="max" select="20"/>
<xsl:if test="$max > 1">
<xsl:call-template name="list-missing-items">
<xsl:with-param name="items" select="$items"/>
<xsl:with-param name="max" select="$max - 1"/>
</xsl:call-template>
</xsl:if>
<xsl:if test="not($max=$items)">
<xsl:value-of select="$max"/>
<xsl:text> </xsl:text>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
结果
<?xml version="1.0" encoding="UTF-8"?>
<missing-items>1,3,4,6,8,9,11,13,14,15,16,17,18,19,20</missing-items>
英文:
Consider the following simplified example:
XML
<root>
<item>2</item>
<item>2</item>
<item>5</item>
<item>7</item>
<item>10</item>
<item>10</item>
<item>10</item>
<item>10</item>
<item>12</item>
</root>
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/root">
<xsl:variable name="missing-items">
<xsl:call-template name="list-missing-items">
<xsl:with-param name="items" select="item"/>
</xsl:call-template>
</xsl:variable>
<missing-items>
<xsl:value-of select="translate(normalize-space($missing-items), ' ', ',')" />
</missing-items>
</xsl:template>
<xsl:template name="list-missing-items">
<xsl:param name="items"/>
<xsl:param name="max" select="20"/>
<xsl:if test="$max > 1">
<xsl:call-template name="list-missing-items">
<xsl:with-param name="items" select="$items"/>
<xsl:with-param name="max" select="$max - 1"/>
</xsl:call-template>
</xsl:if>
<xsl:if test="not($max=$items)">
<xsl:value-of select="$max" />
<xsl:text> </xsl:text>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Result
<?xml version="1.0" encoding="UTF-8"?>
<missing-items>1,3,4,6,8,9,11,13,14,15,16,17,18,19,20</missing-items>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论