Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 20th, 2011, 09:16 AM
Authorized User
 
Join Date: Dec 2009
Posts: 30
Thanks: 2
Thanked 0 Times in 0 Posts
Send a message via Yahoo to kirangentlebreeze1987
Default compare 2 xml files using xslt

i dono how to figure out this issue
this is my file1.xml
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="employee.xsl"?>
<employeedetails>
<employee>
<empid>1231</empid>
<empname>kiran</empname>
</employee>
<employee>
<empid>1232</empid>
</employee>
</employeedetails>
this is my file2.xml

Code:
<xml>
<employee>
<employeeinf>
<empid>1231</empid>
</employeeinf>
<employeeinf>
<empid>1232</empid>
</employeeinf>
</employee>
</xml>
and this is my xslt
Code:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<table border="1">
<xsl:for-each select="document('emp.xml')/*/employee/employeeinf/empid" >
<xsl:for-each select="document('employee.xml')/*/employee[1]">
<xsl:if test= "string(document('emp.xml')/employee/employeeinf/empid)=string(document('employee.xml')/employee/empid)">
<tr>
<td><xsl:value-of select="string(document('employee.xml')/*/employee/empid)"/></td>
<td><xsl:value-of select="string(document('employee.xml')/*/employee/empname)"/></td>
</tr>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

i am a newbie to xslt
dono how to figure it out 
can you help me in solving this issue
i should get both 1231 and 1232 
but i am getting only 1231 that too 4 times 
dono how to break the loop when the condition is met and go to the next node on next iteration
 
Old February 21st, 2011, 04:33 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

You shouldn't be loading the documents multiple times like that. You should load the document once into a variable and then reuse it.

Code:
<xsl:variable name="employees" select="document('employee.xml')"/>
<xsl:variable name="empIds" select="document('emp.xml')"/>
Also, you can use a key to find information in one file based on the information in another file.

Code:
<xsl:key name="empKey" match="employee" use="empid"/>
Then, loop through each ID in your second document, and look the employee record up in the first document. Note - inside the xsl:for-each the context becomes the node you have selected. There is no need to refer to the $empIds variable again (in fact this will give you incorrect results).

Code:
<xsl:for-each select="$empIds/employee/employeeinf/empid">

<xsl:variable name="employee" select="key('empKey', .)"/>

ID = <xsl:value-of select="$employee/empid"/>
Name = <xsl:value-of select="$employee/empname"/>

</xsl:for-each>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?





Similar Threads
Thread Thread Starter Forum Replies Last Post
compare 2 xml files using xslt kirangentlebreeze1987 XSLT 0 February 20th, 2011 02:35 AM
compare 2 xml files nguna XSLT 8 March 31st, 2009 04:04 AM
XML + XSLT Compare Nodes tommyready XSLT 5 September 5th, 2007 03:18 AM
Compare two xml files using xslt sudha XSLT 0 March 10th, 2006 01:04 AM
Is there any tool available to compare 2 xml files parekh_bhakti ASP.NET 1.0 and 1.1 Professional 1 October 28th, 2005 12:59 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.