xslt Merge children of 2 parents 并存储在变量中
xslt Merge children of 2 parents and Store in a variable
我收到这样的 xml 输入:
<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 的 children 与 Tuple2 的 children 合并并将它们存储在变量中 将在 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>
在线https://xsltfiddle.liberty-development.net/bdxtqg,我已经使用XPath的let
代替XSLT的xsl:variable
来存储访问特定Tuple2
的位置。
我收到这样的 xml 输入:
<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 的 children 与 Tuple2 的 children 合并并将它们存储在变量中 将在 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>
在线https://xsltfiddle.liberty-development.net/bdxtqg,我已经使用XPath的let
代替XSLT的xsl:variable
来存储访问特定Tuple2
的位置。