Skip to content

Instantly share code, notes, and snippets.

@gimsieke
Created November 24, 2019 17:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gimsieke/529dab000386a45d6136e850a80ac726 to your computer and use it in GitHub Desktop.
Save gimsieke/529dab000386a45d6136e850a80ac726 to your computer and use it in GitHub Desktop.
solution for a splitting problem raised by Rick Quatro on Mulberry XSL list on 2019-11-24
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:split="http://www.le-tex.de/XSLT/split"
exclude-result-prefixes="xs" version="3.0">
<!-- invoke it with -lib:lib/split.xsl if you checked out
https://subversion.le-tex.de/common/presentations/2019-02-09_xmlprague_xslt-upward-projection/lib/
to lib/ and if you are using Saxon 9.8+ -->
<xsl:use-package name="http://www.le-tex.de/XSLT/split"
package-version="1.0"/>
<xsl:mode on-no-match="shallow-copy"/>
<xsl:mode name="insert-marker" on-no-match="shallow-copy"/>
<xsl:template match="p//text()" mode="insert-marker">
<xsl:analyze-string select="." regex="[\.\?](\s+|$)">
<xsl:matching-substring>
<xsl:value-of select="."/>
<eos/>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:template>
<xsl:template match="p[node()]">
<xsl:variable name="p-with-markers" as="element(p)">
<xsl:apply-templates select="." mode="insert-marker"/>
</xsl:variable><!-- this hasn't changed -->
<xsl:variable name="chunks" as="document-node(element(split:chunks))">
<xsl:apply-templates select="$p-with-markers"
mode="split:split-entrypoint"><!-- mode provided by
lib/split.xsl -->
<xsl:with-param name="group-start-exp" as="xs:string"
select="'self::eos'"/><!-- Will be evaluated as an XPath
expression for each node in a for-each-group[@group-starting-with]
population. If population node satisfies the expression, it will
start a group.-->
<xsl:with-param name="keep-splitting-node" as="xs:boolean"
select="false()"/><!-- remove <eos/> after splitting -->
</xsl:apply-templates>
</xsl:variable>
<xsl:copy-of select="$chunks/split:chunks/split:chunk/p[node()]"
copy-namespaces="no"/>
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment