You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

XSLT中IF条件判断失效,无法显示对应工具的问题求助

问题分析与修正方案

我帮你找出了两处关键错误,这就是为什么你看不到工具列表的原因,咱们一步步来修正:

1. 盒子ID获取错误

在匹配box的模板里,你写了:

<xsl:value-of select="@ID"/>

但你的XML中<box>元素的属性是IDB(比如<box IDB="B1"...>),不是ID,所以这里根本拿不到盒子的标识,得改成@IDB才能正确显示盒子ID。

2. 工具匹配逻辑错误

在findTools模板里,存在两个逻辑问题:

  • 判断条件错误:你用xsl:for-each遍历的是/store/tools/tool/container节点,此时当前上下文就是container节点,你的判断<xsl:if test="container = $currentBOX">是在找当前container的子节点container,这显然不存在,应该用.(代表当前节点的文本内容)来匹配。
  • 工具ID获取错误:IDT属性是在父节点<tool>上,不是<container>节点,所以要通过../@IDT来获取父节点的属性,而不是直接写@IDT(因为<container>没有这个属性)。

修正后的完整XSLT代码

我还做了个小优化:直接在xsl:for-each里筛选出包含对应container的tool节点,省去额外的xsl:if判断,代码更简洁:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
    <xsl:output method="html" encoding="UTF-8" doctype-public="-//W3C//DTD HTML 4.01//EN" doctype-system="http://www.w3.org/TR/html4/strict.dtd" indent="yes" />
    
    <xsl:template match="/">
        <html>
            <head>
                <title>Boxes contents</title>
                <link type="text/css" rel="stylesheet" href="styles.css" />
            </head>
            <body>
                <h1>Boxes contents</h1>
                <ul>
                    <xsl:apply-templates select="/store/boxes/box" />
                </ul>
            </body>
        </html>
    </xsl:template>
    
    <xsl:template match="box" >
        <li>
            <xsl:text>Box </xsl:text>
            <xsl:value-of select="@IDB"/>
            <xsl:text> contains the following tools : </xsl:text>
            <ul>
                <xsl:call-template name="findTools" >
                    <xsl:with-param name="currentBOX" select="@IDB"/>
                </xsl:call-template>
            </ul>
        </li>
    </xsl:template>
    
    <xsl:template name="findTools" >
        <xsl:param name="currentBOX" />
        <xsl:for-each select="/store/tools/tool[container = $currentBOX]">
            <li><xsl:value-of select="@IDT"/></li>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

运行这个修正后的代码,你就能看到每个盒子对应的工具列表了。

内容的提问来源于stack exchange,提问作者beryl

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:44:11