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
Xml 转换Xsl时如何确定节点是否存在?_Xml_Xslt - Fatal编程技术网

Xml 转换Xsl时如何确定节点是否存在?

Xml 转换Xsl时如何确定节点是否存在?,xml,xslt,Xml,Xslt,如何确定输入xml中是否存在给定节点 如果节点存在,我希望使用reportAuthor1的值,否则我将使用reportAuthor的值。我尝试使用if和else都没有成功 <xsl:choose> <xsl:when test="reportAuthor1=''"> <xsl:value-of select="reportAuthor"/> </xsl:when> <xsl:otherwise> <xsl:value-

如何确定输入xml中是否存在给定节点

如果节点存在,我希望使用
reportAuthor1
的值,否则我将使用
reportAuthor
的值。我尝试使用if和else都没有成功

 <xsl:choose>
 <xsl:when test="reportAuthor1=''">
 <xsl:value-of select="reportAuthor"/>
 </xsl:when>
 <xsl:otherwise>
 <xsl:value-of select="reportAuthor1"/>
 </xsl:otherwise>
 </xsl:choose>

使用
not()
检查节点是否根本不存在:

<xsl:choose>  
  <xsl:when test="not(reportAuthor1)">  
    <xsl:value-of select="reportAuthor"/>
  </xsl:when>  
  <xsl:otherwise>  
    <xsl:value-of select="reportAuthor1"/>  
  </xsl:otherwise>  
</xsl:choose> 

使用
not()
检查节点是否根本不存在:

<xsl:choose>  
  <xsl:when test="not(reportAuthor1)">  
    <xsl:value-of select="reportAuthor"/>
  </xsl:when>  
  <xsl:otherwise>  
    <xsl:value-of select="reportAuthor1"/>  
  </xsl:otherwise>  
</xsl:choose> 

在这种特定情况下,您也可以编写

<xsl:value-of select="reportAuthor1 | reportAuthor"/>


|
返回XPath查询从该运算符的左侧和右侧返回的集合的并集,
将向输出发送somequery返回的集合中第一个节点的值。因此,如果同时设置了
reportAuthor1
reportAuthor
,它将输出
reportAuthor1
的值;如果仅设置了
reportAuthor
,它将输出
reportOutput

的值。在这种特定情况下,您还可以编写

<xsl:value-of select="reportAuthor1 | reportAuthor"/>


|
返回XPath查询从该运算符的左侧和右侧返回的集合的并集,
将向输出发送somequery返回的集合中第一个节点的值。因此,如果同时设置了
reportAuthor1
reportAuthor
,它将输出
reportAuthor1
的值;如果只设置了
reportAuthor
,它将输出
reportOutput

的值,这可能是最简单、最短的一行XPath表达式。使用

<xsl:value-of select="reportAuthor1 | reportAuthor[not(reportAuthor1)]"/>

这可能是最简单、最短的一行XPath表达式。使用

<xsl:value-of select="reportAuthor1 | reportAuthor[not(reportAuthor1)]"/>


谢谢你的快速回复。我已经尝试过以前的解决方案。我也尝试过。AAAH,我的评论原来被解析器搞砸了,一个人不能忘记空白行!我的代码示例与Dimitre Novatchev answer类似,尽管速度较慢(
reportAuthor
,即使有
reportAuthor1
),也会计算出来)。另一方面,XSLT处理器应该足够聪明,不会在这种情况下计算
reportAuthor
。使用union运算符而不是
的一个问题是,它会选择XML中最先出现的一个,因此如果
reportAuthor
元素位于
reportAuthor1
之前,它会使用它,即使select子句中的第一个是
reportAuthor1
。您可以通过
reportAuthor1 | reportAuthor[not(../reportAuthor1)]
来缓解这一问题,它只会选择一个
reportAuthor
元素,如果它没有
reportAuthor1
同级(xpath指定它的父元素没有
reportAuthor1
子元素)。@Flynn1179您完全正确。我不知道工会的这个不太明显的特点。这使得我的答案不正确,而Dimitre Novatchev的答案是唯一使用union运算符的正确答案。实际上,Dimitre的答案是错误的,它将选择没有子元素的
reportAuthor
元素。我建议的解决方案中的<代码>…/<代码>很重要。谢谢快速回复。我已经尝试过以前的解决方案。IL也尝试这个。AAAH,我的评论最初被解析器搞砸了,不能忘记空白行!我的代码示例与Dimitre Novatchev answer类似,尽管速度较慢(
reportAuthor
,即使有
reportAuthor1
),也会计算出来)。另一方面,XSLT处理器应该足够聪明,不会在这种情况下计算
reportAuthor
。使用union运算符而不是
的一个问题是,它会选择XML中最先出现的一个,因此如果
reportAuthor
元素位于
reportAuthor1
之前,它会使用它,即使select子句中的第一个是
reportAuthor1
。您可以通过
reportAuthor1 | reportAuthor[not(../reportAuthor1)]
来缓解这一问题,它只会选择一个
reportAuthor
元素,如果它没有
reportAuthor1
同级(xpath指定它的父元素没有
reportAuthor1
子元素)。@Flynn1179您完全正确。我不知道工会的这个不太明显的特点。这使得我的答案不正确,而Dimitre Novatchev的答案是唯一使用union运算符的正确答案。实际上,Dimitre的答案是错误的,它将选择没有子元素的
reportAuthor
元素。我建议的修复中的
。/
非常重要。