xpath - XSLT IF 评估规则

标签 xpath xslt-2.0

从 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/

相关文章:

python - XPath 删除列表 Python 中的空格

xslt - xsl : Can a named template return a node list?

xml - 如何在 xslt 中使用 xpath 作为变量?

java - 将 XPath 转换为 Jsoup 的 CssSelector

JavaScript Xpath : return result as string

xpath - 使用 IMPORTXML xpath 导入表

java - 如何在没有从java输入xml的情况下执行xsl转换

java - 如何定位列表元素(Selenium)?

xml - 如何按内容对元素进行分组(XSLT 2.0)?

xml - 无法在 xslt 中使用 for every 提取唯一值