tags:

views:

1956

answers:

3

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.

A: 
//G1Export/compare(AgencyGroup//OrgId, BranchGroup//OrgId)

result = 0

Edited: There were 2 mistakes in the xslt 1. For brnchOrgId you were using AgencyGroup instead of BranchGroup 2. For the compare() you should have =0 and not ='0'

Corrected xslt:

<xsl:template match="/">
     <xsl:element name="PICRESPONSE" namespace="fieldpoint.com/namespaces">
      <xsl:for-each select="//G1Export/AgencyGroup">
       <xsl:if test="count(.) &gt; 0">
        <!--org_id variable-->
        <xsl:variable name="orgId" select="string(/G1Export/AgencyGroup/Agency/OrgId)"/>
        <xsl:element name="EXPORTRESPONSE" namespace="fieldpoint.com/namespaces">; <xsl:for-each select="//G1Export/BranchGroup">
          <xsl:if test="count(.) &gt; 0">
           <xsl:variable name="brnchOrgId" select="string(/G1Export/BranchGroup/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">asda
            <xsl:value-of select="'orgid is same as branchogid'"/>
           </xsl:if>
          </xsl:if>
         </xsl:for-each>
        </xsl:element>
       </xsl:if>
      </xsl:for-each>
     </xsl:element>
    </xsl:template>

Output:

<?xml version="1.0" encoding="UTF-8"?>
<PICRESPONSE xmlns="fieldpoint.com/namespaces">
    <EXPORTRESPONSE>; orgid is same as branchogid</EXPORTRESPONSE>
</PICRESPONSE>

Hope this helps.

Rashmi Pandit
Hi 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
It compares 2 strings (in this case only the values) using the default collation. For additional info see this link: http://www.xsltfunctions.com/xsl/fn_compare.html
Rashmi Pandit
I 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
That 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.
Rashmi Pandit
<xsl:stylesheet exclude-result-prefixes="exslt saxon bpws cis ihmap" version="2.0" xmlns:bpws="http://schemas.xmlsoap.org/ws/2003/03/business-process/" xmlns:cis="http://www.approuter.com/schemas/2003/1/UserCallouts/" xmlns:exslt="http://exslt.org/common" xmlns:ihmap="http://www.approuter.com/xmlns/2002/Mapping" xmlns:saxon="http://saxon.sf.net/" xmlns:xml="http://www.w3.org/XML/1998/namespace"xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
Bijendra Singh
<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="http://www.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
<xsl:element name="EXPORTRESPONSE" namespace="http://www.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
</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
Bijendra Singh
+1  A: 

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

alamar
I 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
Why not just Agency/OrgId = BranchCode/OrgId?Try doing `#<xsl:value-of select="Agency/OrgId" />#` `#<xsl:value-of select="BranchCode/OrgId" />#` to see if there are any subtle differences between them.
alamar
They both are return me the same value.<xsl:value-of select="Agency/OrgId" /> output:123<xsl:value-of select="BranchCode/OrgId" />output:123 As per my sample XML file.
Bijendra Singh
+2  A: 

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">
Welbog
I 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
There 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.
Welbog
I tried this<xsl:value-of select="/G1Export/AgencyGroup/Agency/OrgId = /G1Export/BranchGroup/BranchCode/OrgId"/>but still this is returning me false.
Bijendra Singh