Jump to content

how to compare 2 xml files using xslt


kirangentlebreeze1987

Recommended Posts

Hello experts, i am kinda new to xslt and stuff, i wanted to know how to compare two xml files using xsltthis is my file1.xml<xml><employeedetails><employee><empname>kiran</empname><empid>1231</empname><emploc>chennai</emploc></employee><employee><empname>kumar</empname><empid>1232</empname><emploc>chennai</emploc></employee></employeedetails></xml>this is my file2.xml<xml><employeedetails><employee><empid>1231</empid><empid>1232</empid><empid>1233</empid></employee></employeedetails></xml>can i use like this <xsl:if test="document('file2.xml')/employeedetails/employee/empid=document(file1.xml)/employeedetails/employee/empid">is this the correct approach i am new to this xslt stuff any help is very much appreciatedthank you

Link to comment
Share on other sites

Rename "xml" to something else. "xml" is a reserved prefix of node names in XML.As for the comparrison... if the idea is to compare the empid to the empid in the other document, then yes, you're on the right track. You just need to account for the root element (currently called "xml"), and enclose the two subqueries in string() to separate them. Oh, and file1.xml needs to be with quotes... so:

<xsl:if test="string(document('file2.xml')/*/employeedetails/employee/empid) = string(document('file1.xml')/*/employeedetails/employee/empid)">

Link to comment
Share on other sites


Rename "xml" to something else. "xml" is a reserved prefix of node names in XML.As for the comparrison... if the idea is to compare the empid to the empid in the other document, then yes, you're on the right track. You just need to account for the root element (currently called "xml"), and enclose the two subqueries in string() to separate them. Oh, and file1.xml needs to be with quotes... so:
<xsl:if test="string(document('file2.xml')/*/employeedetails/employee/empid) = string(document('file1.xml')/*/employeedetails/employee/empid)">

<?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="employeedetails/employee"><xsl:if test="string(document('employee.xml')/*/employeedetails/employee/empid)=string(document('emp.xml')/*/employee/empid)"><tr><td><xsl:value-of select="empid"/></td><td><xsl:value-of select="empname"/></td></tr></xsl:if></xsl:for-each></table></body></html></xsl:template></xsl:stylesheet>

this is my xsl sheet how can i match the template of anothet xml i.e file2.xml..can i use anothet xsl:for-each inside the for-each which i have usedsorry if the question is irrelavant i am new to this xsl stuff

Link to comment
Share on other sites


Rename "xml" to something else. "xml" is a reserved prefix of node names in XML.As for the comparrison... if the idea is to compare the empid to the empid in the other document, then yes, you're on the right track. You just need to account for the root element (currently called "xml"), and enclose the two subqueries in string() to separate them. Oh, and file1.xml needs to be with quotes... so:
<xsl:if test="string(document('file2.xml')/*/employeedetails/employee/empid) = string(document('file1.xml')/*/employeedetails/employee/empid)">

<?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="employeedetails/employee"><xsl:for-each select="string(document('emp.xml')/*/employee/empid)"><xsl:if test="string(document('employee.xml')/*/employeedetails/employee/empid)=string(document('emp.xml')/*/employee/empid)"><tr><td><xsl:value-of select="empid"/></td><td><xsl:value-of select="empname"/></td></tr></xsl:if></xsl:for-each></xsl:for-each></table></body></html></xsl:template>

this is my xsl sheet but i am getting an error "Expression does not return a DOM node. -->string(document('emp.xml')/*/employee/empid)<--"

Link to comment
Share on other sites

This can only mean that "emp.xml" is not found. It should be in the same folder as the XSLT file.No, no... the reason is the for-each... sorry, my bad.

<xsl:for-each select="string(document('emp.xml')/*/employee/empid)">

You can't loop over a string. To loop over all "empid" elements matching this expression, remove the string(), like:

<xsl:for-each select="document('emp.xml')/*/employee/empid">

Link to comment
Share on other sites

This can only mean that "emp.xml" is not found. It should be in the same folder as the XSLT file.No, no... the reason is the for-each... sorry, my bad.
<xsl:for-each select="string(document('emp.xml')/*/employee/empid)">

You can't loop over a string. To loop over all "empid" elements matching this expression, remove the string(), like:

<xsl:for-each select="document('emp.xml')/*/employee/empid">

this is my modified xslt but i am getting only 1 result
<?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 xsltdono how to figure it out can you help me in solving this issuei 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

Link to comment
Share on other sites

This can only mean that "emp.xml" is not found. It should be in the same folder as the XSLT file.No, no... the reason is the for-each... sorry, my bad.
<xsl:for-each select="string(document('emp.xml')/*/employee/empid)">

You can't loop over a string. To loop over all "empid" elements matching this expression, remove the string(), like:

<xsl:for-each select="document('emp.xml')/*/employee/empid">

sorry frd i am getting unexpected results dont know how to solve this
Link to comment
Share on other sites

With

<xsl:for-each select="document('employee.xml')/*/employee[1]">

you're targeting the first "employee" element within the root element of the "employees.xml" document. The first element... obviously... is one element. If the idea is to loop over all empid elements in it, this needs to be explicit, i.e.

<xsl:for-each select="document('employee.xml')/*/employee[1]/empid">

(loops over all empid elements within the first "employee" element within the root element of the "employee.xml" document)

Link to comment
Share on other sites

With
<xsl:for-each select="document('employee.xml')/*/employee[1]">

you're targeting the first "employee" element within the root element of the "employees.xml" document. The first element... obviously... is one element. If the idea is to loop over all empid elements in it, this needs to be explicit, i.e.

<xsl:for-each select="document('employee.xml')/*/employee[1]/empid">

(loops over all empid elements within the first "employee" element within the root element of the "employee.xml" document)

<?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[1]/empid" ><xsl:for-each select="document('employee.xml')/*/employee[1]/empid"><xsl:if test= "string(document('emp.xml')/employee/employeeinf/empid)=string(document('employee.xml')/employee/empid)"><tr><td><xsl:value-of select="document('employee.xml')/*/employee/empid"/></td><td><xsl:value-of select="document('employee.xml')/*/employee/empname"/></td></tr></xsl:if></xsl:for-each></xsl:for-each></table></body></html></xsl:template></xsl:stylesheet>

sorry i am getting data of the first recordnot getting the data of next record

Link to comment
Share on other sites

Once you're in a for-each, use paths relative to it. By using absolute paths, you're restarting the count. So... even though you're getting only the first record, I'm assuming it's repeated N times, right?What's the big idea actually? You want to print all "empid"s and "empname" where the "empid" is present in both files? You should probably do that on the first for-each level, like... wait just a sec... could you give me some of the new sample files? Which file is which? You've altered them a little too much from the start (I've lost track of the changes).

Link to comment
Share on other sites

Once you're in a for-each, use paths relative to it. By using absolute paths, you're restarting the count. So... even though you're getting only the first record, I'm assuming it's repeated N times, right?What's the big idea actually? You want to print all "empid"s and "empname" where the "empid" is present in both files? You should probably do that on the first for-each level, like... wait just a sec... could you give me some of the new sample files? Which file is which? You've altered them a little too much from the start (I've lost track of the changes).
this is my file1.xml
<xml><employee><employeeinf><empid>1231</empid></employeeinf><employeeinf><empid>1232</empid></employeeinf></employee></xml>

this is my file2.xml

<?xml version="1.0" encoding="ISO-8859-1"?><?xml-stylesheet type="text/xsl" href="emp.xsl"?><employeedetails><employee><empid>1231</empid><empname>kiran</empname></employee><employee><empid>1232</empid><empname>kiran</empname></employee></employeedetails>

this is my xsl

<?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[1]/empid[1]" ><xsl:for-each select="document('employee.xml')/*/employee[1]/empid"><xsl:if test= "string(document('emp.xml')/employee/employeeinf/empid)=string(document('employee.xml')/employee/empid)"><tr><td><xsl:value-of select="document('employee.xml')/*/employee/empid"/></td><td><xsl:value-of select="document('employee.xml')/*/employee/empname"/></td></tr></xsl:if></xsl:for-each></xsl:for-each></table></body></html></xsl:template></xsl:stylesheet>

help me i am a newbie

Link to comment
Share on other sites

Forget about all that "string" stuff, it's useless.I think you need something like this:

<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">	<xsl:template match="/">		<html>			<body>				<table border="1">					<xsl:variable name="root" select="/"/>					<xsl:for-each select="document('emp.xml')//employee/employeeinf/empid">						<xsl:variable name="currentempid" select="."/>						<xsl:if test="$currentempid = $root/employeedetails/employee/empid">							<tr>								<td>									<xsl:value-of select="$root/employeedetails/employee[empid = $currentempid]/empid" />								</td>								<td>									<xsl:value-of select="$root/employeedetails/employee[empid = $currentempid]/empname" />								</td>							</tr>						</xsl:if>					</xsl:for-each>				</table>			</body>		</html>	</xsl:template></xsl:stylesheet>

Regards, John Bampton.

Link to comment
Share on other sites

Forget about all that "string" stuff, it's useless.I think you need something like this:
<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">	<xsl:template match="/">		<html>			<body>				<table border="1">					<xsl:variable name="root" select="/"/>					<xsl:for-each select="document('emp.xml')//employee/employeeinf/empid">						<xsl:variable name="currentempid" select="."/>						<xsl:if test="$currentempid = $root/employeedetails/employee/empid">							<tr>								<td>									<xsl:value-of select="$root/employeedetails/employee[empid = $currentempid]/empid" />								</td>								<td>									<xsl:value-of select="$root/employeedetails/employee[empid = $currentempid]/empname" />								</td>							</tr>						</xsl:if>					</xsl:for-each>				</table>			</body>		</html>	</xsl:template></xsl:stylesheet>

Regards, John Bampton.

just now i checked your replyi am a beginner thanks a lot for helping me
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...