问题
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() < 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:
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>
来源:https://stackoverflow.com/questions/25283820/split-table-in-xslt