Given several XML files that I will call source1.xml
that look like this for example (nodes may vary):
Source files
<root id="s1">
<title>example</title>
<p>Some body text.</p>
<div>
<ul>
<li>Some list item</li>
<li>Some list item</li>
</ul>
<p>qqwerty</p>
<p>asdfg</p>
</div>
</root>
Reference files
And other files that I'll call reference1.xml
that look like this:
<root>
<div>
<ul>
<li>Some list item</li>
<li>Some list item</li>
</ul>
<p>qqwerty</p>
<p>asdfg</p>
</div>
<root>
And reference2.xml
that looks like this:
<root>
<div>
<p>Some body text.<p>
</div>
</root>
There can only be one div
element in each of these files.
Mapping file
To map these together I have a file that looks like this:
<references>
<input src="source1.xml" id="s1">
<reference>T: emp
eference1.xml</reference>
<reference>T: emp
eference2.xml</reference>
</input>
</references>
Expected result
I would like to get the following:
<root id="s1">
<title>example</title>
<link href="reference2.xml"/>
<div>
<link href="reference1.xml"/>
</div>
</root>
So the idea is that I want to see the whole content of the div
node of reference.xml
can be found AS/IS in source.xml
and if so, just insert a reference to it in the source. It must a "dumb replace", if I find it, I replace it in the source, wherever the content might be, no matter of the parent element or whatever.
If only certain elements are found in the source, this is a no go, no link is created. It must be the exact same thing for a link to be created.
How do I do this with XSLT 2.0?
Right now, with the code suggested in the answer I get the following output:
<root id="s1">
<title>example</title>
<link href="reference2.xml"/>
<div>
<link href="reference1.xml"/>
<link href="reference1.xml"/>
<link href="reference1.xml"/>
</div>
</root>
Instead of :
<root id="s1">
<title>example</title>
<link href="reference2.xml"/>
<div>
<link href="reference1.xml"/>
</div>
</root>
See Question&Answers more detail:
os