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

时间:2022-06-21 07:20:12

How can I determine whether a given node is present in my input xml?

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

If the node is present I would like to use the value of reportAuthor1, but otherwise I would use the value of reportAuthor. I have tried unsuccessfully to use if and else.

如果节点存在,我希望使用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>

3 个解决方案

#1


5  

Use not() to check whether a node doesn't exist at all:

使用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> 

#2


3  

This is probably the simplest and shortest, one-line XPath expression. Use:

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

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

#3


1  

In this specific case, you may also write

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

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

| returns the union of sets returned by the XPath queries from the left and from the right side of this operator, and <xsl:value-of select="somequery"/> will send to the output the value of the first node in the set returned by somequery. Thus, if both reportAuthor1 and reportAuthor are set, it'll output the value of reportAuthor1; if only reportAuthor is set, it'll output the value of reportOutput.

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

#1


5  

Use not() to check whether a node doesn't exist at all:

使用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> 

#2


3  

This is probably the simplest and shortest, one-line XPath expression. Use:

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

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

#3


1  

In this specific case, you may also write

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

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

| returns the union of sets returned by the XPath queries from the left and from the right side of this operator, and <xsl:value-of select="somequery"/> will send to the output the value of the first node in the set returned by somequery. Thus, if both reportAuthor1 and reportAuthor are set, it'll output the value of reportAuthor1; if only reportAuthor is set, it'll output the value of reportOutput.

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