[xsl] Expanding Ranges

Subject: [xsl] Expanding Ranges
From: Ganesh Babu N <nbabuganesh@xxxxxxxxx>
Date: Thu, 19 Mar 2009 18:01:22 +0530
Dear All,

My input is like below: the <cross-ref> tags are not continues it will
come in between the content.

<links>
<p>some text  <cross-ref><cr refid="MEPRfig2"> 2</cr><cr
refid="MEPRfig4">4</cr></cross-ref> some text
<cross-ref><cr refid="MEPRfig1">Figures 1</cr> and <cr
refid="MEPRfig3">3</cr></cross-ref> some text</p>

<p><cross-ref><cr refid="MEPRtbl1">1</cr>-<cr
refid="MEPRtbl4">4</cr></cross-ref> some text <cross-ref><cr
refid="MEPRtbl1">Tables 1</cr> and <cr
refid="MEPRtbl2">2</cr></cross-ref> some text <cross-ref><cr
refid="MEPRtbl1">Table1</cr></cross-ref> some text </p>

<p>some text <cross-ref><cr refid="MEPRtbl1">1</cr>-<cr
refid="MEPRtbl3">3</cr> and  <cr refid="MEPRtbl4">4</cr></cross-ref>
some text</p>
</links>

The required output will be like below:

<links>
<p>some text  <cross-refs refid="fig2 fig3 fig4"> 24</cross-refs> some text
<cross-refs refid="fig1 fig3">Figures 1 and 3</cross-refs> some text</p>

<p><cross-refs refid="tbl1 tbl2 tbl3 tbl4">1-4</cross-refs> some text
<cross-refs refid="tbl1 tbl2">Tables 1 and 2</cross-refs> some text
<cross-ref refid="tbl1">Table1</cross-ref> some text </p>

<p>some text <cross-refs refid="tbl1 tbl2 tbl3 tbl4">1-3 and
4</cross-refs> some text</p>
</links>

Here is the XSL which i have tried. I do not know how to expand the
ranges. My style sheet is combining available attributes to single
attribute. Please help me in getting ranges expanded.

<xsl:template match="cross-ref">
<xsl:choose>
	<xsl:when test="count(cr) = 1">
		<cross-ref refid="{substring-after(cr/@refid,'MEPR')}">
			<xsl:apply-templates/>
		</cross-ref>
	</xsl:when>
	<xsl:otherwise>
		<cross-refs>
			<xsl:attribute name="refid">
				<xsl:for-each select="cr">
					<xsl:value-of select="substring-after(@refid,'MEPR')"/>
					<xsl:if test="position() != last()">
						<xsl:text> </xsl:text>
					</xsl:if>
				</xsl:for-each>
			</xsl:attribute>
			<xsl:apply-templates/>
		</cross-refs>
	</xsl:otherwise>
</xsl:choose>
</xsl:template>

	<xsl:template match="cr">
		<xsl:apply-templates/>
	</xsl:template>

Regards,
Ganesh

Current Thread