gpt4 book ai didi

xpath - XSLT IF 评估规则

转载 作者:行者123 更新时间:2023-12-03 17:09:33 25 4
gpt4 key购买 nike

从 XML 文件:

    <store >
<tools>
<tool IDT="T1">
<container>B1</container>
<container>B2</container>
</tool>
<tool IDT="T2">
<container>B1</container>
</tool>
<tool IDT="T3">
<container>B2</container>
</tool>
</tools>
<boxes>
<box IDB="B1" height="10" width="20" length="30" weight="4"/>
<box IDB="B2" height="5" width="40" length="30" weight="2"/>
</boxes>
</store>

我尝试为每个框显示进入每个框的工具列表。为此,我编写了以下 XSL:
    <?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="@ID"/>
<xsl:text>contains the following tools : </xsl:text>
</li>
<xsl:call-template name="findTools" >
<xsl:with-param name="currentBOX" select="@IDB"/>
</xsl:call-template>
</xsl:template>

<xsl:template name="findTools" >
<xsl:param name="currentBOX" />
<xsl:for-each select="/store/tools/tool/container" >
<xsl:if test="container = $currentBOX" >
<br><xsl:value-of select="@IDT"/></br>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

当我这样做时,我从来没有看到这些工具。在 OXYGEN 下的调试中,我看到 IF 从来都不是真的。我不明白为什么?我从 XPath 和 XSLT 开始,感谢您的帮助

最佳答案

您已经在 <container> <xsl:for-each> 中的元素.没有 child ,所以选择另一个<container>里面<xsl:if>不会返回任何东西。

您的意思是从 <tool> 执行您的检查。节点。

<xsl:for-each select="/store/tools/tool">
<xsl:if test="container = $currentBOX">
<xsl:value-of select="@IDT"/><br />
</xsl:if>
</xsl:for-each>

这更容易写成
<xsl:for-each select="/store/tools/tool[container = $currentBOX]">
<xsl:value-of select="@IDT"/><br />
</xsl:for-each>

总体而言,编写这两个模板的更直接的方法是:
<xsl:template match="box">
<li>
<xsl:text>Box </xsl:text>
<xsl:value-of select="@ID"/>
<xsl:text>contains the following tools : </xsl:text>
</li>
<xsl:apply-templates select="/store/tools/tool[container = current()/@IDB]" />
</xsl:template>

<xsl:template match="tool">
<xsl:value-of select="@IDT"/><br />
</xsl:template>

或者,您可以使用 <xsl:key>索引 <tool>元素由他们的 <container>值(value):
<xsl:key name="kToolByContainer" match="/store/tools/tool" use="container" />

<xsl:template match="box">
<li>
<xsl:text>Box </xsl:text>
<xsl:value-of select="@ID"/>
<xsl:text>contains the following tools : </xsl:text>
</li>
<xsl:apply-templates select="key('kToolByContainer', @IDB)" />
</xsl:template>

<xsl:template match="tool">
<xsl:value-of select="@IDT"/><br />
</xsl:template>

关于xpath - XSLT IF 评估规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50335380/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com