尝试这样的事情:
<?xml version="1.0"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="node">
<xsl:for-each select="list">
<xsl:apply-templates select="following-sibling::list" >
<xsl:with-param name ="l1" select="." />
<xsl:with-param name ="node" select=".." />
</xsl:apply-templates>
</xsl:for-each>
</xsl:template>
<xsl:template match="list">
<xsl:param name ="l1" />
<xsl:param name ="node" />
<node>
<xsl:apply-templates select="$node/@*" />
<xsl:copy-of select="$l1"/>
<xsl:copy-of select="."/>
</node>
</xsl:template>
</xsl:stylesheet>
输入:
<?xml version="1.0" encoding="UTF-8"?>
<xml>
<node value="red">
<list num="1">ABC</list>
<list num="2">DEF</list>
<list num="3">GHI</list>
</node>
<node value="blue">
<list num="4">JKL</list>
<list num="5">MNO</list>
</node>
</xml>
输出:
<?xml version="1.0"?>
<xml>
<node value="red">
<list num="1">ABC</list>
<list num="2">DEF</list>
</node>
<node value="red">
<list num="1">ABC</list>
<list num="3">GHI</list>
</node>
<node value="red">
<list num="2">DEF</list>
<list num="3">GHI</list>
</node>
<node value="blue">
<list num="4">JKL</list>
<list num="5">MNO</list>
</node>
</xml>