XSLT For Breaking Elements Into Sets?
Is XSLT a good solution for breaking an XML document into sets by element name? For example, if my document is:
<mydocument>
<items>
<item>one</item>
<item>two</item>
<item>three</item>
<item>four</item>
</items>
</mydocument>
I want to split this into sets of 3 or less like:
<mydocument>
<items page="1">
<item>one</item>
<item>two</item>
<item>three</item>
</items>
<items page="2">
<item>four</item>
</items>
</mydocument>
Can someone provide me with a XSLT approach, or even bett开发者_如何学Cer, an example.
This transformation:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:variable name="vSetSize" select="3"/>
<xsl:template match="/*/*">
<mydocument>
<xsl:for-each select=
"item[position() mod $vSetSize = 1]">
<items page="{position()}">
<xsl:copy-of select=
". | following-sibling::*
[not(position() >= $vSetSize)]"/>
</items>
</xsl:for-each>
</mydocument>
</xsl:template>
</xsl:stylesheet>
when applied on the provided XML document, produces the wanted result:
<mydocument>
<items page="1">
<item>one</item>
<item>two</item>
<item>three</item>
</items>
<items page="2">
<item>four</item>
</items>
</mydocument>
精彩评论