Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old May 22nd, 2018, 10:26 PM
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
  #2 (permalink)  
Old May 23rd, 2018, 04:15 AM
Friend of Wrox
Points: 6,663, Level: 34
Points: 6,663, Level: 34 Points: 6,663, Level: 34 Points: 6,663, Level: 34
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,239
Thanks: 0
Thanked 244 Times in 243 Posts
Default

As you mention Java, can we assume that you can use Saxon 9 and XSLT 2 or 3 for the XSLT?
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
Reply With Quote
  #3 (permalink)  
Old May 23rd, 2018, 04:25 AM
Friend of Wrox
Points: 6,663, Level: 34
Points: 6,663, Level: 34 Points: 6,663, Level: 34 Points: 6,663, Level: 34
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,239
Thanks: 0
Thanked 244 Times in 243 Posts
Default

Also, is the number of nested category levels static? Can you explain in words how the merging of the data elements like content or products is supposed to happen, e.g. if you simply want to use the data from both files? It is hard to tell from the unindented code samples how the input and target structure is supposed to look like and where the final data comes from.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
Reply With Quote
  #4 (permalink)  
Old May 23rd, 2018, 10:30 AM
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
Default

Hi Martin,

Thanks for your reply.
I am using Xalan Java 2.7 & XSL Processor Version 1.1.
Yes the category level will be same every time in both xmls. The below are 4 level categories
root > master > sec > ch.
And the products comes under "ch" category. Category tree path with products looks as [root > master > sec > ch > products > p1,p2,..etc]. Here in both xmls the number of products may vary and may also contains same products. In both xmls the id's of each categories may or may not vary. I think we have to build a category tree path first and merge the products that comes under same category tree path after comparing both xmls. No duplicate products following duplicate categories should contain after merge.
For eg..,

root_id > master_id > sec_id > ch_id > products > (p1_id,p2_id...pn)
xml1:
root_123 > master_234 > sec_456 > ch_567 > products > p_1,p_2
root_123 > master_234 > sec_356 > ch_567 > products > p_5,p_6
root_234 > master_345 > sec_456 > ch_981 > products > p_3,p_4

xml2:
root_123 > master_234 > sec_456 > ch_567 > products > p_1,p_2,p_8
root_123 > master_234 > sec_356 > ch_569 > products > p_5,p_6,p_7
root_234 > master_345 > sec_456 > ch_981 > products > p_3,p_9

mergedxml:
1) root_123 > master_234 > sec_456 > ch_567 > products > p_1,p_2,p_8
2) root_123 > master_234 > sec_356 > ch_567 > products > p_5,p_6
3) root_123 > master_234 > sec_356 > ch_569> products > p_5,p_6,p_7
4) root_234 > master_345 > sec_456 > ch_981 > products > p_3,p_9,p_4

After merge
the line 1) category path gets more products
the line 2) gets two more "ch" categories under sec_356
the line 3) gets additional product p_7 from second xml
the line 4) gets additonal product p_9 from second xml

Please let me know if this example is good enough to understand the requirement.

We can ignore merging of <content> if the same category id exists in the both xmls. TIA!

Last edited by NagKrv; May 24th, 2018 at 12:08 AM.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
xslt footnotes from 2 xmls sunnykeerthi XSLT 0 April 12th, 2013 10:23 AM
XSLT from two XMLs sunnykeerthi XSLT 1 April 10th, 2013 01:14 PM
Change XMLs chameleon XSLT 2 November 12th, 2009 10:53 AM
Grouping xmls sac XSLT 4 July 29th, 2008 04:16 AM
Multiple xmls and xsls pasupathy.b XSLT 6 June 4th, 2007 01:07 PM



All times are GMT -4. The time now is 12:12 PM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.