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

Question

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 "
          href="SBX0139003.jpg"
          outputclass="Fig-Image_Ref" placement="break"/>
<p class="- topic/p " outputclass="Fig-Text">Caption</p>
</fig>
<p class="- topic/p ">So, to try and lower your blood glucose levels</p>
</body>
</topic>

XSL Which i used as:

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

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

</xsl:stylesheet>

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

I'm getting output as:

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

But my expected Output is:

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

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 to Need to tag certain paragraph in one tag and remaining in another tag using XSLT ( 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="http://www.w3.org/1999/XSL/Transform">
    
    <xsl:output method="xml" indent="yes" />
    
    <xsl:template match="topic[@outputclass='TOPIC-MLU-Body']">
    <mlu9_body>
    <mlu9_text>
     <text_top><xsl:value-of select="body/p[1]"/></text_top>
     <text_bottom><xsl:value-of select="body/p[2]"/></text_bottom>
    </mlu9_text> 
    </mlu9_body>
    </xsl:template>
    
    </xsl:stylesheet>
    

    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>
    

Leave a reply to - Need to tag certain paragraph in one tag and remaining in another tag using XSLT

◀ Go back