xslt与Saxon合并功能

Ste*_*man 6 xslt

有没有人知道在XSLT中执行合并的内置函数,还是我需要编写自己的函数?

我有一些像这样的xml:

<root>
 <Element1>
   <Territory>Worldwide</Territory>
   <Name>WorldwideName</Name>
   <Age>78</Age>
 </Element1>
 <Element1>
   <Territory>GB</Territory>
   <Name>GBName</Name>
 </Element1>
</root>
Run Code Online (Sandbox Code Playgroud)

第二个元素1(GB Territory)是完全可选的,可能会也可能不会发生,但是当它确实发生时,它优先于WorldWide Territory.

所以我追求的是类似下面的合并:

<xsl:variable name="Worldwide" select="root/Element1[./TerritoryCode ='Worldwide']"/>
<xsl:variable name="GB" select="root/Element1[./TerritoryCode ='GB']"/>

<xsl:variable name="Name" select="ext:coalesce($GB/Name, $Worldwide/Name)"/>
Run Code Online (Sandbox Code Playgroud)

id是上例中的变量Name,它将包含GBName.

我知道我可以使用xsl:choose,但我有一些地方有4个地方可以看,而且xsl:选择只是变得凌乱和复杂,所以希望找到一个内置函数,但到目前为止没有运气.

谢谢.

Mad*_*sen 6

在XSLT 2.0中,您可以从变量中创建一系列项目,然后使用谓词过滤器选择第一个项目:

<xsl:variable name="Name" select="($GB/Name, $Worldwide/Name)[1]"/>
Run Code Online (Sandbox Code Playgroud)

谓词过滤器将选择序列中的第一个非空项.

例如,这仍然会产生"GBName":

<xsl:variable name="emptyVar" select="foo"/>
<xsl:variable name="Worldwide" select="root/Element1[Territory ='Worldwide']"/>
<xsl:variable name="GB" select="root/Element1[Territory ='GB']"/>

<xsl:variable name="Name" select="($emptyVar, $GB/Name, $Worldwide/Name)[1]"/>
Run Code Online (Sandbox Code Playgroud)