XSLT中的拆分表

时间:2014-08-13 10:28:31

标签: xml xslt xslt-2.0

我有一个XML,我需要将它分成50或100行,XML就像这样。

<?xml version = "1.0" encoding = "utf-8" standalone = "yes"?>
<DOCUMENT xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
    <A>
        <B>
            <C>
                <D>
                    <ROW>
                        <SNo>1</SNo>
                        <Date>06/JUN/2014</Date>
                        <Time>12:31:49</Time>
                    </ROW>
                    <ROW>
                        <SNo>2</SNo>
                        <Date>07/JUN/2014</Date>
                        <Time>11:50:42</Time>
                    </ROW>
                </D>
                <D>
                    <ROW>
                        <SNo>3</SNo>
                        <Date>08/JUN/2014</Date>
                        <Time>19:43:09</Time>
                    </ROW>
                    <ROW>
                        <SNo>4</SNo>
                        <Date>10/JUN/2014</Date>
                        <Time>08:26:07</Time>
                    </ROW>
                </D>
            </C>
        </B>
    </A>
</DOCUMENT>

我尝试过使用下面的位置()mod

ROW[ position() mod 50 =1 ]
following-sibling::ROW[ position() &lt; 50 ]

我需要一个如下所示的输出

<?xml version = "1.0" encoding = "utf-8" standalone = "yes"?>
<DOCUMENT xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
    <A>
        <B>
            <C>
                <D>
                    <ROW>
                        <SNo>1</SNo>
                        <Date>06/JUN/2014</Date>
                        <Time>12:31:49</Time>
                    </ROW>
                    <ROW>
                        <SNo>2</SNo>
                        <Date>07/JUN/2014</Date>
                        <Time>11:50:42</Time>
                    </ROW>
                    <ROW>
                        <SNo>3</SNo>
                        <Date>08/JUN/2014</Date>
                        <Time>19:43:09</Time>
                    </ROW>
                    <ROW>
                        <SNo>4</SNo>
                        <Date>10/JUN/2014</Date>
                        <Time>08:26:07</Time>
                    </ROW>
                    .
                    .
                    .
                    .
                    .
                    <ROW>
                        <SNo>50</SNo>
                        <Date>08/JUN/2014</Date>
                        <Time>19:43:09</Time>
                    </ROW>

                </D>
                <D>
                    <ROW>
                        <SNo>1</SNo>
                        <Date>08/JUN/2014</Date>
                        <Time>19:43:09</Time>
                    </ROW>
                    <ROW>
                        <SNo>2</SNo>
                        <Date>10/JUN/2014</Date>
                        <Time>08:26:07</Time>
                    </ROW>
                    .
                    .
                    .
                    .
                    .
                    <ROW>
                        <SNo>50</SNo>
                        <Date>10/JUN/2014</Date>
                        <Time>08:26:07</Time>
                    </ROW>
                </D>
            </C>
        </B>
    </A>
</DOCUMENT>

但它似乎不起作用。 任何人都可以对此进行检查并帮助我......

1 个答案:

答案 0 :(得分:1)

当您使用XSLT 2.0时,您可以简单地使用for-each-groups例如

<xsl:param name="size" as="xs:integer" select="50"/>

<xsl:template match="@* | node()">
  <xsl:copy>
    <xsl:apply-templates select="@* , node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="C">
  <xsl:copy>
    <xsl:for-each-group select="D/ROW" group-adjacent="(position() - 1) idiv $size">
      <D>
        <xsl:apply-templates select="current-group()"/>
      </D>
    </xsl:for-each-group>
  </xsl:copy>
</xsl:template>

<xsl:template match="ROW">
  <xsl:copy>
    <SNo><xsl:value-of select="position()"/></SNo>
    <xsl:apply-templates select="node() except SNo"/>
  </xsl:copy>
</xsl:template>