Skip to content

Instantly share code, notes, and snippets.

@emchateau
Created February 19, 2021 21:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emchateau/10397d9c65136919ff3b5dc66df040b7 to your computer and use it in GitHub Desktop.
Save emchateau/10397d9c65136919ff3b5dc66df040b7 to your computer and use it in GitHub Desktop.
getDeepest
//*[not(*)][count(ancestor::node())
=
max(//*[not(*)]/count(ancestor::node()))
]
<xsl:for-each-group select="//title"
group-by="count(ancestor::*)">
<xsl:sort select="current-grouping-key()">
<xsl:if test="position()=last()">
<xsl:copy-of select="current-group()"/>
</if>
</sort>
</xsl:for-each-group>
innermost(//*)[count(ancestor::node())
=
max(//*[not(*)]/count(ancestor::node()))
]
<xsl:accumulator name="depth" initial-value="0, 0" as="xs:integer*">
<xsl:accumulator-rule match="*" phase="start"
select="
let $myDepth := head($value) + 1
return
($myDepth, max(($myDepth, tail($value))))"/>
<xsl:accumulator-rule match="*" phase="end"
select="
head($value) - 1, tail($value)
"/>
</xsl:accumulator>
<xsl:function name="re:find-spine" as="node()+">
<xsl:param name="node" as="node()"/>
<xsl:variable name="end1" select="re:find-deepest($node) => path()"/>
<xsl:variable name="newTree" as="node()" select="$node => re:make-root($end1)"/>
<xsl:variable name="end2" as="node()" select="re:find-deepest($newTree)"/>
<xsl:sequence select="$end2/ancestor-or-self::*"/>
</xsl:function>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment