xml - How to merge elements of same name found consecutively -


please suggest how merge elements same name [element mi only] appearing consecutively. element mi [without having attributes] required merge. element mi may have parent.

xml:

<article>     <math>         <mtext>one</mtext>         <mi>i</mi>         <mi>n</mi>         <mi>s</mi>         <mi>t</mi>         <mtext>the</mtext>         <mi>s</mi>         <mi>m</mi>         <mrow>             <mi>a</mi>             <mi>l</mi>             <mi>l</mi>         </mrow>         <mi>y</mi>         <mn>8</mn>         <mi>z</mi>     </math> </article> 

xslt:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform">      <xsl:template match="@*|node()">         <xsl:copy><xsl:apply-templates select="@* | node()"/></xsl:copy>     </xsl:template>      <xsl:template match="mi">         <xsl:choose>             <xsl:when test="not(preceding-sibling::*[name()='mi'][1]) , not(following-sibling::*[name()='mi'][1])">                 <xsl:copy><xsl:apply-templates select="@* | node()"/></xsl:copy>             </xsl:when>              <xsl:when test="following-sibling::*[1][name()='mi']">                 <xsl:variable name="varcnt">                     <xsl:value-of select="count(following-sibling::*)"/>                 </xsl:variable>                 <xsl:copy>                 <xsl:for-each select="1 number($varcnt)">                     <xsl:if test="name()='mi'">                     <xsl:apply-templates/>                     </xsl:if>                 </xsl:for-each>                 </xsl:copy>             </xsl:when>             <xsl:otherwise><xsl:copy><xsl:apply-templates select="@* | node()"/></xsl:copy></xsl:otherwise>         </xsl:choose>     </xsl:template> </xsl:stylesheet> 

required output:

<article>     <math>         <mtext>one</mtext>         <mi variant="italic">inst</mi>         <mtext>the</mtext>         <mi variant="italic">sm</mi>         <mrow>             <mi variant="italic">all</mi>         </mrow>         <mi>y</mi>         <mn>8</mn>         <mi>z</mi>     </math> </article> 

use for-each-group group-adjacent="boolean(self::mi)", in

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform">      <xsl:output indent="yes"/>      <xsl:template match="@*|node()">         <xsl:copy><xsl:apply-templates select="@* | node()"/></xsl:copy>     </xsl:template>      <xsl:template match="*[mi]">       <xsl:copy>         <xsl:for-each-group select="*" group-adjacent="boolean(self::mi)">           <xsl:choose>             <xsl:when test="current-grouping-key() , count(current-group()) gt 1">               <mi variant="italic"><xsl:apply-templates select="current-group()/node()"/></mi>             </xsl:when>             <xsl:otherwise>               <xsl:apply-templates select="current-group()"/>             </xsl:otherwise>           </xsl:choose>         </xsl:for-each-group>       </xsl:copy>     </xsl:template>  </xsl:stylesheet> 

Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -