I have the following html markup.
<p>
<i>This is italic tag</i>. This text belongs to x element. The text from here should <sub>2</sub> belong to y element.
</p>
And here is the template rule I am using:
`<xsl:template match="p/text()[not(parent::i)]">
<xsl:variable name="tokens" select="tokenize(.,'.')"/>
<xsl:element name="x">
<xsl:value-of select="normalize-space($tokens[2])"/>
</xsl:element>
<xsl:element name="y">
<xsl:value-of select="normalize-space(string-join($tokens[position() > 2 and position() != last()], '.'))"/>
</xsl:element>
</xsl:template>`
`<xsl:template match="sub">
<xsl:element name="SUBSCRIPT">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>`
`<xsl:template match="p">
<xsl:element name="PARA">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>`
`<xsl:template match="i">
<xsl:element name="EMPHASIS">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>`
When running the transformation some text is missing in the output.
This is what I am getting now:
`<PARA>
<EMPHASIS>This is italic tag</EMPHASIS>
<X>This text belongs to x element</X>
<Y/>
<SUBSCRIPT>2</SUBSCRIPT>
<X/>
<Y/>
</PARA>`
What I am expecting:
`<PARA>
<EMPHASIS>This is italic tag</EMPHASIS>
<X>This text belongs to x element</X>
<Y>The text from here should <SUBSCRIPT>2</SUBSCRIPT> belong to y element</Y>
</PARA>`
How can I make sure that when dividing the p tag’s text node and its element node (child element node except for i child) there is no content loss and sub element is also transformed correctly?
New contributor
Bakhovuddin Latifjanov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.