Xpath xslt合并两个父项的子项并存储在一个变量中
我收到如下xml输入:Xpath xslt合并两个父项的子项并存储在一个变量中,xpath,xslt-1.0,xslt-2.0,xslt-3.0,Xpath,Xslt 1.0,Xslt 2.0,Xslt 3.0,我收到如下xml输入: <root> <Tuple1> <child11></child11> <child12></child12> <child13></child13> </Tuple1> <Tuple1> <child11></child11> <chi
<root>
<Tuple1>
<child11></child11>
<child12></child12>
<child13></child13>
</Tuple1>
<Tuple1>
<child11></child11>
<child12></child12>
</Tuple1>
<Tuple2>
<child21></child21>
<child22></child22>
</Tuple2>
<Tuple2>
<child21></child21>
<child22></child22>
<child23></child23>
</Tuple2>
</root>
如何将每个Tuple1的子项与Tuple2的子项合并,并将它们存储在变量中,该变量将在xslt文档的其余部分中使用?
第一个tuple1将与第一个Tuple2合并,第二个tuple1将与第二个Tuple2合并,依此类推。应存储在变量中的合并输出在内存中的外观如下:
<root>
<Tuple1>
<child11></child11>
<child12></child12>
<child13></child13>
<child21></child21>
<child22></child22>
</Tuple1>
<Tuple1>
<child11></child11>
<child12></child12>
<child21></child21>
<child22></child22>
<child23></child23>
</Tuple1>
</root>
变量是最好的选择吗?若我们使用变量,它是创建一次还是每次调用时都创建?
我使用xslt 3.0,所以任何版本的解决方案都会有所帮助。
感谢并感谢您的帮助)这里有一个简单的XSLT 3方法:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="3.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="root">
<xsl:variable name="temp1">
<xsl:copy>
<xsl:apply-templates select="Tuple1"/>
</xsl:copy>
</xsl:variable>
<xsl:copy-of select="$temp1"/>
</xsl:template>
<xsl:template match="Tuple1">
<xsl:copy>
<xsl:copy-of select="*, let $pos := position() return ../Tuple2[$pos]/*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
在网上,我使用XPath的
let
而不是XSLT的xsl:variable
来存储访问特定Tuple2
的位置。好吧,对每个使用处理所有Tuple1
元素,或者例如应用模板,然后使用position()
(
)复制相关元组2
(
)的子级。无论是对变量还是直接对输出树这样做,在XSLT 2或XSLT 3中都没有任何区别。变量的使用和实现策略取决于XSLT处理器,但我非常确定,大多数人在第一次使用变量时只会对变量求值一次。或者,当您使用XSLT3时,Saxon是一种参考实现,我认为它就是这样做的。(谢谢)