This conversion is :
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:key name="kSectsByPara" match="section[not(@paragraphMarker='true')]" use="generate-id(preceding-sibling::* [@paragraphMarker='true'] [1] )" /> <xsl:template match="*[@paragraphMarker='true']"> <p> <xsl:copy-of select= "text()|key('kSectsByPara', generate-id())/text()"/> </p> </xsl:template> <xsl:template match="*/*[not(@paragraphMarker='true')]"/> </xsl:stylesheet>
when applied to the provided XML document :
<xmldoc> <section paragraphMarker="true">Part 1. </section> <section paragraphMarker="false">Part 2. </section> <section paragraphMarker="false">Part 3. </section> <section paragraphMarker="true">Part 4. </section> <section paragraphMarker="true">Part 5. </section> <section paragraphMarker="false">Part 6. </section> </xmldoc>
creates the desired, correct result :
<p>Part 1. Part 2. Part 3. </p> <p>Part 4. </p> <p>Part 5. Part 6. </p>
Explanation
<xsl:key> with the name "kSectsByPara" defines the mapping between the generate-id() the section attribute with the paragraphMarker="true" attribute and the group of next section elements that have their own paragraphMarker="false" attribute.
source share