Skip to content

Instantly share code, notes, and snippets.

@jeffreycwitt
Created November 11, 2024 01:05
Show Gist options
  • Save jeffreycwitt/07e9ca3cf73cad708f78bcf05273212f to your computer and use it in GitHub Desktop.
Save jeffreycwitt/07e9ca3cf73cad708f78bcf05273212f to your computer and use it in GitHub Desktop.
<?xml version="1.0" ?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xi="http://www.w3.org/2001/XInclude"
xmlns:tei="http://www.tei-c.org/ns/1.0" xpath-default-namespace="http://www.tei-c.org/ns/1.0" exclude-result-prefixes="tei">
<xsl:output method="xml" indent="no"/>
<xsl:template match="body//lb">
<xsl:variable name="nextPbNumber" select="./following::tei:pb[1]/@n"/>
<!--<xsl:message>Test: <xsl:value-of select="$nextPbNumber"/></xsl:message>-->
<xsl:variable name="followingPageBreak" select="count(./preceding::tei:pb[1]//following::tei:lb[not(parent::tei:reg)])"/>
<!--<xsl:message>count folllowing page break: <xsl:value-of select="$followingPageBreak"/></xsl:message>-->
<xsl:variable name="followingLineBreak" select="count(.//following::tei:lb[not(parent::tei:reg)])"/>
<!--<xsl:message>count folllowing line break: <xsl:value-of select="$followingLineBreak"/></xsl:message>-->
<xsl:variable name="pbNumber" select="./preceding::tei:pb[1]/@n"/>
<xsl:variable name="lineNumber">
<xsl:choose>
<xsl:when test="not(./preceding::tei:pb[1][ancestor::tei:body])">
<xsl:variable name="lineCount" select="$followingPageBreak - $followingLineBreak"/>
<xsl:variable name="startline"><xsl:value-of select="//tei:body/descendant::tei:lb[1]/@n"/></xsl:variable>
<xsl:value-of select="$lineCount + $startline - 1"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$followingPageBreak - $followingLineBreak"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:copy copy-namespaces="no">
<xsl:apply-templates select="./@* | node()"/>
<xsl:attribute name="ed">
<xsl:value-of select="./@ed"/>
<!--<xsl:value-of select="'#B'"/>-->
</xsl:attribute>
<xsl:attribute name="n">
<xsl:value-of select="$lineNumber"/>
</xsl:attribute>
</xsl:copy>
</xsl:template>
<!-- IdentityTransform -->
<xsl:template match="@* | node()">
<xsl:copy copy-namespaces="no">
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment