1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
| <?xml version="1.0"?>
<!--
Merging two XML files
Version 1.6
LGPL (c) Oliver Becker, 2002-07-05
obecker@informatik.hu-berlin.de
-->
<xslt:transform version="2.0"
xmlns:xslt="http://www.w3.org/1999/XSL/Transform"
xmlns:m="http://informatik.hu-berlin.de/merge"
exclude-result-prefixes="m" >
<xslt:output method="xml" indent="yes" use-character-maps="doctype"/>
<xslt:character-map name="doctype">
<xslt:output-character character=""
string="
<!DOCTYPE" />
<xslt:output-character character="" string=">
" />
<xslt:output-character character="
" string=" " />
</xslt:character-map>
<!-- Normalize the contents of text, comment, and processing-instruction
nodes before comparing?
Default: yes -->
<xslt:param name="normalize" select="'yes'" />
<!-- Don't merge elements with this (qualified) name -->
<xslt:param name="dontmerge" />
<!-- If set to true, text nodes in file1 will be replaced -->
<xslt:param name="replace" select="false()" />
<!-- Variant 1: Source document looks like
<?xml version="1.0"?>
<merge xmlns="http://informatik.hu-berlin.de/merge">
<file1>file1.xml</file1>
<file2>file2.xml</file2>
</merge>
The transformation sheet merges file1.xml and file2.xml.
-->
<xslt:template match="m:merge" >
<xslt:variable name="file1" select="string(m:file1)" />
<xslt:variable name="file2" select="string(m:file2)" />
<xslt:message>
<xslt:text />Merging '<xslt:value-of select="$file1" />
<xslt:text />' and '<xslt:value-of select="$file2"/>'<xslt:text />
</xslt:message>
<xslt:if test="$file1='' or $file2=''">
<xslt:message terminate="yes">
<xslt:text>No files to merge specified</xslt:text>
</xslt:message>
</xslt:if>
<xslt:call-template name="m:merge">
<xslt:with-param name="nodes1" select="document($file1,/*)/node()" />
<xslt:with-param name="nodes2" select="document($file2,/*)/node()" />
</xslt:call-template>
</xslt:template>
<!-- Variant 2:
The transformation sheet merges the source document with the
document provided by the parameter "with".
-->
<xslt:param name="with" />
<xslt:template match="*">
<xslt:if test="string($with)=''">
<xslt:message terminate="yes">
<xslt:text />Merging input with '<xslt:value-of select="$with"/>
<xslt:text>'</xslt:text>
<xslt:text>No input file specified (parameter 'with')</xslt:text>
</xslt:message>
</xslt:if>
<xslt:value-of select="replace(
unparsed-text(document-uri(/)),
'.*?<!DOCTYPE([^>]+)>.*',
'$1', 's')" />
<xslt:comment>
This is an automagically merged file.
No need to edit, it will be reconstructed every time you build webapp.
If you want some changes inside this file, edit the corresponding
source files.
This was merged using '<xslt:value-of select="$with"/>'
</xslt:comment>
<xslt:call-template name="m:merge">
<xslt:with-param name="nodes1" select="/node()" />
<xslt:with-param name="nodes2" select="document($with,/*)/node()" />
</xslt:call-template>
</xslt:template>
<!-- ============================================================== -->
<!-- The "merge" template -->
<xslt:template name="m:merge">
<xslt:param name="nodes1" />
<xslt:param name="nodes2" />
<xslt:choose>
<!-- Is $nodes1 resp. $nodes2 empty? -->
<xslt:when test="count($nodes1)=0">
<xslt:copy-of select="$nodes2" />
</xslt:when>
<xslt:when test="count($nodes2)=0">
<xslt:copy-of select="$nodes1" />
</xslt:when>
<xslt:otherwise>
<!-- Split $nodes1 and $nodes2 -->
<xslt:variable name="first1" select="$nodes1[1]" />
<xslt:variable name="rest1" select="$nodes1[position()!=1]" />
<xslt:variable name="first2" select="$nodes2[1]" />
<xslt:variable name="rest2" select="$nodes2[position()!=1]" />
<!-- Determine type of node $first1 -->
<xslt:variable name="type1">
<xslt:apply-templates mode="m:detect-type" select="$first1" />
</xslt:variable>
<!-- Compare $first1 and $first2 -->
<xslt:variable name="diff-first">
<xslt:call-template name="m:compare-nodes">
<xslt:with-param name="node1" select="$first1" />
<xslt:with-param name="node2" select="$first2" />
</xslt:call-template>
</xslt:variable>
<xslt:choose>
<!-- $first1 != $first2 -->
<xslt:when test="$diff-first='!'">
<!-- Compare $first1 and $rest2 -->
<xslt:variable name="diff-rest">
<xslt:for-each select="$rest2">
<xslt:call-template name="m:compare-nodes">
<xslt:with-param name="node1" select="$first1" />
<xslt:with-param name="node2" select="." />
</xslt:call-template>
</xslt:for-each>
</xslt:variable>
<xslt:choose>
<!-- $first1 is in $rest2 and
$first1 is *not* an empty text node -->
<xslt:when test="contains($diff-rest,'=') and
not($type1='text' and
normalize-space($first1)='')">
<!-- determine position of $first1 in $nodes2
and copy all preceding nodes of $nodes2 -->
<xslt:variable name="pos"
select="string-length(substring-before(
$diff-rest,'=')) + 2" />
<xslt:copy-of
select="$nodes2[position() < $pos]" />
<!-- merge $first1 with its equivalent node -->
<xslt:choose>
<!-- Elements: merge -->
<xslt:when test="$type1='element'">
<xslt:element name="{name($first1)}"
namespace="{namespace-uri($first1)}">
<xslt:copy-of select="$first1/namespace::*" />
<xslt:copy-of select="$first2/namespace::*" />
<xslt:copy-of select="$first1/@*" />
<xslt:call-template name="m:merge">
<xslt:with-param name="nodes1"
select="$first1/node()" />
<xslt:with-param name="nodes2"
select="$nodes2[position()=$pos]/node()" />
</xslt:call-template>
</xslt:element>
</xslt:when>
<!-- Other: copy -->
<xslt:otherwise>
<xslt:copy-of select="$first1" />
</xslt:otherwise>
</xslt:choose>
<!-- Merge $rest1 and rest of $nodes2 -->
<xslt:call-template name="m:merge">
<xslt:with-param name="nodes1" select="$rest1" />
<xslt:with-param name="nodes2"
select="$nodes2[position() > $pos]" />
</xslt:call-template>
</xslt:when>
<!-- $first1 is a text node and replace mode was
activated -->
<xslt:when test="$type1='text' and $replace">
<xslt:call-template name="m:merge">
<xslt:with-param name="nodes1" select="$rest1" />
<xslt:with-param name="nodes2" select="$nodes2" />
</xslt:call-template>
</xslt:when>
<!-- else: $first1 is not in $rest2 or
$first1 is an empty text node -->
<xslt:otherwise>
<xslt:copy-of select="$first1" />
<xslt:call-template name="m:merge">
<xslt:with-param name="nodes1" select="$rest1" />
<xslt:with-param name="nodes2" select="$nodes2" />
</xslt:call-template>
</xslt:otherwise>
</xslt:choose>
</xslt:when>
<!-- else: $first1 = $first2 -->
<xslt:otherwise>
<xslt:choose>
<!-- Elements: merge -->
<xslt:when test="$type1='element'">
<xslt:element name="{name($first1)}"
namespace="{namespace-uri($first1)}">
<xslt:copy-of select="$first1/namespace::*" />
<xslt:copy-of select="$first2/namespace::*" />
<xslt:copy-of select="$first1/@*" />
<xslt:call-template name="m:merge">
<xslt:with-param name="nodes1"
select="$first1/node()" />
<xslt:with-param name="nodes2"
select="$first2/node()" />
</xslt:call-template>
</xslt:element>
</xslt:when>
<!-- Other: copy -->
<xslt:otherwise>
<xslt:copy-of select="$first1" />
</xslt:otherwise>
</xslt:choose>
<!-- Merge $rest1 and $rest2 -->
<xslt:call-template name="m:merge">
<xslt:with-param name="nodes1" select="$rest1" />
<xslt:with-param name="nodes2" select="$rest2" />
</xslt:call-template>
</xslt:otherwise>
</xslt:choose>
</xslt:otherwise>
</xslt:choose>
</xslt:template>
<!-- Comparing single nodes:
if $node1 and $node2 are equivalent then the template creates a
text node "=" otherwise a text node "!" -->
<xslt:template name="m:compare-nodes">
<xslt:param name="node1" />
<xslt:param name="node2" />
<xslt:variable name="type1">
<xslt:apply-templates mode="m:detect-type" select="$node1" />
</xslt:variable>
<xslt:variable name="type2">
<xslt:apply-templates mode="m:detect-type" select="$node2" />
</xslt:variable>
<xslt:choose>
<!-- Are $node1 and $node2 element nodes with the same name? -->
<xslt:when test="$type1='element' and $type2='element' and
local-name($node1)=local-name($node2) and
namespace-uri($node1)=namespace-uri($node2) and
name($node1)!=$dontmerge and name($node2)!=$dontmerge">
<!-- Comparing the attributes -->
<xslt:variable name="diff-att">
<!-- same number ... -->
<xslt:if test="count($node1/@*)!=count($node2/@*)">.</xslt:if>
<!-- ... and same name/content -->
<xslt:for-each select="$node1/@*">
<xslt:if test="not($node2/@*
[local-name()=local-name(current()) and
namespace-uri()=namespace-uri(current()) and
.=current()])">.</xslt:if>
</xslt:for-each>
</xslt:variable>
<xslt:choose>
<xslt:when test="string-length($diff-att)!=0">!</xslt:when>
<xslt:otherwise>=</xslt:otherwise>
</xslt:choose>
</xslt:when>
<!-- Other nodes: test for the same type and content -->
<xslt:when test="$type1!='element' and $type1=$type2 and
name($node1)=name($node2) and
($node1=$node2 or
($normalize='yes' and
normalize-space($node1)=
normalize-space($node2)))">=</xslt:when>
<!-- Otherwise: different node types or different name/content -->
<xslt:otherwise>!</xslt:otherwise>
</xslt:choose>
</xslt:template>
<!-- Type detection, thanks to M. H. Kay -->
<xslt:template match="*" mode="m:detect-type">element</xslt:template>
<xslt:template match="text()" mode="m:detect-type">text</xslt:template>
<xslt:template match="comment()" mode="m:detect-type">comment</xslt:template>
<xslt:template match="processing-instruction()" mode="m:detect-type">pi</xslt:template>
</xslt:transform> |
Partager