Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xslt 如何添加使用查找表检查xml数据的条件_Xslt - Fatal编程技术网

Xslt 如何添加使用查找表检查xml数据的条件

Xslt 如何添加使用查找表检查xml数据的条件,xslt,Xslt,我想问一下。可以添加一个条件,该条件将使用查找表检查xml数据,如果我们在查找表中没有值,是否将常量8添加到输出? xslt代码: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:key name="Department" match="Department" use="../Collection"/> <xsl:template mat

我想问一下。可以添加一个条件,该条件将使用查找表检查xml数据,如果我们在查找表中没有值,是否将常量8添加到输出? xslt代码:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="Department" match="Department" use="../Collection"/>
    <xsl:template match="/">
    <document>
        <xsl:apply-templates/>
    </document>
</xsl:template>
 <xsl:template match="line">
     <xsl:variable name="inputDep" select="field[@id='3']"/>
<Department>
    <xsl:for-each select="document('lookup.xml')">
        <xsl:for-each select="key('Deparment',$inputDep)">
            <xsl:value-of select="."/>
        </xsl:for-each>
    </xsl:for-each>
</Department>

 </xsl:template>

</xsl:stylesheet>

查找表:

<document>
    <line-item>
        <Collection>1</Collection>
        <Department>3</Department>
    </line-item>
    <line-item>
        <Collection>2</Collection>
        <Department>1</Department>
    </line-item>
    <line-item>
        <Collection>3</Collection>
        <Department>2</Department>
    </line-item>
</document>

1.
3.
2.
1.
3.
2.
xml文件:

<document>
    <line id="0">
        <field id="3"><![CDATA[1]]></field>
    </line>
    <line id="1">
        <field id="3"/>
    </line>
    <line id="2">
        <field id="3"/><![CDATA[4]]></field>
    </line>
</document>

结果:

<Department>3<Department>
<Department>8<Department>
<Department>8<Department>
3
8.
8.

您可以将查找的值分配给变量,并根据是否找到任何内容来选择输出内容

编辑2:完整的演示样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:key name="Department" match="Department" use="../Collection"/>

  <xsl:template match="/">
    <document>
      <xsl:apply-templates/>
    </document>
  </xsl:template>

  <xsl:template match="line">
    <xsl:variable name="inputDep" select="field[@id='3']"/>
    <Department>
      <xsl:for-each select="document('lookup.xml')">
        <xsl:variable name="value" select="key('Department',$inputDep)"/>
        <xsl:choose>
          <xsl:when test="$value">
            <xsl:value-of select="$value"/> <!-- see note -->
          </xsl:when>
          <xsl:otherwise>8</xsl:otherwise>
        </xsl:choose>
      </xsl:for-each>
    </Department>
  </xsl:template>

</xsl:stylesheet>

8.

注意:将原始样式表中的
xsl:for-each
循环替换为简单的
xsl:value
,假设循环值不是有意的。如果确实是这样,您可以将其替换为for each循环。

为什么每个循环都使用内部for?您是否希望使用Deparment(sic)键的查找将返回多个节点?如果输出正确,输出是否正确?对于每个查找表,内部为非xml文件。我试着用你的答案,但没用。。我尝试添加您的代码,而不是为每个循环添加代码。@Petras试图通过提供一个更完整的示例代码来说明我的想法,从而澄清我的答案。我尝试您的解决方案,首先在线显示错误。我尝试将select值更改为/field[@id=3],但所有部门=8@Petras,您会收到什么错误消息?该部分直接来自您自己的示例代码。未知命名键。我试图在altova 2011 rel.2上继续执行此代码