Xml 转换Xsl时如何确定节点是否存在?
如何确定输入xml中是否存在给定节点 如果节点存在,我希望使用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-
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
元素。我建议的修复中的。/
非常重要。