Combining xml files -
I have several hundred XML files, which are 2kb each, so they are small, but I want to combine them all. One is required because I need to cross the information in those databases I have.
Each file contains a special case number with other non-critical content.
Is there any way by which I can add all those files to an XML file and for this to exclude all necessary information in addition to the case number: 123456
File
If so, you can use the archive ()
function in XSLT or XQuery, which can point to a directory of XML files.
There are 3 exams in XML files which I put in my 'C: \ test_xml' directory. Two of these are "123456" in their case number and not one of them:
File 1:
& lt ;? XML version = "1.0" encoding = "UTF-8"? & Gt; & Lt; Doc & gt; & Lt; Case & gt; 123456 & lt; / Case & gt; & Lt; Blah & gt; Test 1 file & lt; / Blah & gt; & Lt; / Doc & gt;
File 2:
& lt ;? XML version = "1.0" encoding = "UTF-8"? & Gt; & Lt; Doc & gt; & Lt; Case & gt; Abcdef & lt; / Case & gt; & Lt; Blah & gt; Test 2 file & lt; / Blah & gt; & Lt; / Doc & gt;
File 3:
& lt ;? XML version = "1.0" encoding = "UTF-8"? & Gt; & Lt; Doc & gt; & Lt; Case & gt; 123456 & lt; / Case & gt; & Lt; Blah & gt; Test 3 file & lt; / Blah & gt; & Lt; / Doc & gt; XSLT:
/ P >
& lt ;? XML version = "1.0" encoding = "UTF-8"? & Gt; & Lt; Xsl: stylesheet version = "2.0" xmlns: xsl = "http://www.w3.org/1999/XSL/transform" & gt; & Lt; Xsl: output indent = "yes" /> & Lt; Xsl: strip-space element = "*" /> & Lt; Xsl: template match = "node () | @ *" & gt; & Lt; XSL: Copy & gt; & Lt; Xsl: apply-select template = "node () | @ *" /> & Lt; / XSL: Copy & gt; & Lt; / XSL: Templates & gt; & Lt; Xsl: template match = "/" & gt; & Lt; Collections & gt; & Lt; Xsl: each selection = "archive ('file: ///.c: / test_xml? * .xml') / doctor [case = '123456']" & gt; & Lt; XSL: Copy & gt; & Lt; Xsl: apply-select template = "node () | @ *" /> & Lt; / XSL: Copy & gt; & Lt; / XSL: for-each & gt; & Lt; / Collection & gt; & Lt; / XSL: Templates & gt; & Lt; / XSL: stylesheet & gt;
XQuery:
& lt; Collections & gt; {$ File in the collection ('file: /// C: / test_xml? * .xml') / doc [case = '123456'] Return $ file} & lt; / Collection & gt;
Produces the following output:
Output:
I used Saxon-O (Free Home Edition) to do processing. In addition, XQuery was about 8ms faster than XSLT.
Comments
Post a Comment