1

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

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

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

4

1 回答 1

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>
于 2013-05-07T14:45:21.517 回答