]> Pierre Choffet | Git repositories - wdef_tools.git/commitdiff
Add replace_id.xslt
authorPierre Choffet <peuc@wanadoo.fr>
Tue, 11 Jan 2022 20:56:33 +0000 (15:56 -0500)
committerPierre Choffet <peuc@wanadoo.fr>
Tue, 11 Jan 2022 21:14:14 +0000 (16:14 -0500)
This stylesheet can be used to replace a wdef:id value by another. It will replace references to the element at the same time.

README
xslts/replace_id.xslt [new file with mode: 0644]

diff --git a/README b/README
index 036803071cad2175f204dff604fde8a9fd9e8879..9006401859a5ed24648fa8b256d7616a8baf0ff1 100644 (file)
--- a/README
+++ b/README
@@ -24,3 +24,6 @@ Description of the provided tools:
 
   - xslts/canonicalize.xslt
     Return a wdef under its normal form.
+
+  - xslts/replace_id.xslt
+    Change the wdef:id of an element and its references.
diff --git a/xslts/replace_id.xslt b/xslts/replace_id.xslt
new file mode 100644 (file)
index 0000000..774422a
--- /dev/null
@@ -0,0 +1,59 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<xsl:stylesheet version="1.0" xmlns:wdef="https://purl.choffet.net/wdef"
+                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
+<!-- replace_id.xslt - Replace wdef id value while keeping references in sync.
+     Copyright (C) 2020, 2021, 2022  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 method="xml" encoding="utf-8" />
+       
+       <xsl:param name="old-id" />
+       <xsl:param name="new-id" />
+       
+       <xsl:template match="@*">
+               <xsl:copy>
+                       <xsl:apply-templates select="@*" />
+               </xsl:copy>
+       </xsl:template>
+       
+       <xsl:template match="node()">
+               <xsl:copy>
+                       <xsl:choose>
+                               <xsl:when test="not(@wdef:id) or @wdef:id != $old-id">
+                                       <xsl:apply-templates select="node()|@*" />
+                               </xsl:when>
+                               <xsl:otherwise>
+                                       <xsl:apply-templates select="@*" />
+                                       <xsl:attribute name="wdef:id">
+                                               <xsl:value-of select="$new-id" />
+                                       </xsl:attribute>
+                                       <xsl:apply-templates select="node()" />
+                               </xsl:otherwise>
+                       </xsl:choose>
+               </xsl:copy>
+       </xsl:template>
+       
+       <xsl:template match="wdef:ref-element">
+               <xsl:copy>
+                       <xsl:choose>
+                               <xsl:when test=". = $old-id">
+                                       <xsl:value-of select="$new-id" />
+                               </xsl:when>
+                               <xsl:otherwise>
+                                       <xsl:apply-templates select="node()|@*" />
+                               </xsl:otherwise>
+                       </xsl:choose>
+               </xsl:copy>
+       </xsl:template>
+</xsl:stylesheet>