I have two source xml files and I need to construct a new xml file which contains elements chosen for one or other of the files depending on whether their ‘name’ is contained in a plain text file.
xml file a:
<data name="name1">
<value>abc1</value>
</data>
<data name="name2">
<value>abc2</value>
</data>
<data name="name3">
<value>abc3</value>
</data>
xml file b:
<data name="name1">
<value>xyz1</value>
</data>
<data name="name2">
<value>xyz2</value>
</data>
<data name="name3">
<value>xyz3</value>
</data>
text file:
name1
name3
desired output:
<data name="name1">
<value>abc1</value>
</data>
<data name="name2">
<value>xyz2</value> <---- note this element is from file 'b'
</data>
<data name="name3">
<value>abc3</value>
</data>
So the elements with names ‘name1’ and ‘name3’ come from ‘xml file a’ because they are listed in the text file, but ‘name2’ comes from ‘xml file b’ because it isn’t.
The actual names aren’t ‘name1’ etc, but arbitrary string identifiers, but they are unique within the files.
Is it possible to do this with XSLT?
This transformation:
when applied on any XML document (not used) and having these two files:
c:/temp/delete/FileA.xml:
c:/temp/delete/FileB.xml:
c:/temp/delete/Names.txt:
produces the wanted, correct result:
Explanation:
Proper use of the standard XSLT functions:
unparsed-text()(2.0 and up only) anddocument()and the standard XPath 2.0 functiontokenize()