Skip to content

Instantly share code, notes, and snippets.

@dmj
Created August 29, 2019 06:16
Show Gist options
  • Save dmj/ad64cd44435adf0462370b12f3a563b0 to your computer and use it in GitHub Desktop.
Save dmj/ad64cd44435adf0462370b12f3a563b0 to your computer and use it in GitHub Desktop.
Merge siblings
<xsl:transform version="2.0"
xmlns:fn="tag:[email protected],2019:merge-siblings"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:function name="fn:sibling-group-key" as="xs:string">
<xsl:param name="node" as="node()"/>
<xsl:value-of select="generate-id(fn:sibling-group($node)[1])"/>
</xsl:function>
<xsl:function name="fn:sibling-group" as="node()+">
<xsl:param name="node" as="node()"/>
<xsl:sequence select="fn:sibling-group-head($node) union fn:sibling-group-tail($node)"/>
</xsl:function>
<xsl:function name="fn:sibling-group-tail" as="node()+">
<xsl:param name="node" as="node()"/>
<xsl:variable name="following" select="$node/following-sibling::node()"/>
<xsl:variable name="preceding" select="$node/preceding-sibling::node()"/>
<xsl:sequence select="($node, $following[every $n in (preceding-sibling::node() except $preceding) satisfies fn:is-sibling-group-member($n)])"/>
</xsl:function>
<xsl:function name="fn:sibling-group-head" as="node()+">
<xsl:param name="node" as="node()"/>
<xsl:variable name="following" select="$node/following-sibling::node()"/>
<xsl:variable name="preceding" select="$node/preceding-sibling::node()"/>
<xsl:sequence select="($preceding[every $n in (following-sibling::node() except $following) satisfies fn:is-sibling-group-member($n)], $node)"/>
</xsl:function>
<xsl:function name="fn:is-sibling-group-member" as="xs:boolean">
<xsl:param name="node" as="node()?"/>
<xsl:sequence select="false()"/>
</xsl:function>
</xsl:transform>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment