logo
down
shadow

Compare value of two nodes of same type using xslt


Compare value of two nodes of same type using xslt

By : Wang Han
Date : November 22 2020, 02:59 PM
I wish this helpful for you I am new to XSLT. I need help comparing the value of two nodes in same element of XML. , Here's one way to look at it:
XSLT 2.0
code :
<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:key name="cust-by-pan" match="Customer" use="Pancard" />
<xsl:variable name="duplicates" select="Bank/BankName/Customer[count(key('cust-by-pan', Pancard)) gt 1]" />

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="BankName[not(Customer intersect $duplicates)]"/>
<xsl:template match="Customer[not(. intersect $duplicates)]"/>

</xsl:stylesheet>


Share : facebook icon twitter icon
xslt compare two different nodes and then combine

xslt compare two different nodes and then combine


By : gson
Date : March 29 2020, 07:55 AM
I hope this helps . Try following solution based on Muenchian grouping. The TXLifeRequest are grouped by FundCode and AccountNumber.
It shout work even if there are more than two entries in the group. All Data for a group output (especial ReversalInd) is from the one with the highest TotalAmount. The value of TotalAmount is the difference of the first (highest) TotalAmount and the remaining ones.
code :
xsl:stylesheet version="1.0" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:ns="http://ACORD.org/Standards/Life/2">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>

    <xsl:key name="kTXLifeRequest" match="ns:TXLifeRequest" use="concat(ns:FundCode,'#',ns:AccountNumber)"/>

    <xsl:template match="node()|@*">

        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="/*">
        <xsl:copy>

            <xsl:for-each select=
                 "ns:TXLifeRequest[generate-id() = generate-id(key('kTXLifeRequest',concat(ns:FundCode,'#',ns:AccountNumber))[1])]" >
                <xsl:copy>
                    <xsl:variable name="group"
                                  select="key('kTXLifeRequest',concat(current()/ns:FundCode,'#',current()/ns:AccountNumber))" />
                    <xsl:for-each select= "$group" >
                        <xsl:sort select="ns:TotalAmount" data-type="number" order="descending"/>
                        <xsl:if test="position() = 1">
                            <xsl:apply-templates select="*[local-name() != 'TotalAmount']" />
                            <TotalAmount>
                                <xsl:value-of select="ns:TotalAmount -  sum($group/ns:TotalAmount)+ ns:TotalAmount" />
                            </TotalAmount>
                        </xsl:if>
                    </xsl:for-each>
                </xsl:copy>
            </xsl:for-each>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>
<TXLife xmlns="http://ACORD.org/Standards/Life/2">
  <TXLifeRequest>
    <FundCode>LTRW00</FundCode>
    <AccountNumber>34142</AccountNumber>
    <ReversalInd>Cr</ReversalInd>
    <TotalAmount xmlns="">1250</TotalAmount>
  </TXLifeRequest>
  <TXLifeRequest>
    <FundCode>LUL500</FundCode>
    <AccountNumber>34142</AccountNumber>
    <ReversalInd>Dr</ReversalInd>
    <TotalAmount xmlns="">300</TotalAmount>
  </TXLifeRequest>
</TXLife>
<xsl:stylesheet version="1.0" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:ns="http://ACORD.org/Standards/Life/2">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>

    <xsl:key name="kTXLifeRequest" match="ns:TXLifeRequest" use="concat(ns:FundCode,'#',ns:AccountNumber)" />

    <xsl:template match="node()|@*">

        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="/*">
        <xsl:copy>
            <xsl:for-each select=
                 "ns:TXLifeRequest[generate-id() = generate-id(key('kTXLifeRequest',concat(ns:FundCode,'#',ns:AccountNumber))[1])]" >
                <xsl:copy>
                    <xsl:variable name="group"
                                  select="key('kTXLifeRequest',concat(current()/ns:FundCode,'#',current()/ns:AccountNumber))" />
                    <xsl:for-each select= "$group" >
                        <xsl:sort select="ns:TotalAmount" data-type="number" order="descending"/>
                        <xsl:if test="position() = 1">

                            <xsl:apply-templates select="*[local-name() != 'TotalAmount']" />
                            <TotalAmount>
                                <xsl:value-of select="ns:TotalAmount -  sum($group/ns:TotalAmount)+ ns:TotalAmount" />
                            </TotalAmount>
                        </xsl:if>
                    </xsl:for-each>
                </xsl:copy>

            </xsl:for-each>
            <GrandTotal>
                <xsl:call-template name="totalSum">
                    <xsl:with-param name="groups"
                        select=
                        "ns:TXLifeRequest[generate-id() = generate-id(key('kTXLifeRequest',concat(ns:FundCode,'#',ns:AccountNumber))[1])]" />
            </xsl:call-template>
            </GrandTotal>
        </xsl:copy>
    </xsl:template>

    <xsl:template name="totalSum">
        <xsl:param name="groups" />
        <xsl:param name="gpos" select="1"/>
        <xsl:param name="sum" select="0" />
        <xsl:choose>
            <xsl:when test="$gpos &lt;= count($groups)" >
                    <xsl:variable name="group"
                                  select="key('kTXLifeRequest',concat($groups[$gpos]/ns:FundCode,'#',$groups[$gpos]/ns:AccountNumber))" />
                    <xsl:for-each select= "$group" >
                        <xsl:sort select="ns:TotalAmount" data-type="number" order="descending"/>
                        <xsl:if test="position() = 1">
                            <xsl:variable name="actTotal" select="ns:TotalAmount -  sum($group/ns:TotalAmount)+ ns:TotalAmount" />
                            <xsl:call-template name="totalSum">
                                <xsl:with-param name="groups" select="$groups" />
                                <xsl:with-param name ="gpos" select="$gpos + 1" />
                                <xsl:with-param name="sum" select="$sum + $actTotal" />
                            </xsl:call-template>
                        </xsl:if>
                    </xsl:for-each>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$sum"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

</xsl:stylesheet>
Compare two nodes based on child nodes from different xml using xslt

Compare two nodes based on child nodes from different xml using xslt


By : Drittes Reich
Date : March 29 2020, 07:55 AM
To fix this issue Vivek original answer seems to be OK. You can changed for-each to apply-templates and modified output to something more clear than Y. The program is just comparing all elements column from two XMLs to find if they are identical.
code :
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"/>
<xsl:template match="column">
<xsl:variable name="currantName" select="name"/>
<xsl:variable name="currantValue" select="value"/>
 <xsl:for-each select="document('currentNode.xml')//column"><!--modify your PATH to currentNode.xml-->
 <xsl:if test="(./name/text() = $currantName) and (./value/text() = $currantValue) ">
 YES, found the element
 <xsl:copy-of select="."/>
from  currentNode.xml   in keylist.xml.
 </xsl:if>
 </xsl:for-each>

</xsl:template>

<xsl:template match="row">
<root>
<xsl:apply-templates/>
Search is over. All possible matches should be listed above.
</root>
</xsl:template>
</xsl:stylesheet>
compare xml nodes and select via xslt:for-each

compare xml nodes and select via xslt:for-each


By : 599644
Date : March 29 2020, 07:55 AM
it fixes the issue Reconsider position() which changes context within the , relative to items in the for-each mapping and not original node position in tree.
Similar to @zx485's answer for generating element name, consider this adjusted XSLT script without the identity transform or and using ancestor::* with indentation and strip space for pretty print output.
code :
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>
  <xsl:param name="maxEventNum" select="0"/>

  <xsl:template match="/products/product">
    <products>
      <xsl:apply-templates select="subproducts"/>
    </products>
  </xsl:template>

  <xsl:template match="subproducts">
    <xsl:apply-templates select="subproduct[stock &gt; $maxEventNum]"/>
  </xsl:template>

  <xsl:template match="subproduct">
    <xsl:variable name="curr_color" select="color"/>
    <product>
      <xsl:copy-of select="*"/>
      <modelCode>
        <xsl:value-of select="ancestor::product/ws_code"/>
      </modelCode>
      <xsl:for-each select="ancestor::product/images/img_item[@type_name=$curr_color]">
        <xsl:element name="{concat('image', position())}">
          <xsl:value-of select="."/>
        </xsl:element>
      </xsl:for-each>
    </product>
  </xsl:template>

</xsl:stylesheet>
XSLT: How to compare attributes of different nodes within a for-each loop

XSLT: How to compare attributes of different nodes within a for-each loop


By : Evi
Date : March 29 2020, 07:55 AM
I hope this helps . I don't see much wrong with your code, but in XSLT 2.0 if you want something less verbose you could write
code :
<xsl:value-of select="for $bike in test/bikes/bike 
                      return test/cars/car[@id=$bike/@id]/@color"/>
How to compare the value of two nodes in XSLT

How to compare the value of two nodes in XSLT


By : user337885
Date : March 29 2020, 07:55 AM
With these it helps 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:
Related Posts Related Posts :
  • XPath - Selecting xml nodes with attributes with names that follow pattern
  • XSLT 2.0 Grouping (for WordML) from XML
  • Special Characters will not be removed from XML via XLST
  • WSO2 Identity XACML - Support for Condition Statement?
  • How to combine multiple XML Files with XQuery
  • XSLT select all text and specific node
  • how to interpret the jacoco.xml file?
  • XSLT with different closing tags
  • How to remove an element in Groovy using XmlSlurper?
  • Each result on a new line XQuery
  • Understanding multi-level XML reference using Perl XML Parser?
  • XQuery function to limit the number of records in the output
  • How to write an XQuery flwor expression to calculate the probability between words?
  • XSL translator which removes blank space and convert inches to feet
  • Parse JUnit result XML format (created by 3rd party tool) with Maven
  • Invalid content was found starting with element 'sport'. One of '{spalanie}' is expected
  • How to select particular node from XML
  • FLWOR XQuery to return only unique words combinition, and the number of times the combinition occured, sorted in descend
  • How can I get Chrome to display XML styled with XSL?
  • XML Schema: keyref with optional field
  • Validate and revise XML: schema or code?
  • How to append a node in an XML file with node.js fast without string comparison
  • Searching XML using XPath returning no nodes
  • How to add attribute for child node in XSL?
  • Golang xml doesn't unmarshal
  • Turn XML attribute with 2 or more values into SVG x/y coordinates using XSL
  • Connect to a complex SOAP Web Service
  • solr 5.3.1 verifying that schema.xml is loaded
  • updated xml through excel 2010 vba and calling command for use in windows command line
  • Convert xml date output yyyy-mm-ddThh:mm:ss to dd-mmm-yy using xsl1.0
  • Template returns true() when is told to return false
  • XML node cannot traced
  • is there a difference between <element/> and <element></element> in XML?
  • Extracting attribute value from non context element
  • Can xsd type of 'anyURI' contain spaces?
  • Wiktionary in Structured Format
  • Passing a parameter in XMLA
  • How to add a single element to the end of an xml using xslt?
  • cannot found web.xml error while restarting app in openshift
  • XML Recursive Adding beyond parent node
  • XSLT Identity Transformation while removing all attributes
  • How to reference grouped elements in XSLT 2.0?
  • Unix - Format XML to single line nodes
  • Surround XML elements with CDATA section via XSLT
  • Covert an XML which contains &lt; &gt; in it, using XSLT
  • Using XML prefixes/namespaces to create an Xpath
  • Is there a way to infer an ontology from an XML data file
  • How to restrict the value of an XML element using xsi:type in XSD?
  • Invalid content was found starting with element 'beans:http'
  • Get all nodes as new Elements without tree structure
  • xslt assign a path to a variable and call the variable later on
  • Wrap XML response with JAXB and Jersey
  • How to open an excel file in R where file format and file extension differ?
  • Can we define an xsd with same named element with different attributes, Will this be a valid xsd?
  • Analyze text() node and add new nodes in XSLT
  • VB.NET Query Linq for get all elements node
  • XML: Webservice returns multiple root elements
  • Using XPath to extract elements from a specific location in the document
  • Access Using XSLT to import XML
  • Remove XML node if date condition met
  • shadow
    Privacy Policy - Terms - Contact Us © animezone.co