XSLT以2为一组显示节点列表

时间:2013-05-07 14:06:24

标签: xslt

所以说我有一个看起来像这样的节点列表:

<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>
...

在每个节点下,<list>个项目的数量并不总是相同。 我希望使用xslt 1.0以2为一组输出,显示每个项目组合。所以像这样:

<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>
...

任何帮助都会很棒,谢谢!

1 个答案:

答案 0 :(得分:2)

尝试这样的事情:

<?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>