Skip to content

Instantly share code, notes, and snippets.

@mathias-goebel
Last active August 29, 2015 14:18
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 mathias-goebel/8933387edcc09146a369 to your computer and use it in GitHub Desktop.
Save mathias-goebel/8933387edcc09146a369 to your computer and use it in GitHub Desktop.
sort zones with xslt
<?xml version="1.0" encoding="UTF-8"?>
<!-- Stylesheet imporved by hriebl! -->
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:tei="http://www.tei-c.org/ns/1.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="2.0"
exclude-result-prefixes="xs tei"
xpath-default-namespace="http://www.tei-c.org/ns/1.0">
<xsl:output method="xml" indent="yes" />
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="zone">
<xsl:variable name="id" select="@xml:id"/>
<xsl:if test="not(//zone[@next = concat('#', $id)])">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
<xsl:variable name="next" select="substring-after(@next, '#')"/>
<xsl:apply-templates select="//zone[@xml:id = $next]" mode="next"/>
</xsl:if>
</xsl:template>
<xsl:template match="zone" mode="next">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
<xsl:variable name="next" select="substring-after(@next, '#')"/>
<xsl:apply-templates select="//zone[@xml:id = $next]" mode="next"/>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<TEI xmlns="http://www.tei-c.org/ns/1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.tei-c.org/ns/1.0">
<sourceDoc>
<surface n="1r">
<zone xml:id="a"/>
<zone xml:id="b" next="#d"/>
<zone xml:id="c"/>
<zone xml:id="d" next="#g"/>
<zone xml:id="e"/>
</surface>
<surface n="1v">
<zone xml:id="f"/>
<zone xml:id="g" next="#c"/>
</surface>
</sourceDoc>
</TEI>
<?xml version="1.0" encoding="UTF-8"?>
<TEI xmlns="http://www.tei-c.org/ns/1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.tei-c.org/ns/1.0">
<sourceDoc>
<surface n="1r">
<zone xml:id="a"/>
<zone xml:id="b" next="#d"/>
<zone xml:id="d" next="#g"/>
<zone xml:id="g" next="#c"/>
<zone xml:id="c"/>
<zone xml:id="e"/>
</surface>
<surface n="1v">
<zone xml:id="f"/>
</surface>
</sourceDoc>
</TEI>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:tei="http://www.tei-c.org/ns/1.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="2.0"
exclude-result-prefixes="xs tei"
xpath-default-namespace="http://www.tei-c.org/ns/1.0">
<xsl:output method="xml" indent="yes" />
<xsl:strip-space elements="*"/>
<xsl:variable name="zonesort">
<xsl:call-template name="zonesort"/>
</xsl:variable>
<xsl:template name="zonesort">
<xsl:apply-templates select="/" mode="zonesort"/>
</xsl:template>
<xsl:template match="/">
<xsl:apply-templates select="$zonesort" mode="cleanup"/>
</xsl:template>
<!-- generic copy in all modes -->
<xsl:template match="@*|node()" mode="#all">
<xsl:copy>
<xsl:apply-templates select="@*|node()" mode="#current"/>
</xsl:copy>
</xsl:template>
<xsl:template match="zone[@next][@xml:id != preceding::zone/substring-after(@next, '#')]" mode="zonesort">
<xsl:copy-of select="current()"/>
<!-- grab the next zone ¯\_(ツ)_/¯ -->
<xsl:variable name="nxt" select="substring-after(@next, '#')"/>
<xsl:call-template name="zonecopy">
<xsl:with-param name="nxt" select="$nxt"/>
</xsl:call-template>
<!-- <!-\- grab next zones next zone ¯\_(ツ)_/¯ -\->
<xsl:variable name="nxt" select="//zone[@xml:id = $nxt][@next]/substring-after(@next, '#')"/>
<xsl:apply-templates select="//zone[@xml:id = $nxt]"/>
<!-\- grab next zones next zones next zone ¯\_(ツ)_/¯ -\->
<xsl:variable name="nxt" select="//zone[@xml:id = $nxt][@next]/substring-after(@next, '#')"/>
<xsl:apply-templates select="//zone[@xml:id = $nxt]"/>
<!-\- grab next zones next zones next zone ¯\_(ツ)_/¯ -\->
<xsl:variable name="nxt" select="//zone[@xml:id = $nxt][@next]/substring-after(@next, '#')"/>
<xsl:apply-templates select="//zone[@xml:id = $nxt]"/>
<!-\- grab next zones next zones next zone ¯\_(ツ)_/¯ -\->
<xsl:variable name="nxt" select="//zone[@xml:id = $nxt][@next]/substring-after(@next, '#')"/>
<xsl:apply-templates select="//zone[@xml:id = $nxt]"/>
<!-\- grab next zones next zones next zone ¯\_(ツ)_/¯ -\->
<xsl:variable name="nxt" select="//zone[@xml:id = $nxt][@next]/substring-after(@next, '#')"/>
<xsl:apply-templates select="//zone[@xml:id = $nxt]"/>
<!-\- grab next zones next zones next zone ¯\_(ツ)_/¯ -\->
<xsl:variable name="nxt" select="//zone[@xml:id = $nxt][@next]/substring-after(@next, '#')"/>
<xsl:apply-templates select="//zone[@xml:id = $nxt]"/>
<!-\- grab next zones next zones next zone ¯\_(ツ)_/¯ -\->
<xsl:variable name="nxt" select="//zone[@xml:id = $nxt][@next]/substring-after(@next, '#')"/>
<xsl:apply-templates select="//zone[@xml:id = $nxt]"/>-->
</xsl:template>
<xsl:template name="zonecopy">
<xsl:param name="nxt"/>
<xsl:copy-of select="//zone[@xml:id = $nxt]"/>
<xsl:if test="//zone[@xml:id = $nxt]/@next">
<xsl:call-template name="zonecopy">
<xsl:with-param name="nxt" select="//zone[@xml:id = $nxt]/substring-after(@next, '#')"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template match="zone[@xml:id = preceding::zone/@xml:id]" mode="cleanup"/>
<xsl:template match="sourceDoc/surface" mode="whatsnext0">
<xsl:element name="pb" namespace="http://www.tei-c.org/ns/1.0">
<xsl:attribute name="n" select="@n"/>
</xsl:element>
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
@mathias-goebel
Copy link
Author

trying to sort the zones according to the next attribute

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment