View Single Post
  #1 (permalink)  
Old May 22nd, 2018, 10:26 PM
NagKrv NagKrv is offline
Registered User
Points: 8, Level: 1
Points: 8, Level: 1 Points: 8, Level: 1 Points: 8, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2018
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Post Merge two xmls into third one using xsl

Hi,

I have two xml files.
xml1:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<category type="root" id="12345">
<content>
<title>Root Category</title>
<key-visuals>
<info-link>https;///</info-link>
</key-visuals>
<key-visuals>
<info-link>https;///</info-link>
</key-visuals>
<thumb-nails>
<image-url>https://saldf.png</image-url>
</thumb-nails>
</content>
<category type="master" id="67890">
<content>
<title>Category</title>
<key-visuals>
<info-link>https;///</info-link>
</key-visuals>
<key-visuals>
<info-link>https;///</info-link>
</key-visuals>
<thumb-nails>
<image-url>https://saldf.png</image-url>
</thumb-nails>
</content>
<category type="sec" id="22222">
<content>
<title>Category</title>
<key-visuals>
<info-link>https;///</info-link>
</key-visuals>
<key-visuals>
<info-link>https;///</info-link>
</key-visuals>
<thumb-nails>
<image-url>https://saldf.png</image-url>
</thumb-nails>
</content>
<category type="ch" id="333333">
<products>
<product type="prod" id="25"><product-links><status>aa</status>
<status>bb</status>
</product-links></product>
<product type="prod" id="26"><product-links><status>aa</status>
<status>bb</status>
</product-links></product>
</products>
</category>
<category type="ch" id="444444">
<products>
<product type="prod" id="27"><product-links><status>aa</status>
<status>bb</status>
</product-links></product>
<product type="prod" id="28"><product-links><status>aa</status>
<status>bb</status>
</product-links></product>
</products>
</category>

</category>
</category>
</category>
</catalog>
xml2:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<catalog>

<category type="root" id="12345">
<content>
<title>Root Category</title>
<key-visuals>
<info-link>https;///</info-link>
</key-visuals>
<key-visuals>
<info-link>https;///</info-link>
</key-visuals>
<thumb-nails>
<image-url>https://saldf.png</image-url>
</thumb-nails>
</content>
<category type="master" id="67890">
<content>
<title>master Category</title>
<key-visuals>
<info-link>https;///</info-link>
</key-visuals>
<key-visuals>
<info-link>https;///</info-link>
</key-visuals>
<thumb-nails>
<image-url>https://saldf.png</image-url>
</thumb-nails>
</content>
<category type="sec" id="22222">
<content>
<title>master Category</title>
<key-visuals>
<info-link>https;///</info-link>
</key-visuals>
<key-visuals>
<info-link>https;///</info-link>
</key-visuals>
<thumb-nails>
<image-url>https://saldf.png</image-url>
</thumb-nails>
</content>
<category type="ch" id="333333">
<products>
<product type="prod" id="29"><product-links><status>aa</status>
<status>bb</status>
</product-links></product>
<product type="prod" id="25"><product-links><status>aa</status>
<status>bb</status>
</product-links></product>

<product type="prod" id="27"><product-links><status>aa</status>
<status>bb</status>
</product-links></product>
</products>
</category>
<category type="ch" id="444444">
<products>
<product type="prod" id="40"><product-links><status>aa</status>
<status>bb</status>
</product-links></product>
<product type="prod" id="27"><product-links><status>aa</status>
<status>bb</status>
</product-links></product>
</products>
</category>
</category>
</category>
<category type="master" id="111111">
<content>
<title>master Category</title>
<key-visuals>
<info-link>https;///</info-link>
</key-visuals>
<key-visuals>
<info-link>https;///</info-link>
</key-visuals>
<thumb-nails>
<image-url>https://saldf.png</image-url>
</thumb-nails>
</content>
<category type="sec" id="22222">
<content>
<title>master Category</title>
<key-visuals>
<info-link>https;///</info-link>
</key-visuals>
<key-visuals>
<info-link>https;///</info-link>
</key-visuals>
<thumb-nails>
<image-url>https://saldf.png</image-url>
</thumb-nails>
</content>
<category type="ch" id="333003">
<products>
<product type="prod" id="36"><product-links><status>aa</status>
<status>bb</status>
</product-links></product>
<product type="prod" id="39"><product-links><status>aa</status>
<status>bb</status>
</product-links></product>
</products>
</category>
<category type="ch" id="444444">
<products>
<product type="prod" id="25"><product-links><status>aa</status>
<status>bb</status>
</product-links></product>
<product type="prod" id="26"><product-links><status>aa</status>
<status>bb</status>
</product-links></product>
</products>
</category>
</category>
</category>
</category>
</catalog>
Expected Output:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<category type="root" id="12345">
<content>
<title>Root Category</title>
<key-visuals>
<info-link>https;///</info-link>
</key-visuals>
<key-visuals>
<info-link>https;///</info-link>
</key-visuals>
<thumb-nails>
<image-url>https://saldf.png</image-url>
</thumb-nails>
</content>
<category type="master" id="67890">
<content>
<title>master Category</title>
<key-visuals>
<info-link>https;///</info-link>
</key-visuals>
<key-visuals>
<info-link>https;///</info-link>
</key-visuals>
<thumb-nails>
<image-url>https://saldf.png</image-url>
</thumb-nails>
</content>
<category type="sec" id="22222">
<content>
<title>master Category</title>
<key-visuals>
<info-link>https;///</info-link>
</key-visuals>
<key-visuals>
<info-link>https;///</info-link>
</key-visuals>
<thumb-nails>
<image-url>https://saldf.png</image-url>
</thumb-nails>
</content>
<category type="ch" id="333333">
<products>
<product type="prod" id="25"><product-links><status>aa</status>
<status>bb</status>
</product-links></product>
<product type="prod" id="26"><product-links><status>aa</status>
<status>bb</status>
</product-links></product>
<product type="prod" id="29"><product-links><status>aa</status>
<status>bb</status>
</product-links></product>
<product type="prod" id="27"><product-links><status>aa</status>
<status>bb</status>
</product-links></product>
</products>
</category>
<category type="ch" id="444444">
<products>
<product type="prod" id="27"><product-links><status>aa</status>
<status>bb</status>
</product-links></product>
<product type="prod" id="28"><product-links><status>aa</status>
<status>bb</status>
</product-links></product>
<product type="prod" id="40"><product-links><status>aa</status>
<status>bb</status>
</product-links></product>
</products>
</category>
</category>
</category>
<category type="master" id="111111">
<content>
<title>master Category</title>
<key-visuals>
<info-link>https;///</info-link>
</key-visuals>
<key-visuals>
<info-link>https;///</info-link>
</key-visuals>
<thumb-nails>
<image-url>https://saldf.png</image-url>
</thumb-nails>
</content>
<category type="sec" id="22222">
<content>
<title>master Category</title>
<key-visuals>
<info-link>https;///</info-link>
</key-visuals>
<key-visuals>
<info-link>https;///</info-link>
</key-visuals>
<thumb-nails>
<image-url>https://saldf.png</image-url>
</thumb-nails>
</content>
<category type="ch" id="333003">
<products>
<product type="prod" id="36"><product-links><status>aa</status>
<status>bb</status>
</product-links></product>
<product type="prod" id="39"><product-links><status>aa</status>
<status>bb</status>
</product-links></product>
</products>
</category>
<category type="ch" id="444444">
<products>
<product type="prod" id="25"><product-links><status>aa</status>
<status>bb</status>
</product-links></product>
<product type="prod" id="26"><product-links><status>aa</status>
<status>bb</status>
</product-links></product>
</products>
</category>
</category>
</category>
</category>
</catalog>
I want merge all categories, subcategories and products that comes under same category tree path by comparing category ids. Could any one please provide xsl for this? I am loading first file from java and second file from style sheet.

