Reputation: 55
I can't find a working solution to my problem. I need to merge two elements with the same name into one when they are in specific position in the document: tags hi when between them stays lb break='no'. How can I do that? The xml code is:
<p>
<lb n="28"/> Lorem ipsum dolor sit
<lb n="29"/> amet, consectetur, <hi rend="u">adi</hi>
<lb n="30" break="no"/><hi rend="u">pisci</hi> elit...
</p>
I transform it into a floating text without line breakes and as there is a hyphen there I need to merge the two elements into one. Now I get always a blank between them.
<p>Lorem ipsum dolor sit amet, consectetur <span class="u">adipisici</span> elit...</p>
Thanks a lot!
Upvotes: 1
Views: 421
Reputation: 167471
Assuming an XSLT 2.0 processor like Saxon 9 or XmlPrime you can use
<xsl:stylesheet
version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs">
<xsl:template match="p">
<xsl:copy>
<xsl:for-each-group select="node() except text()[not(normalize-space())]"
group-adjacent="self::hi or self::lb[@break = 'no']">
<xsl:choose>
<xsl:when test="current-grouping-key()">
<span class="{current-group()[self::hi][1]/@rend}">
<xsl:apply-templates select="current-group()/node()"/>
</span>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="current-group()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
to transform
<p>
<lb n="28"/> Lorem ipsum dolor sit
<lb n="29"/> amet, consectetur, <hi rend="u">adi</hi>
<lb n="30" break="no"/><hi rend="u">pisci</hi> elit...
</p>
into
<p> Lorem ipsum dolor sit
amet, consectetur, <span class="u">adipisci</span> elit...
</p>
Upvotes: 1