Advertisement
Guest User

Merge sibling elements separated by blankspace

a guest
Jan 7th, 2014
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <xsl:transform version="1.0"
  3.  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  4. >
  5.  
  6. <!-- merge sibling elements, maybe separated with blankspace text nodes -->
  7.  
  8. <xsl:template match="*">
  9.   <xsl:copy><xsl:copy-of select="@*"/><xsl:apply-templates/></xsl:copy>
  10. </xsl:template>
  11.  
  12.  
  13. <xsl:template match="text()[not( normalize-space() )]" mode="merge"/>
  14.  
  15. <xsl:template match="text()[not( normalize-space() )][ following-sibling::node()[1][self::spoj] ]" mode="merge">
  16.   <xsl:text> </xsl:text>
  17. </xsl:template>
  18.  
  19. <xsl:template match="spoj" mode="merge">
  20.   <xsl:apply-templates/>
  21. </xsl:template>
  22.  
  23. <xsl:template name="merge">
  24.   <!-- recursive for the all following siblings in the group -->
  25.   <xsl:apply-templates select="." mode="merge"/>
  26.   <xsl:for-each select="following-sibling::node()[1][ self::text()[not(normalize-space())] or self::spoj ]">
  27.     <xsl:call-template name="merge"/>
  28.   </xsl:for-each>
  29. </xsl:template>
  30.  
  31.  
  32. <!-- skip merged -->
  33. <xsl:template match="text()[ preceding-sibling::node()[1]/self::spoj ][ following-sibling::node()[1]/self::spoj ][not(normalize-space())]"/>
  34. <xsl:template match="spoj" />
  35.  
  36. <!-- the first 'spoj' in the group -->
  37. <xsl:template match="spoj[preceding-sibling::node()[1][ self::text()[normalize-space()] or self::*[ not(self::spoj )] ] ]">
  38.   <expression>
  39.     <xsl:call-template name="merge"/>
  40.   </expression>
  41. </xsl:template>
  42.  
  43.  
  44. <xsl:output
  45.  encoding="UTF-8"
  46.  indent="yes"
  47.  method="xml"
  48. />
  49.  
  50. </xsl:transform>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement