I have a xml like this,
<doc>
<para>texttext<page>1</page>texttext<page>1</page>texttext</para>
<para>texttext<page>1</page><page>2</page>texttext</para>
<para>texttext<page>1</page><page>2</page><page>3</page>texttext<page>4</page><page>5</page><page>6</page>texttext</para>
<para>texttext<page>1</page><page>2</page><page>3</page><page>4</page>texttext</para>
</doc>
I need to transform <page>
nodes to <link>
using xsl transform and following rules need to be considered,
- if only one
<page>
node appear (not followed any page node) it just transform to<link>
- if two
<page>
node placed successively (scenario 2 from above example) ',' has to added between output<link>
nodes - if 3 or more
<page>
nodes placed successively (scenario 3 and 4 from above example), just adds first and last content of page node separated by '-'
So, output should be like this,
<doc>
<para>texttext<link>1</link>texttext<link>1</link>texttext</para>
<para>texttext<link>1</link>,<link>2</link>texttext</para>
<para>texttext<link>1</link>-<link>3</link>texttext<link>4</link>-<link>6</link>texttext</para>
<para>texttext<link>1</link>-<link>4</link>texttext</para>
</doc>
I wrote following xsl for do this task,
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="page">
<link>
<xsl:apply-templates/>
</link>
</xsl:template>
<xsl:template match="page[following-sibling::node()[1][self::page]]">
<link>
<xsl:apply-templates/>
</link>
<xsl:text>,</xsl:text>
<link>
<xsl:apply-templates select="following-sibling::*[1]"/>
</link>
</xsl:template>
<xsl:template match="page[following-sibling::node()[1][self::page]][following-sibling::node()[2][self::page]]">
<link>
<xsl:apply-templates/>
</link>
<xsl:text>-</xsl:text>
<link>
<xsl:apply-templates select="following-sibling::*[2]"/>
</link>
</xsl:template>
but this method is not woking as, it adds ',' when there are 3 successive <page>
nodes appear and if there are more <page>
nodes appear successively this method is not efficient.
Can anyone suggest a good method in xslt to analyze following siblings form xslt and do this task..