xml - XQuery to merge elements and children -
i'm working large volume of xml files have common elements repeated across them. i've been able concatenate them single file , sort using xquery i'm having difficulty taking next step merge elements based on key identifiers. example, have xml file following structure:
<example> <store id="111"> <manager id="123"> <employee> <employeeid>0001001</employeeid> <hiredate value="1-jan-2000"/> <action id="001" type="s"> <details id="a1"> <transactiontype>i</transactiontype> </details> <transactiontype>r</transactiontype> </action> <transactiontype>r</transactiontype> </employee> <transactiontype>r</transactiontype> </manager> <transactiontype>r</transactiontype> </store> <store id="111"> <manager id="123"> <employee> <employeeid>0001001</employeeid> <hiredate value="1-jan-2000"/> <action id="003" name="ecg" type="s"> <details id="b1"> <transactiontype>i</transactiontype> </details> <transactiontype>r</transactiontype> </action> <transactiontype>r</transactiontype> </employee> <transactiontype>r</transactiontype> </manager> <transactiontype>r</transactiontype> </store> <store id="00102"> <manager id="00302"> <employee> <employeeid>0002001</employeeid> <sex value="m"/> <confidential birthdate="1970-07-03"/> <action id="003" name="ecg" type="s"> <details id="c1"> <transactiontype>i</transactiontype> </details> <transactiontype>r</transactiontype> </action> <transactiontype>r</transactiontype> </employee> <transactiontype>r</transactiontype> </manager> <transactiontype>r</transactiontype> </store> </example>
i able merge first 2 main store elements based on attribute values of store id, manager id , element value of employeeid, such resulting xml follows:
<example> <store id="111"> <manager id="123"> <employee> <employeeid>0001001</employeeid> <hiredate value="1-jan-2000"/> <action id="001" type="s"> <details id="a1"> <transactiontype>i</transactiontype> </details> <transactiontype>r</transactiontype> </action> <action id="003" name="ecg" type="s"> <details id="b1"> <transactiontype>i</transactiontype> </details> <transactiontype>r</transactiontype> </action> <transactiontype>r</transactiontype> </employee> <transactiontype>r</transactiontype> </manager> <transactiontype>r</transactiontype> </store> <store id="00102"> <manager id="00302"> <employee> <employeeid>0002001</employeeid> <sex value="m"/> <confidential birthdate="1970-07-03"/> <action id="003" name="ecg" type="s"> <details id="c1"> <transactiontype>i</transactiontype> </details> <transactiontype>r</transactiontype> </action> <transactiontype>r</transactiontype> </employee> <transactiontype>r</transactiontype> </manager> <transactiontype>r</transactiontype> </store> </example>
any suggestions re: xquery approaches achieve result appreciated - or alternative approaches (e.g. xslt?). thanks!
xquery 1.0 lacks grouping capability, makes tricky. if have access xquery 3.0, can make use of new "group by" construct.
similarly in xslt, there's no built-in grouping capability in 1.0, there in 2.0. xslt 2.0 typically do:
<xsl:for-each-group select="store" group-by="concat(@id, '/', manager/@id), '/', manager/employee/employeeid"> <store id="{@id}"> <manager id="{manager/@id}"> <employee> <xsl:variable name="e" select="current-group()/manager/employee"/> <xsl:copy-of select="($e/employeeid)[1]"/> <xsl:copy-of select="($e/hiredate)[1]"/> <xsl:copy-of select="$e/action"/> </employee> </manager> </store> </xsl:for-each-group>
i've made assumptions here: want first hiredate, want actions. you'll have adapt actual requirements.
Comments
Post a Comment