我可以将这个 xsl:if 测试添加到 xsl:for-each 测试中吗?

huangapple go评论48阅读模式
英文:

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子句呢?所以:

  1. $HistDB//AssignmentHistory/*/StudentItems/Item/Name
  2. Name=$strName
  3. 具有@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:

	&lt;xsl:for-each select=&quot;$HistDB//AssignmentHistory/*/StudentItems/Item/Name&quot;&gt;
		&lt;xsl:sort select=&quot;@Counsel&quot; data-type=&quot;number&quot;/&gt;
		&lt;xsl:sort select=&quot;local-name(../../..)&quot;/&gt;
		
		&lt;xsl:if test=&quot;.=$strName and @Counsel&quot;&gt;
			&lt;tr&gt;
				&lt;td style=&quot;border: solid 1px black&quot;&gt;
					&lt;xsl:value-of select=&quot;@Counsel&quot;/&gt;
				&lt;/td&gt;
				&lt;td style=&quot;border: solid 1px black&quot;&gt;
					&lt;xsl:variable name=&quot;strWeek&quot; select=&quot;local-name(../../..)&quot;/&gt;
					&lt;xsl:value-of select=&quot;substring($strWeek, 8, 2)&quot;/&gt;
					&lt;xsl:text&gt;/&lt;/xsl:text&gt;
					&lt;xsl:value-of select=&quot;substring($strWeek, 5, 2)&quot;/&gt;
					&lt;xsl:text&gt;/&lt;/xsl:text&gt;
					&lt;xsl:value-of select=&quot;substring($strWeek, 2, 4)&quot;/&gt;
				&lt;/td&gt;
			&lt;/tr&gt;
		&lt;/xsl:if&gt;
	&lt;/xsl:for-each&gt;

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:

    &lt;xsl:for-each select=&quot;$HistDB//AssignmentHistory/*/StudentItems/Item/Name[$strName and @Counsel]&quot;&gt;

Doesn&#39;t work.
---
This bit now works:

    &lt;xsl:for-each select=&quot;$HistDB//AssignmentHistory/*/StudentItems/Item/Name[@Counsel]&quot;&gt;

But when I try to do:

    &lt;xsl:for-each select=&quot;$HistDB//AssignmentHistory/*/StudentItems/Item/Name[@Counsel]=$strName&quot;&gt;

Fails.

</details>


# 答案1
**得分**: 1

尝试:

```xml
<xsl:for-each select="$HistDB//AssignmentHistory/*/StudentItems/Item/Name[@Counsel and .=$strName]">

未经测试,因为您没有提供可重现的示例。

英文:

Try:

&lt;xsl:for-each select=&quot;$HistDB//AssignmentHistory/*/StudentItems/Item/Name[@Counsel and .=$strName]&quot;&gt;

Untested, because you did not provide a reproducible example.

huangapple
  • 本文由 发表于 2023年7月13日 21:39:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76680040.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定