How to compare the value of two nodes in XSLT
Solution 1
I don't know the context under which you need to compare these values, but the =
operator is what you are looking for. This will compare them, but probably isn't the context you need:
<xsl:if
test="/G1Export/AgencyGroup/Agency/OrgId = /G1Export/BranchGroup/BranchCode/OrgId">
Solution 2
Why not do AgencyGroup/Agency/OrgId = BranchGroup/BranchCode/OrgId
?
For extra anal, AgencyGroup/Agency/OrgId/text() = BranchGroup/BranchCode/OrgId/text()
.
If you need difference, consider AgencyGroup/Agency/OrgId - BranchGroup/BranchCode/OrgId
Bijendra Singh
Updated on July 12, 2020Comments
-
Bijendra Singh almost 4 years
I am new to XSLT. I need help comparing the value of two nodes' values in XML.
My sample XML:
<?xml version="1.0" encoding="utf-8"?> <G1Export xmlns=""> <AgencyGroup xmlns=""> <Agency xmlns=""> <RecordType xmlns="">RecordType</RecordType> <OrgId xmlns="">123</OrgId> </Agency> </AgencyGroup> <BranchGroup xmlns=""> <BranchCode xmlns=""> <OrgId xmlns="">123</OrgId> </BranchCode> </BranchGroup> </G1Export>
In the above XML file I need to compare the values of the
OrgId
node under the<AgencyGroup>
node to the one under the<BranchGroup>
node.I tried to used the
compare()
method, but it gives me the reult of 1. The actual result must be 0 (for equal). I am using XSLT 2. -
Bijendra Singh about 15 yearsHi Rashmi, U mean to say that I shoul use comapare method with Xpath of both the node. Does the compare() method compare only the value of the node plus the position of these node in the xml file?
-
Bijendra Singh about 15 yearsI tried <xsl:variable name="orgId" select="string(Agency/OrgId)" /> <xsl:variable name="brnchOrgId" select="string(BranchCode/OrgId)" /> match:<xsl:value-of select="$brnchOrgId=$orgId"/> this is giving false. In my xml file value of Agency/OrgId node is 123 and BranchCode/OrgId is also 123.
-
Welbog about 15 yearsThere is no context in which Agency/OrgId and BranchCode/OrgId will both return actual values, because at that level they do not have a common parent. Use the full paths I have in my example. and tell me if it works.
-
SO User about 15 yearsIt compares 2 strings (in this case only the values) using the default collation. For additional info see this link: xsltfunctions.com/xsl/fn_compare.html
-
Bijendra Singh about 15 yearsI tried <xsl:variable name="orgId" select="string(Agency/OrgId)" /> <xsl:variable name="brnchOrgId" select="string(BranchCode/OrgId)" /> match:<xsl:value-of select="$brnchOrgId=$orgId"/> this is giving false. In my xml file value of Agency/OrgId node is 123 and BranchCode/OrgId is also 123.
-
Bijendra Singh about 15 yearsI tried this <xsl:value-of select="/G1Export/AgencyGroup/Agency/OrgId = /G1Export/BranchGroup/BranchCode/OrgId"/> but still this is returning me false.
-
SO User about 15 yearsThat is because your xpaths are incorrect. From a single location you cannot use "Agency/OrgId" and "BranchCode/OrgId". Try with "AgencyGroup/Agency/OrgId" and "AgencyGroup/BranchCode/OrgId". It depends on your current location. It will help if you post sample of your xslt or atleast let me know in which template you are declaring these variables.
-
Bijendra Singh about 15 years<xsl:stylesheet exclude-result-prefixes="exslt saxon bpws cis ihmap" version="2.0" xmlns:bpws="schemas.xmlsoap.org/ws/2003/03/business-process" xmlns:cis="approuter.com/schemas/2003/1/UserCallouts" xmlns:exslt="exslt.org/common" xmlns:ihmap="approuter.com/xmlns/2002/Mapping" xmlns:saxon="saxon.sf.net" xmlns:xml="w3.org/XML/1998/namespace" xmlns:xsd="w3.org/2001/XMLSchema" xmlns:xsi="w3.org/2001/XMLSchema-instance" xmlns:xsl="w3.org/1999/XSL/Transform">
-
Bijendra Singh about 15 years<xsl:output encoding="UTF-8" indent="yes" method="xml" /> <xsl:variable name="srcDoc1" select="bpws:getVariableData('g1Export')" /> <xsl:template match="/"> <xsl:element name="PICRESPONSE" namespace="fieldpoint.com/namespaces"> <xsl:for-each select="$srcDoc1/G1Export/AgencyGroup"> <xsl:if test="count(.) > 0"> <!--org_id variable--> <xsl:variable name="orgId" select="string(/G1Export/AgencyGroup/Agency/OrgId)" />
-
Bijendra Singh about 15 years<xsl:element name="EXPORTRESPONSE" namespace="fieldpoint.com/namespaces"> <xsl:for-each select="$srcDoc1/G1Export/BranchGroup"> <xsl:if test="count(.) > 0"> <xsl:variable name="brnchOrgId" select="string(/G1Export/AgencyGroup/BranchCode/OrgId)" /> <!--Put the Branch information inside the current agency node only if branch belong to current Agency--> <xsl:if test="compare($brnchOrgId,$orgId)='0'"> <xsl:value-of select="'orgid is same as branchogid'"/>
-
Bijendra Singh about 15 years</xsl:if> </xsl:if> </xsl:for-each> </xsl:element> </xsl:if> </xsl:for-each> </xsl:element> </xsl:template> </xsl:stylesheet> This my xslt file which will be processing my xml file