Split Table in XSLT

2019-08-20 04:43发布

I Have a XML that i need to split it by 50 or 100 rows the XML looks like this.

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

I Have tried using the position() mod like below

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

I need a output like below

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

But it does not seem to work. Can anyone have a check on this and help me...

1条回答
时光不老,我们不散
2楼-- · 2019-08-20 05:20

As you use XSLT 2.0, you can simply use for-each-group e.g.

<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>
查看更多
登录 后发表回答