This is my first code in xsl and tried little hard to make it work for one element with hard coded values, I mean the all the categories/subcategories/products have been grouped and merged into one file properly. Other solutions which I tried to make call template and pass the id's of categories dynamically to the sec,ch templates didn't actually worked. Below I am pasting the code with static values that worked for one element. Could you please help me out to iterate all the categories having same id and merged products properly?

Code:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output indent="yes" method="xml"/>
<xsl:param name="doc-file" select"xml1.xml"/>
<xsl:param name="updates" select="document($doc-file)"/>
<xsl:variable name="updatemastercat" select="$updates/catalog/category"/>
<xsl:variable name="updateseccategories" select="$updates/catalog/category/category"/>
<xsl:variable name="updatechcategories" select="$updates/catalog/category/category/category"/>
<xsl:template match="*|@*">
	<xsl:copy><xsl:copy-of select = "@*"/></xsl:apply-templates/>
</xsl:template>

<xsl:template match="catalog/category">
	<xsl:variable name="allMasterCat" select="descendant::*"/>
	<xsl:copy>
		<xsl:apply-templates select="@*"/>
			<xsl:apply-templates/>
	<xsl:for-each select="$updatemastercat/category">
		<xsl:variable name="masterid" select="@id"/>
		<xsl:if test="not ($allMasterCat[@id=$masterid]/@id=$masterid)">
			<xsl:element name="category">
				<xsl:apply-templates select="@*"/>
				<xsl:apply-templates/>
			<xsl:element>
			</xsl:if>
		</xsl:for-each>
	</xsl:copy>
</xsl:template>

<xsl:template match="catalog/category/category[@id='67890']" name="sec">
	<xsl:variable name="allSecCat" select="descendant::*"/>
	<xsl:copy>
		<xsl:apply-templates select="@*"/>
			<xsl:apply-templates/>
	<xsl:for-each select="$updateseccategories/category[@id='67890']/category">
		<xsl:variable name="secid" select="@id"/>
		<xsl:if test="not ($allSecCat[@id=$secid]/@id=$secid)">
			<xsl:element name="category">
				<xsl:apply-templates select="@*"/>
				<xsl:apply-templates/>
			<xsl:element>
			</xsl:if>
		</xsl:for-each>
	</xsl:copy>
</xsl:template>
<!--update ch category-->
<xsl:template match="catalog/category/category[@id='67890']/category[@id='22222']" name="ch">
	<xsl:variable name="allChCat" select="descendant::*"/>
	<xsl:copy>
		<xsl:apply-templates select="@*"/>
			<xsl:apply-templates/>
	<xsl:for-each select="$updatechcategories/category[@id='67890']/category[@id='22222']/category">
		<xsl:variable name="chid" select="@id"/>
		<xsl:if test="not ($allChCat[@id=$chid]/@id=$chid)">
			<xsl:element name="category">
				<xsl:apply-templates select="@*"/>
				<xsl:apply-templates/>
			<xsl:element>
			</xsl:if>
		</xsl:for-each>
	</xsl:copy>
</xsl:template>

<!---update prod-->
<xsl:template match="catalog/category/category[@id='67890']/category[@id='22222']/category[@id='333333']/products" name="prod">
	<xsl:variable name="allCatProd" select="descendant::*"/>
	<xsl:copy>
		<xsl:apply-templates select="@*"/>
			<xsl:apply-templates/>
	<xsl:for-each select="$updatechcategories/category[@id='67890']/category[@id='22222']/category[@id='333333']/products/product">
		<xsl:variable name="prodid" select="@id"/>
		<xsl:if test="not ($allCatProd[@id=$prodid]/@id=$prodid)">
			<xsl:element name="category">
				<xsl:apply-templates select="@*"/>
				<xsl:apply-templates/>
			<xsl:element>
			</xsl:if>
		</xsl:for-each>
	</xsl:copy>
</xsl:template>
</xsl:stylesheet>

TIA!

Last edited by NagKrv; May 23rd, 2018 at 12:56 AM. Reason: Added xsl code with static values
Reply With Quote