Need to tag certain paragraph in one tag and remaining in another tag using XSLT


I want to take the first set of paragraph in one element and second set of paragraph in another element

My Input XML file is:

<topic class="- topic/topic " outputclass="TOPIC-MLU-Body">
<title outputclass="MLU-Body">Body</title>
<body class="- topic/body ">
<p class="- topic/p ">Insulin is a medicine</p>
<fig class="- topic/fig ">
<image class="- topic/image "
          outputclass="Fig-Image_Ref" placement="break"/>
<p class="- topic/p " outputclass="Fig-Text">Caption</p>
<p class="- topic/p ">So, to try and lower your blood glucose levels</p>

XSL Which i used as:

<xsl:stylesheet version="2.0" 

<xsl:template match="topic[@outputclass='TOPIC-MLU-Body']">
 <text_top><xsl:value-of select="title|body/p"/></text_top>
 <text_bottom><xsl:value-of select="body/fig|p"/></text_bottom>


I need the paragraphs coming before the figure element as "text-top" and after figure elements as "text_bottom"

I'm getting output as:

    <text_top>Insulin is a medicine So, to try and lower your blood glucose levels</text_top>

But my expected Output is:

<text_top>Insulin is a medicine</text_top>
<text_bottom>So, to try and lower your blood glucose levels</text_bottom>

Im using Saxon PE and version=2.0 stylesheet. Please give me the suggestions for this. Thanks in advance.

Show source
| xml   | xslt   | dita   2016-12-29 12:12 1 Answers

Answers ( 1 )

  1. 2016-12-29 13:12

    The output you say you are currently getting does not actually correspond to the XSLT you have provided. For example, your XSLT outputs a <body> tag, but your output shows a <mlu9_body> tag.

    Additionally, the | operator in XSLT is the union operator, and so will return a union of the two node sets. For example, you are doing this

    <xsl:value-of select="body/fig|p"/

    In XSLT 2.0, this will return the concatenation of the string value of body/fig and the string value of both p elements.

    Anyway, try this XSLT which will give your expected output:

    <xsl:stylesheet version="2.0" xmlns:xsl="">
    <xsl:output method="xml" indent="yes" />
    <xsl:template match="topic[@outputclass='TOPIC-MLU-Body']">
     <text_top><xsl:value-of select="body/p[1]"/></text_top>
     <text_bottom><xsl:value-of select="body/p[2]"/></text_bottom>

    Note, if you really wanted to target the p element that occurred after the fig element, you could do this...

    <text_bottom><xsl:value-of select="body/fig/following-sibling::p"/></text_bottom>
◀ Go back