Hi Karl,
I approached the problem as follows -
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:variable name="start1" select="1" />
<xsl:variable name="end1" select="12" />
<xsl:variable name="start2" select="14" />
<xsl:variable name="end2" select="30" />
<xsl:template match="/A">
<result>
<period begins="{$start1}" ends="{$end1}">
<xsl:for-each select="B[(@period_begin >=
$start1) and (@period_end <= $end1)]">
<xsl:sort select="@period_begin"
data-type="number" />
<xsl:copy-of select="." />
</xsl:for-each>
</period>
<period begins="{$start2}" ends="{$end2}">
<xsl:for-each select="B[(@period_begin >=
$start2) and (@period_end <= $end2)]">
<xsl:sort select="@period_begin"
data-type="number" />
<xsl:copy-of select="." />
</xsl:for-each>
</period>
</result>
</xsl:template>
</xsl:stylesheet>
When the above XSLT stylesheet is given this XML as
input -
<?xml version="1.0" encoding="UTF-8"?>
<A>
<B period_begin="14" period_end="16"/>
<B period_begin="2" period_end="7"/>
<B period_begin="16" period_end="20"/>
<B period_begin="1" period_end="5"/>
<B period_begin="4" period_end="12"/>
<B period_begin="16" period_end="30"/>
<B period_begin="3" period_end="10"/>
</A>
The output recieved is -
<?xml version="1.0" encoding="utf-8"?>
<result>
<period begins="1" ends="12">
<B period_begin="1" period_end="5"/>
<B period_begin="2" period_end="7"/>
<B period_begin="3" period_end="10"/>
<B period_begin="4" period_end="12"/>
</period>
<period begins="14" ends="30">
<B period_begin="14" period_end="16"/>
<B period_begin="16" period_end="20"/>
<B period_begin="16" period_end="30"/>
</period>
</result>
The other answers are brilliant.. But I thought of
giving it a try myself, and seeing how my approach
will differ with other people's answers!
(I don't know if I should have answered at this stage,
as your question is already answered :) But just
thought of..)
Regards,
Mukul
--- Karl Stubsjoen <kstubs(_at_)gmail(_dot_)com> wrote:
A challenge, group the following XML into 2 periods.
The periods are
arbitrary, but for this example they happen to be:
Period 1: 1 - 12
Period 2: 14 - 30
Expected Result:
<result>
<period begins="1" ends="12">
<B period_begin="1" period_end="5"/>
<B period_begin="2" period_end="7"/>
<B period_begin="3" period_end="10"/>
<B period_begin="4" period_end="12"/>
</period>
<period begins="14" ends="30">
<B period_begin="14" period_end="16"/>
<B period_begin="16" period_end="20"/>
<B period_begin="16" period_end="30"/>
</period>
</result>
Source XML / Result (sorted)
<A>
<B period_begin="1" period_end="5"/>
<B period_begin="2" period_end="7"/>
<B period_begin="3" period_end="10"/>
<B period_begin="4" period_end="12"/>
<B period_begin="14" period_end="16"/>
<B period_begin="16" period_end="20"/>
<B period_begin="16" period_end="30"/>
</A>
Source XML / Result (un-sorted)
<A>
<B period_begin="14" period_end="16"/>
<B period_begin="2" period_end="7"/>
<B period_begin="16" period_end="20"/>
<B period_begin="1" period_end="5"/>
<B period_begin="4" period_end="12"/>
<B period_begin="16" period_end="30"/>
<B period_begin="3" period_end="10"/>
</A>
--~------------------------------------------------------------------
XSL-List info and archive:
http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to:
http://lists.mulberrytech.com/xsl-list/
or e-mail:
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--
Discover Yahoo!
Stay in touch with email, IM, photo sharing and more. Check it out!
http://discover.yahoo.com/stayintouch.html
--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--