XML : Insert 'Space' element between subscripts text and closing bracket

Please suggest to insert space between subscript(msub) text and closing bracket ')', where 'p' is the first child or first child's descendant text. (xsl:template match will have the text like braces ')'.)

There some criteria to insert the spaces between text 'p' and close bracket ')'. (In below text, assume p2) as <sub><mi>p</mi><mn>2</mn></sub>).

Template match must from 'mo' where it contains closing bracket ')'. See comments (ignore those) to explain the required result. Please suggest.

  • p2) - to p2<space/>) - space required between 'p2' and ')', as 'p' is first child text of 'msub' or subscripts text.
  • p2+a) - to p2+a<space/>) - space required between 'p2+a' and ')', space required, as 'p' is first child text of 'msub' or subscripts text, and remaining text will come in subscript's second text. From bracket first preceded text belongs to SUBSCRIPT.

XML:

  <article>    <math id="m1">      <mo>(</mo>      <msub>          <mi>p</mi>          <mn>2</mn>      </msub>      <mo>)</mo>  </math>    <math id="m2">      <mo>(</mo>      <msub>          <mrow><mi>r</mi></mrow>          <mrow><mi>p</mi></mrow>      </msub>      <mn>8</mn>      <mo>)</mo>  </math>    <math id="m3">      <mo>(</mo>      <msub>          <mrow><mi>p</mi></mrow>          <mrow><mn>2</mn></mrow>      </msub>      <mo>)</mo>  </math>    <math id="m4">      <mo>(</mo>      <msub>          <mrow><mi>p</mi></mrow>          <mrow><mn>2</mn><mo>+</mo><mi>s</mi></mrow>      </msub>      <mo>)</mo>  </math>    <math id="m5">      <mo>(</mo>      <mi>p</mi>      <mn>2</mn>      <mi>t</mi>      <mo>)</mo>  </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="mo">      <xsl:variable name="varPreceded2">          <xsl:value-of select="preceding::text()[normalize-space(.)!=''][2][generate-id(ancestor::math)=generate-id(current()/ancestor::math)]"/>      </xsl:variable>        <xsl:choose>          <xsl:when test="contains(., ')') and matches($varPreceded2, '^(f|j|p|y|g)$')               and               preceding::text()[normalize-space(.)!=''][2][generate-id(ancestor::math)=generate-id(current()/ancestor::math)]               is               (preceding::msub[1]/*[1]/descendant-or-self::*[text()][1])">              <mspace/><xsl:copy><xsl:apply-templates select="@* | node()"/></xsl:copy>          </xsl:when>          <xsl:otherwise>              <xsl:copy><xsl:apply-templates select="@* | node()"/></xsl:copy>          </xsl:otherwise>      </xsl:choose>  </xsl:template>    </xsl:stylesheet>    

Required Result (comments for explaining the required result)

  <article>    <math id="m1">      <mo>(</mo>      <msub>          <mi>p</mi>          <mn>2</mn>      </msub>      <mspace/><!-- Here Space required,  because 'before closing bracket first preceded text belongs to MSUB element, and first child of MSUB is having 'p'-->      <mo>)</mo>  </math>    <math id="m2">      <mo>(</mo>      <msub>          <mrow><mi>r</mi></mrow>          <mrow><mi>p</mi></mrow>      </msub>      <mn>8</mn>      <!-- Space is not required here, because 'p' is not first child's desendant text, that is second ones-->      <mo>)</mo>  </math>    <math id="m3">      <mo>(</mo>      <msub>          <mrow><mi>p</mi></mrow>          <mrow><mn>2</mn></mrow>      </msub>      <mspace/><!-- Here Space required,  because 'before closing bracket first preceded text belongs to MSUB element, and first child of MSUB is having  'p'-->      <mo>)</mo>  </math>    <math id="m4">      <mo>(</mo>      <msub>          <mrow><mi>p</mi></mrow>          <mrow><mn>2</mn><mo>+</mo><mi>s</mi></mrow>      </msub>      <mspace/><!-- Here Space required,  because 'before closing bracket first preceded text belongs to MSUB element, and first child of MSUB is having  'p', 'p' is not preceded[2] text, even thou bracket's preceded text is SUBSCRIPT's text where 'p' is first child's text-->      <mo>)</mo>  </math>    <math id="m5">      <mo>(</mo>      <mi>p</mi>      <mi>t</mi>      <!-- Space not required because 'p' not a part of SUBSCRIPT -->      <mo>)</mo>  </math>    </article>    

No comments:

Post a Comment