英文:
Can I add this xsl:if test into the xsl:for-each test?
问题
以下是翻译的内容:
这是来自XSL脚本的一部分:
<xsl:for-each select="$HistDB//AssignmentHistory/*/StudentItems/Item/Name">
<xsl:sort select="@Counsel" data-type="number"/>
<xsl:sort select="local-name(../../..)"/>
<xsl:if test=".=$strName and @Counsel">
<tr>
<td style="border: solid 1px black">
<xsl:value-of select="@Counsel"/>
</td>
<td style="border: solid 1px black">
<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:if>
</xsl:for-each>
如何修改for-each
的选择路径以包括xsl:if
子句呢?所以:
$HistDB//AssignmentHistory/*/StudentItems/Item/Name
Name=$strName
- 具有
@Counsel
属性
然后,我可以删除内部的xsl:if
子句,因为xsl:for-each
的选择路径已经包含它。
我尝试过:
<xsl:for-each select="$HistDB//AssignmentHistory/*/StudentItems/Item/Name[$strName and @Counsel]">
但不起作用。
这部分现在可以工作:
<xsl:for-each select="$HistDB//AssignmentHistory/*/StudentItems/Item/Name[@Counsel]">
但当我尝试这样做:
<xsl:for-each select="$HistDB//AssignmentHistory/*/StudentItems/Item/Name[@Counsel]=$strName">
失败。
<details>
<summary>英文:</summary>
Here is a snippet from a XSL script:
<xsl:for-each select="$HistDB//AssignmentHistory/*/StudentItems/Item/Name">
<xsl:sort select="@Counsel" data-type="number"/>
<xsl:sort select="local-name(../../..)"/>
<xsl:if test=".=$strName and @Counsel">
<tr>
<td style="border: solid 1px black">
<xsl:value-of select="@Counsel"/>
</td>
<td style="border: solid 1px black">
<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:if>
</xsl:for-each>
How can I modify the `for-each` selection path to include the `xsl:if` clause? So:
1. `$HistDB//AssignmentHistory/*/StudentItems/Item/Name`
2. `Name=$strName`
3. Has `@Counsel` attribute
Then I can remove the inner `xsl:if` clause because the `xsl:for-each` selection path is already doing it.
I tried:
<xsl:for-each select="$HistDB//AssignmentHistory/*/StudentItems/Item/Name[$strName and @Counsel]">
Doesn't work.
---
This bit now works:
<xsl:for-each select="$HistDB//AssignmentHistory/*/StudentItems/Item/Name[@Counsel]">
But when I try to do:
<xsl:for-each select="$HistDB//AssignmentHistory/*/StudentItems/Item/Name[@Counsel]=$strName">
Fails.
</details>
# 答案1
**得分**: 1
尝试:
```xml
<xsl:for-each select="$HistDB//AssignmentHistory/*/StudentItems/Item/Name[@Counsel and .=$strName]">
未经测试,因为您没有提供可重现的示例。
英文:
Try:
<xsl:for-each select="$HistDB//AssignmentHistory/*/StudentItems/Item/Name[@Counsel and .=$strName]">
Untested, because you did not provide a reproducible example.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论