fix: correct xsl template to remove any existing onscreen episode-num before replacing it with new one

This commit is contained in:
Nic D 2026-02-13 07:29:02 -05:00
parent ca23ded3dc
commit 65da0ab778

View File

@ -2,43 +2,69 @@
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- identity copy -->
<!-- identity -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<!-- modify programme/episode-num -->
<xsl:template match="programme[season-num and episode-num]/episode-num">
<xsl:variable name="s" select="../season-num"/>
<xsl:variable name="e" select="."/>
<xsl:variable name="s2">
<xsl:choose>
<xsl:when test="string-length($s) = 1">
<xsl:value-of select="concat('0',$s)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$s"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<!-- Special handling for programme -->
<xsl:template match="programme">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:variable name="e2">
<xsl:choose>
<xsl:when test="string-length($e) = 1">
<xsl:value-of select="concat('0',$e)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$e"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<!-- Grab xmltv_ns once -->
<xsl:variable name="raw"
select="normalize-space(episode-num[@system='xmltv_ns'][1])"/>
<episode-num>
<xsl:value-of select="concat('S',$s2,'E',$e2)"/>
</episode-num>
<xsl:variable name="season0"
select="number(substring-before($raw, '.'))"/>
<xsl:variable name="rest"
select="substring-after($raw, '.')"/>
<xsl:variable name="episode"
select="number(normalize-space(substring-before($rest, '.')))"/>
<xsl:variable name="season" select="$season0 + 1"/>
<!-- zero pad -->
<xsl:variable name="s2">
<xsl:choose>
<xsl:when test="$season &lt; 10">
<xsl:value-of select="concat('0',$season)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$season"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="e2">
<xsl:choose>
<xsl:when test="$episode &lt; 10">
<xsl:value-of select="concat('0',$episode)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$episode"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="onscreenValue"
select="concat('S',$s2,'E',$e2)"/>
<!-- Copy all children EXCEPT existing onscreen -->
<xsl:apply-templates
select="node()[not(self::episode-num[@system='onscreen'])]" />
<!-- Add the onscreen one at the end -->
<episode-num system="onscreen">
<xsl:value-of select="$onscreenValue"/>
</episode-num>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
</xsl:stylesheet>