XSLT 1.0 - 需要帮助

时间:2015-07-17 05:58:08

标签: xml xslt

我有以下输入

<?xml version="1.0"?>
<file>
    <Header>
        <record>1</record>
        <date>20101111</date>
        <time>0200</time>
    </Header>
    <subHead>
        <record>2</record>
        <unique>123</unique>
        <time>0200</time>
    </subHead>
    <content>
        <record>4</record>
        <field1>20101111</field1>
        <field2>0200</field2>
    </content>
    <content>
        <record>4</record>
        <field1>20101111</field1>
        <field2>0200</field2>
    </content>
    <subTail>
        <record>6</record>
        <count>2</count>
        <uniquenum>123</uniquenum>
    </subTail>
    <subHead>
        <record>2</record>
        <unique>456</unique>
        <time>0300</time>
    </subHead>
    <content>
        <record>4</record>
        <field1>2010</field1>
        <field2>0300</field2>
    </content>
    <subTail>
        <record>6</record>
        <count>1</count>
        <uniquenum>456</uniquenum>
    </subTail>
    <footer>
        <record>8</record>
        <countSubhead>2</countSubhead>
    </footer>
</file>

所需的输出是

<?xml version="1.0"?>
<file>
    <Header>
        <record>1</record>
        <date>20101111</date>
        <time>0200</time>
    </Header>
    <Group>
        <subHead>
            <record>2</record>
            <unique>123</unique>
            <time>0200</time>
        </subHead>
        <content>
            <record>4</record>
            <field1>20101111</field1>
            <field2>0200</field2>
        </content>
        <content>
            <record>4</record>
            <field1>20101111</field1>
            <field2>0200</field2>
        </content>
        <subTail>
            <record>6</record>
            <count>2</count>
            <uniquenum>123</uniquenum>
        </subTail>
    </Group>
    <Group>
        <subHead>
            <record>2</record>
            <unique>456</unique>
            <time>0300</time>
        </subHead>
        <content>
            <record>4</record>
            <field1>2010</field1>
            <field2>0300</field2>
        </content>
        <subTail>
            <record>6</record>
            <count>1</count>
            <uniquenum>456</uniquenum>
        </subTail>
    </Group>
    <footer>
        <record>8</record>
        <countSubhead>2</countSubhead>
    </footer>
</file>

这里我的xslt转换逻辑在发生新的subHead时分组,然后它必须被分组为单个组

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="/*">

        <file>
            <xsl:copy-of select="Header"/>
            <xsl:for-each select="subHead">
                <group>

                    <xsl:copy-of select="self::subHead"/>
                    <xsl:copy-of select="following-sibling::content"/>
                    <xsl:copy-of select="following-sibling::subTail"/>
                </group>
            </xsl:for-each>
            <xsl:copy-of select="footer"/>
        </file>
    </xsl:template>
</xsl:stylesheet>

我希望错误出现在以下兄弟代码中,该代码将所有后续节点组合到第一个组中。任何人都可以帮我纠正我正在做的错误。

1 个答案:

答案 0 :(得分:0)

问题在于你怀疑的following-sibling陈述中。尝试为每个subHead指定一下,我们只对以下兄弟元素感兴趣,这些元素的最近前兄弟subHead等于我们正在处理的当前subHead,如下所示:

<group>
  <xsl:copy-of select="self::subHead"/>
  <xsl:copy-of select="following-sibling::content[preceding-sibling::subHead[1] = current()]"/>
  <xsl:copy-of select="following-sibling::subTail[preceding-sibling::subHead[1] = current()]"/>
</group>