how to find and apply style for following immediate tag in xslt

Viewed 32

i am trying to create a tex file for the below MWE content.

need to update the below requirements when <displayMaths> elements are stacked immediately (follows/proceeding immediatedly),

  1. to place the command \turnoffbeloweqspace before the first <displayMaths>

  2. at the end of the last <displayMaths> need to place \turnonabovespace at end

  3. in between all middle <displayMaths> need to place \turnoffaboveeqspace

  4. the above 3 points should not be done if <displayMaths> does not following/preceeding immediately

I am not getting expected result, How to get the desired output

MWE:

<?xml version="1.0" encoding="UTF-8"?>
<book>
<p>Assuming that income X of a unit is a random variable whose domain is a set of real numbers varying from  is defined as
<displayMaths id="oso-9780198852841-chapter-2-displayMaths-1"><graphic id="oso-9780198852841-chapter-2-graphic-6" electronicRight="Y" role="display" sysId="oso-9780198852841-math-0006.gif"/><texMath><![CDATA[\begin{align*}F\left( x \right) = P\left( {X \le x} \right)\end{align*}]]></texMath></displayMaths>
</p>
<p>and<displayMaths id="oso-9780198852841-chapter-2-displayMaths-19"><graphic id="oso-9780198852841-chapter-2-graphic-35" electronicRight="Y" role="display" sysId="oso-9780198852841-math-0033.gif"/><texMath><![CDATA[\begin{align}{P_t}\left( {r,u} \right) = {p_{u\,}} > 0\end{align}]]></texMath></displayMaths><displayMaths id="oso-9780198852841-chapter-2-displayMaths-20"><graphic id="oso-9780198852841-chapter-2-graphic-36" electronicRight="Y" role="display" sysId="oso-9780198852841-math-0034.gif"/><texMath><![CDATA[\begin{align*}if - n \le u \le - r\end{align*}]]></texMath></displayMaths>
</p>
<p>The density function of the Pareto distribution is obtained by differentiating 
<displayMaths id="oso-9780198852841-chapter-2-displayMaths-7"><graphic id="oso-9780198852841-chapter-2-graphic-17" electronicRight="Y" role="display" sysId="oso-9780198852841-math-0015.gif"/><texMath><![CDATA[\begin{align}\begin{array}{l}f\left( x \right) = \alpha x_0^\alpha {x^{ - 1 - \alpha }}\,\,\,\,\,\,\,\,\,{\rm{\,when}}\,x \ge {x_0}\\{\rm{ }} = 0\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,{\rm{when}}\,x < {x_0}\,.\end{array}\end{align}]]></texMath></displayMaths>    <displayMaths id="oso-9780198852841-chapter-2-displayMaths-8"><graphic id="oso-9780198852841-chapter-2-graphic-18" electronicRight="Y" role="display" sysId="oso-9780198852841-math-0016.gif"/><texMath><![CDATA[\begin{align}\log \left( {R\left( x \right)} \right) = \alpha \log \left( {{x_0}} \right) - \alpha {\rm{log}}\left( x \right).\end{align}]]></texMath></displayMaths><displayMaths id="oso-9780198852841-chapter-2-displayMaths-9"><graphic id="oso-9780198852841-chapter-2-graphic-19" electronicRight="Y" role="display" sysId="oso-9780198852841-math-0017.gif"/><texMath><![CDATA[\begin{align*}E\left( x \right) = \alpha x_0^\alpha \int_{{x_0}}^\infty {{x^{ - \alpha }}dx} ,\end{align*}]]></texMath></displayMaths>
</p>
<p>The function has a domain then it follows that
<displayMaths id="oso-9780198852841-chapter-2-displayMaths-2"><graphic id="oso-9780198852841-chapter-2-graphic-10" electronicRight="Y" role="display" sysId="oso-9780198852841-math-0008.gif"/><texMath><![CDATA[\begin{align*}\frac{{dF\left( X \right)}}{{dX}} = f\left( x \right)\end{align*}]]></texMath></displayMaths><displayMaths id="oso-9780198852841-chapter-2-displayMaths-4"><graphic id="oso-9780198852841-chapter-2-graphic-13" electronicRight="Y" role="display" sysId="oso-9780198852841-math-0011.gif"/><texMath><![CDATA[\begin{align*}F\left( x \right) = \int_0^x {f\left( X \right)dx} \end{align*}]]></texMath></displayMaths><displayMaths id="oso-9780198852841-chapter-2-displayMaths-5"><graphic id="oso-9780198852841-chapter-2-graphic-14" electronicRight="Y" role="display" sysId="oso-9780198852841-math-0012.gif"/><texMath><![CDATA[\begin{align*}P\left( {x \le X \le x + dx} \right) = f\left( x \right),\end{align*}]]></texMath></displayMaths><displayMaths id="oso-9780198852841-chapter-2-displayMaths-6"><graphic id="oso-9780198852841-chapter-2-graphic-16" electronicRight="Y" role="display" sysId="oso-9780198852841-math-0014.gif"/><texMath><![CDATA[\begin{align}\begin{array}{l}R\left( x \right) = {\left( {\frac{x}{{{x_0}}}} \right)^{ - \alpha }}\,\,\,\,\,\,\,\,\,{\rm{when}}\,x > {x_0}\\{\rm{ }} = 1\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,{\rm{ }}\,\,{\rm{when}}\,x \le {x_0}\,\,\end{array}\end{align}]]></texMath></displayMaths>
</p>
<p>The function has a domain then it follows that
<displayMaths id="oso-9780198852841-chapter-2-displayMaths-12"><graphic id="oso-9780198852841-chapter-2-graphic-10" electronicRight="Y" role="display" sysId="oso-9780198852841-math-0008.gif"/><texMath><![CDATA[\begin{align*}\frac{{dF\left( X \right)}}{{dX}} = f\left( x \right)\end{align*}]]></texMath></displayMaths>
and
<displayMaths id="oso-9780198852841-chapter-2-displayMaths-13"><graphic id="oso-9780198852841-chapter-2-graphic-13" electronicRight="Y" role="display" sysId="oso-9780198852841-math-0011.gif"/><texMath><![CDATA[\begin{align*}F\left( x \right) = \int_0^x {f\left( X \right)dx} \end{align*}]]></texMath></displayMaths>
follow
<displayMaths id="oso-9780198852841-chapter-2-displayMaths-14"><graphic id="oso-9780198852841-chapter-2-graphic-14" electronicRight="Y" role="display" sysId="oso-9780198852841-math-0012.gif"/><texMath><![CDATA[\begin{align*}P\left( {x \le X \le x + dx} \right) = f\left( x \right),\end{align*}]]></texMath></displayMaths><displayMaths id="oso-9780198852841-chapter-2-displayMaths-15"><graphic id="oso-9780198852841-chapter-2-graphic-16" electronicRight="Y" role="display" sysId="oso-9780198852841-math-0014.gif"/><texMath><![CDATA[\begin{align}\begin{array}{l}R\left( x \right) = {\left( {\frac{x}{{{x_0}}}} \right)^{ - \alpha }}\,\,\,\,\,\,\,\,\,{\rm{when}}\,x > {x_0}\\{\rm{ }} = 1\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,{\rm{ }}\,\,{\rm{when}}\,x \le {x_0}\,\,\end{array}\end{align}]]></texMath></displayMaths>
</p>
</book>

XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:m="http://www.w3.org/1998/Math/MathML"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:mml="http://www.w3.org/1998/Math/MathML"
    xmlns:xlink="http://www.w3.org/1999/xlink"
    xmlns:functx="http://www.functx.com"
    xmlns:xhtml="http://www.w3.org/1999/xhtml"
    version="2.0" >

<xsl:template match="p">
    <xsl:apply-templates/>
</xsl:template>

<xsl:template match="displayMaths">
    
    <xsl:variable name="Nodename" select="preceding-sibling::*[1]/name()"/>
    <xsl:variable name="Eqnfollows" select="following-sibling::*[1]/name()"/>
    <!-- <xsl:value-of select="if($Nodename='displayMaths') then '\turnoffabovespace' else ''"/> -->
    <xsl:value-of select="//*[text()='displayMaths ']/following-sibling::*[1]" />
    <xsl:apply-templates/>
    <xsl:value-of select="if(not($Eqnfollows='displayMaths')) then (if($Nodename='displayMaths') then '\turnonabovespace' else '') else ''"/>
</xsl:template>

<xsl:template match="graphic"/>


</xsl:stylesheet>

Expexted Result:

Assuming that income X of a unit is a random variable whose domain is a set of real numbers varying from  is defined as
\begin{align*}F\left( x \right) = P\left( {X \le x} \right)\end{align*}

and
\turnoffbeloweqspace
\begin{align}{P_t}\left( {r,u} \right) = {p_{u\,}} &gt; 0\end{align}
\turnoffabovespace
\begin{align*}if - n \le u \le - r\end{align*}
\turnonabovespace

The density function of the Pareto distribution is obtained by differentiating 
\turnoffbeloweqspace
\begin{align}\begin{array}{l}f\left( x \right) = \alpha x_0^\alpha {x^{ - 1 - \alpha }}\,\,\,\,\,\,\,\,\,{\rm{\,when}}\,x \ge {x_0}\\{\rm{ }} = 0\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,{\rm{when}}\,x &lt; {x_0}\,.\end{array}\end{align}
\turnoffabovespace
\begin{align}\log \left( {R\left( x \right)} \right) = \alpha \log \left( {{x_0}} \right) - \alpha {\rm{log}}\left( x \right).\end{align}
\turnoffabovespace
\begin{align*}E\left( x \right) = \alpha x_0^\alpha \int_{{x_0}}^\infty {{x^{ - \alpha }}dx} ,\end{align*}
\turnonabovespace

The function has a domain then it follows that
\turnoffbeloweqspace
\begin{align*}\frac{{dF\left( X \right)}}{{dX}} = f\left( x \right)\end{align*}
\turnoffabovespace
\begin{align*}F\left( x \right) = \int_0^x {f\left( X \right)dx} \end{align*}
\turnoffabovespace
\begin{align*}P\left( {x \le X \le x + dx} \right) = f\left( x \right),\end{align*}
\turnoffabovespace
\begin{align}\begin{array}{l}R\left( x \right) = {\left( {\frac{x}{{{x_0}}}} \right)^{ - \alpha }}\,\,\,\,\,\,\,\,\,{\rm{when}}\,x &gt; {x_0}\\{\rm{ }} = 1\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,{\rm{ }}\,\,{\rm{when}}\,x \le {x_0}\,\,\end{array}\end{align}
\turnonabovespace

The function has a domain then it follows that
\begin{align*}\frac{{dF\left( X \right)}}{{dX}} = f\left( x \right)\end{align*}
and
\begin{align*}F\left( x \right) = \int_0^x {f\left( X \right)dx} \end{align*}
follow
\turnoffbeloweqspace
\begin{align*}P\left( {x \le X \le x + dx} \right) = f\left( x \right),\end{align*}
\turnoffabovespace
\begin{align}\begin{array}{l}R\left( x \right) = {\left( {\frac{x}{{{x_0}}}} \right)^{ - \alpha }}\,\,\,\,\,\,\,\,\,{\rm{when}}\,x &gt; {x_0}\\{\rm{ }} = 1\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,{\rm{ }}\,\,{\rm{when}}\,x \le {x_0}\,\,\end{array}\end{align}
\turnonabovespace
1 Answers

It sounds as if you want to process e.g. <xsl:template match="p"><xsl:for-each-group select="node()" group-adjacent="boolean(self::displayMaths)"><xsl:choose><xsl:when test="current-grouping-key()"><xsl:apply-templates select="current-group()" mode="displayMaths"/></xsl:when><xsl:otherwise><xsl:apply-templates select="current-group()"/></xsl:otherwise></xsl:choose></xsl:template>, in <xsl:template match="displayMaths" mode="displayMaths"> check the position() and for e.g. 1 output the wanted tex, for last() the respective tex, in between the default.

Related