xslt 合并 2 个父母的孩子并存储在一个变量中 [英] xslt Merge children of 2 parents and Store in a variable
问题描述
我收到这样的 xml 输入:
I receive an xml input like this:
<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 合并,依此类推.应该存储在变量中的合并输出在内存中看起来像这样:
How can I merge the children of each Tuple1 with children of Tuple2 and store them in a variable that will be used in the rest of xslt document? First tuple1 will be merged with first Tuple2 and second Tuple1 will be merged with 2nd Tuple2 and so on. The merged output that should be stored in variable would look like this in memory:
<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,因此任何版本的解决方案都可以提供帮助.谢谢,我很感激你的帮助)
Is variable the best option? If we use variable, is it created once or it is created every time called? I use xslt 3.0 so solution for any version can help. Thanks and I appreciate your help)
推荐答案
这是一个最小的 XSLT 3 方法:
Here is a minimal XSLT 3 approach:
<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>
在线于 https://xsltfiddle.liberty-development.net/bdxtqg,我使用 XPath 的 let
而不是 XSLT 的 xsl:variable
来存储位置以访问特定的 Tuple2
.
Online at https://xsltfiddle.liberty-development.net/bdxtqg, I have used XPath's let
instead of XSLT's xsl:variable
to store the position to access the specific Tuple2
.
这篇关于xslt 合并 2 个父母的孩子并存储在一个变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!