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检查中的节点可用性_Xslt_Xslt 1.0 - Fatal编程技术网

XSLT检查中的节点可用性

XSLT检查中的节点可用性,xslt,xslt-1.0,Xslt,Xslt 1.0,我在我的项目中使用XSLT1.0。在XSLT转换中,我必须检查特定元素,如果存在,我必须执行一些连接或其他连接操作 然而,我在这里找不到像某些内置函数那样的选项 要求就像 <Root> <a></a> <b></b> <c></c> </Root> 这里是元素,进入请求负载,然后我们需要执行和的串联,否则和您可以通过模板匹配来实现: <xsl:template match="R

我在我的项目中使用XSLT1.0。在XSLT转换中,我必须检查特定元素,如果存在,我必须执行一些连接或其他连接操作

然而,我在这里找不到像某些内置函数那样的选项

要求就像

<Root>
  <a></a>
  <b></b>
  <c></c>
</Root>


这里是元素
,进入请求负载,然后我们需要执行
的串联,否则

您可以通过模板匹配来实现:

<xsl:template match="Root[not(a)]">
  <xsl:value-of select="concat(c, b)"/>
</xsl:template>

<xsl:template match="Root[a]">
  <xsl:value-of select="concat(b, c)"/>
</xsl:template>

尝试以下方法:

<xsl:template match="/Root">
    <xsl:choose>
        <xsl:when test="a">
            <!-- do something -->
        </xsl:when>
        <xsl:otherwise>
            <!-- do something else -->
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>


解释:测试返回表达式
a
选择的节点集的布尔值。如果节点集非空,则结果为真。

使用
xsl:choose

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"   
    version="1.0">

    <xsl:template match="/Root"> 
        <xsl:choose>
            <xsl:when test="a">
                <xsl:value-of select="concat(c, b)"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="concat(b, c)"/>
            </xsl:otherwise>
        </xsl:choose>  
    </xsl:template>

</xsl:stylesheet>

或在模板匹配的谓词中:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"   
    version="1.0">

    <xsl:template match="/Root[a]"> 
         <xsl:value-of select="concat(c, b)"/> 
    </xsl:template>

    <xsl:template match="/Root[not(a)]"> 
         <xsl:value-of select="concat(b, c)"/>
    </xsl:template>

</xsl:stylesheet>

在您的情况下,使用
选择
并在相应的xpath上使用
boolean()
测试是否存在
a

<xsl:template match="Root">
  <xsl:choose>
    <xsl:when test="boolean(./a)">
      <xsl:value-of select="concat(./b, ./c)" />
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="concat(./c, ./b)" />
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

kumarb,如果您不理解此提示:在提出新问题之前,请接受以下答案之一。