--- /dev/null
+<?xml version="1.0" encoding="UTF-8"?>
+<xsl:stylesheet version="1.0" xmlns:marc="http://www.loc.gov/MARC21/slim"
+ xmlns:xml="http://www.w3.org/XML/1998/namespace"
+ xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
+ <!-- merge_fixes_bibliographic.xslt - Merge title fixes for bibliographic entries.
+ Copyright (C) 2025 Pierre Choffet
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of version 3 of the GNU General Public License as
+ published by the Free Software Foundation.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+ -->
+ <xsl:output indent="yes" method="xml" encoding="utf-8" />
+
+ <xsl:param name="fixes-path" />
+ <xsl:param name="leader" />
+ <xsl:param name="original-title" />
+ <xsl:param name="title-lang" />
+ <xsl:param name="fixed-title" />
+ <xsl:param name="original-subtitle" />
+ <xsl:param name="subtitle-lang" />
+ <xsl:param name="fixed-subtitle" />
+
+ <xsl:variable name="doublequote-workaround">%DOUBLEQUOTE-WORKAROUND%</xsl:variable>
+ <xsl:variable name="original-title-clean">
+ <xsl:call-template name="string-replace-all">
+ <xsl:with-param name="text" select="$original-title" />
+ <xsl:with-param name="replace" select="$doublequote-workaround" />
+ <xsl:with-param name="by" select="'"'" />
+ </xsl:call-template>
+ </xsl:variable>
+ <xsl:variable name="fixed-title-clean">
+ <xsl:call-template name="string-replace-all">
+ <xsl:with-param name="text" select="$fixed-title" />
+ <xsl:with-param name="replace" select="$doublequote-workaround" />
+ <xsl:with-param name="by" select="'"'" />
+ </xsl:call-template>
+ </xsl:variable>
+ <xsl:variable name="original-subtitle-clean">
+ <xsl:call-template name="string-replace-all">
+ <xsl:with-param name="text" select="$original-subtitle" />
+ <xsl:with-param name="replace" select="$doublequote-workaround" />
+ <xsl:with-param name="by" select="'"'" />
+ </xsl:call-template>
+ </xsl:variable>
+ <xsl:variable name="fixed-subtitle-clean">
+ <xsl:call-template name="string-replace-all">
+ <xsl:with-param name="text" select="$fixed-subtitle" />
+ <xsl:with-param name="replace" select="$doublequote-workaround" />
+ <xsl:with-param name="by" select="'"'" />
+ </xsl:call-template>
+ </xsl:variable>
+
+ <xsl:key name="record-by-id" match="/records/record" use="@id" />
+
+ <xsl:template match="node()|@*">
+ <xsl:copy>
+ <xsl:apply-templates select="node()|@*" />
+ </xsl:copy>
+ </xsl:template>
+
+ <xsl:template match="/records">
+ <!-- Rewrite -->
+
+ <xsl:choose>
+ <xsl:when test="key('record-by-id', $leader)">
+ <!-- Fix document has an entry with the given leader -->
+ <xsl:copy>
+ <xsl:apply-templates select="node()|@*" />
+ </xsl:copy>
+ </xsl:when>
+ <xsl:otherwise>
+ <!-- No entry with the given leader in fix document -->
+ <xsl:copy select=".">
+ <xsl:copy-of select="record" />
+ <xsl:element name="record">
+ <xsl:attribute name="id">
+ <xsl:value-of select="$leader" />
+ </xsl:attribute>
+
+ <!-- Add title -->
+ <!-- TODO: Deal with "[Sans titre]" -->
+ <xsl:if test="$original-title-clean and $title-lang and $fixed-title-clean">
+ <xsl:call-template name="write-title" />
+ </xsl:if>
+
+ <!-- Add subtitle -->
+ <xsl:if test="$original-subtitle-clean and $subtitle-lang and $fixed-subtitle-clean">
+ <xsl:element name="fix">
+ <xsl:attribute name="type">
+ <xsl:text>subtitle</xsl:text>
+ </xsl:attribute>
+ <xsl:attribute name="source">
+ <xsl:value-of select="$original-subtitle-clean" />
+ </xsl:attribute>
+ <xsl:attribute name="xml:lang">
+ <xsl:value-of select="$subtitle-lang" />
+ </xsl:attribute>
+ <xsl:value-of select="$fixed-subtitle-clean" />
+ </xsl:element>
+ </xsl:if>
+ </xsl:element>
+ </xsl:copy>
+ </xsl:otherwise>
+ </xsl:choose>
+ </xsl:template>
+
+ <xsl:template match="/records/record">
+ <xsl:choose>
+ <xsl:when test="@id != $leader">
+ <xsl:copy-of select="." />
+ </xsl:when>
+ <xsl:otherwise>
+ <xsl:copy select=".">
+ <xsl:copy-of select="@*" />
+ <!-- Keep any title in another language but warn for them -->
+ <xsl:copy-of select="fix[@type='title' and @xml:lang != $title-lang]" />
+ <xsl:for-each select="fix[@type='title' and @xml:lang != $title-lang]">
+ <xsl:message terminate="no">
+ <xsl:text>L’entrée avec le leader </xsl:text>
+ <xsl:value-of select="../@id" />
+ <xsl:text> a un titre dans la langue « </xsl:text>
+ <xsl:value-of select="@xml:lang" />
+ <xsl:text> » qui a été conservé en plus du nouveau titre en « </xsl:text>
+ <xsl:value-of select="$title-lang" />
+ <xsl:text> ».</xsl:text>
+ </xsl:message>
+ </xsl:for-each>
+ <!-- Add new title -->
+ <xsl:call-template name="write-title" />
+ </xsl:copy>
+ </xsl:otherwise>
+ </xsl:choose>
+ </xsl:template>
+
+ <xsl:template name="write-title">
+ <xsl:element name="fix">
+ <xsl:attribute name="type">
+ <xsl:text>title</xsl:text>
+ </xsl:attribute>
+ <xsl:attribute name="source">
+ <xsl:value-of select="$original-title-clean" />
+ </xsl:attribute>
+ <xsl:attribute name="xml:lang">
+ <xsl:value-of select="$title-lang" />
+ </xsl:attribute>
+ <xsl:value-of select="$fixed-title-clean" />
+ </xsl:element>
+ </xsl:template>
+ <xsl:template match="fix" mode="title">
+ <xsl:choose>
+ <!-- Same type and language -->
+ <xsl:when test="@type='title' and @xml:lang=$title-lang">
+ <xsl:call-template name="write-title" />
+ </xsl:when>
+ <xsl:otherwise>
+ <xsl:copy-of select="." />
+ </xsl:otherwise>
+ </xsl:choose>
+ </xsl:template>
+
+ <xsl:template name="string-replace-all">
+ <xsl:param name="text" />
+ <xsl:param name="replace" />
+ <xsl:param name="by" />
+ <xsl:choose>
+ <xsl:when test="$text = '' or $replace = ''or not($replace)" >
+ <xsl:value-of select="$text" />
+ </xsl:when>
+ <xsl:when test="contains($text, $replace)">
+ <xsl:value-of select="substring-before($text,$replace)" />
+ <xsl:value-of select="$by" />
+ <xsl:call-template name="string-replace-all">
+ <xsl:with-param name="text" select="substring-after($text,$replace)" />
+ <xsl:with-param name="replace" select="$replace" />
+ <xsl:with-param name="by" select="$by" />
+ </xsl:call-template>
+ </xsl:when>
+ <xsl:otherwise>
+ <xsl:value-of select="$text" />
+ </xsl:otherwise>
+ </xsl:choose>
+ </xsl:template>
+</xsl:stylesheet>