Please suggest for how to split the xml nodes based on particular comment text (<!--Break-->). I tried thru using xsl:text disable-output-escaping format to place the required closing and opening tags (elements).
- My code is static, how to make that dynamic code which will work for any numbers of ancestor with respect to comment text, i.e., if ancestor count 'n', then from n to 1 how to make use of call-template method.
- some empty nodes are presented which are not ancestor to the content, how to avoid these
I referred some suggestions from our site, but find difficult to understand the logics. Please suggest. (XSLT 2 version)
XSLT:
<root> <a> <b>The text1 <c> <d>The text2</d><d>The text3</d><!--Break--><d>The text4</d> <e>The text5<!--Break--></e> </c> <f>The text6</f> </b> </a> </root>
XSLT:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> <xsl:template match="@*|node()"> <xsl:copy><xsl:apply-templates select="node()|@*"/></xsl:copy> </xsl:template> <xsl:template match="//comment()[contains(., 'Break')]"> <xsl:variable name="varAncestorCount" select="count(ancestor::*)"/> <xsl:choose> <xsl:when test="$varAncestorCount = 4"> <xsl:text disable-output-escaping="yes"></</xsl:text><xsl:value-of select="ancestor::*[1]/name()"/><xsl:text disable-output-escaping="yes">></xsl:text> <xsl:text disable-output-escaping="yes"></</xsl:text><xsl:value-of select="ancestor::*[2]/name()"/><xsl:text disable-output-escaping="yes">></xsl:text> <xsl:text disable-output-escaping="yes"></</xsl:text><xsl:value-of select="ancestor::*[3]/name()"/><xsl:text disable-output-escaping="yes">></xsl:text> <xsl:text disable-output-escaping="yes"></</xsl:text><xsl:value-of select="ancestor::*[4]/name()"/><xsl:text disable-output-escaping="yes">></xsl:text> <xsl:comment><xsl:value-of select="."/></xsl:comment> <xsl:text disable-output-escaping="yes"><</xsl:text><xsl:value-of select="ancestor::*[4]/name()"/><xsl:text disable-output-escaping="yes">></xsl:text> <xsl:text disable-output-escaping="yes"><</xsl:text><xsl:value-of select="ancestor::*[3]/name()"/><xsl:text disable-output-escaping="yes">></xsl:text> <xsl:text disable-output-escaping="yes"><</xsl:text><xsl:value-of select="ancestor::*[2]/name()"/><xsl:text disable-output-escaping="yes">></xsl:text> <xsl:text disable-output-escaping="yes"><</xsl:text><xsl:value-of select="ancestor::*[1]/name()"/><xsl:text disable-output-escaping="yes">></xsl:text> </xsl:when> <xsl:when test="$varAncestorCount = 5"> <xsl:text disable-output-escaping="yes"></</xsl:text><xsl:value-of select="ancestor::*[1]/name()"/><xsl:text disable-output-escaping="yes">></xsl:text> <xsl:text disable-output-escaping="yes"></</xsl:text><xsl:value-of select="ancestor::*[2]/name()"/><xsl:text disable-output-escaping="yes">></xsl:text> <xsl:text disable-output-escaping="yes"></</xsl:text><xsl:value-of select="ancestor::*[3]/name()"/><xsl:text disable-output-escaping="yes">></xsl:text> <xsl:text disable-output-escaping="yes"></</xsl:text><xsl:value-of select="ancestor::*[4]/name()"/><xsl:text disable-output-escaping="yes">></xsl:text> <xsl:text disable-output-escaping="yes"></</xsl:text><xsl:value-of select="ancestor::*[5]/name()"/><xsl:text disable-output-escaping="yes">></xsl:text> <xsl:comment><xsl:value-of select="."/></xsl:comment> <xsl:text disable-output-escaping="yes"><</xsl:text><xsl:value-of select="ancestor::*[5]/name()"/><xsl:text disable-output-escaping="yes">></xsl:text> <xsl:text disable-output-escaping="yes"><</xsl:text><xsl:value-of select="ancestor::*[4]/name()"/><xsl:text disable-output-escaping="yes">></xsl:text> <xsl:text disable-output-escaping="yes"><</xsl:text><xsl:value-of select="ancestor::*[3]/name()"/><xsl:text disable-output-escaping="yes">></xsl:text> <xsl:text disable-output-escaping="yes"><</xsl:text><xsl:value-of select="ancestor::*[2]/name()"/><xsl:text disable-output-escaping="yes">></xsl:text> <xsl:text disable-output-escaping="yes"><</xsl:text><xsl:value-of select="ancestor::*[1]/name()"/><xsl:text disable-output-escaping="yes">></xsl:text> </xsl:when> </xsl:choose> </xsl:template> </xsl:stylesheet>
Desired Output:
<root> <a> <b>The text1 <c> <d>The text2</d> <d>The text3</d> </c> </b> </a> </root> <!--Break--> <root> <a> <b> <c> <d>The text4</d> <e>The text5</e> </c> </b> </a> </root> <!--Break--> <root> <a> <b> <f>The text6</f> </b> </a> </root>