Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7828965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T10:33:50+00:00 2026-06-02T10:33:50+00:00

Firstly, I suck at XSLT. Secondly, I know there are several articles on how

  • 0

Firstly, I suck at XSLT. Secondly, I know there are several articles on how to perform merges with XSLT, but I didn’t find anything on my particular challenge.

I have 2 XML files. One is new Customer Information and the other is the Current/Previous information below. I need the resultant XML to merge all of the Customer/Addresses and add attributes (NoChange, Updated, Deleted, New) to the attribute of the final XML:


Input 1

Current Customer Information.

<Customer>
 <CustId>1</CustId>
 <CustName>Acme</CustName>
 <Addresses>
  <Address>
   <AddressesId>1</AddressesId>
   <Street>123 Main</Street>
  </Address>
  <Address>
   <AddressesId>2</AddressesId>
   <Street>345 Main</Street</Street>
  </Address>
  <Address>
   <AddressesId>4</AddressesId>
   <Street>888 Goner St.</Street>
  </Address>
 </Addresses>
</Customer>

Input 2

Updates Information.

<Customer>
 <CustId>1</CustId>
 <CustName>Acme</CustName>
 <Addresses>
  <Address>
   <AddressesId>2</AddressesId>
   <Street>999 Updated St.</Street>
  </Address>
  <Address>
   <AddressesId>3</AddressesId>
   <Street>3999 New St.</Street>
  </Address>
 </Addresses>
</Customer>

Result

<Customer>
 <CustId>1</CustId>
 <CustName>Acme</CustName>
 <Addresses>
  <Address>
   <Address status="NoChange">
   <AddressesId>1</AddressesId>
   <Street>123 Main</Street>
  </Address>
  <Address>
   <Address status="Updated">
   <AddressesId>2</AddressesId>
   <Street>999 Updated St.</Street>
  </Address>
   <Address status="New">
   <AddressesId>3</AddressesId>
   <Street>3999 New St.</Street>
  </Address>
  <Address status="Deleted">
   <AddressesId>4</AddressesId>
   <Street>888 Goner St.</Street>
  </Address>
 </Addresses>
</Customer>

How can I do the merge I want?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T10:33:55+00:00Added an answer on June 2, 2026 at 10:33 am

    I. This XSLT 1.0 transformation (a corresponding XSLT 2.0 solution is shorter and easier):

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:key name="kAddrById" match="Address"
      use="concat(../../CustId, '+', ../../CustName,
                  '+', AddressesId)"/>
    
     <xsl:key name="kNodeByGenId" match="node()"
              use="generate-id()"/>
    
     <xsl:param name="pUpdatesPath" select=
      "'file:///c:/temp/delete/CustomersUpdates.xml'"/>
    
     <xsl:variable name="vUpdates" select=
       "document($pUpdatesPath)"/>
    
     <xsl:variable name="vmainDoc" select="/"/>
    
     <xsl:template match="node()|@*">
      <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
     </xsl:template>
    
     <xsl:template match="Addresses">
      <Addresses>
       <xsl:apply-templates select="Address | $vUpdates/*/*/*">
        <xsl:sort select="AddressesId" data-type="number"/>
       </xsl:apply-templates>
      </Addresses>
     </xsl:template>
    
     <xsl:template match="Address">
       <xsl:variable name="vIsThisUpdate" select=
         "generate-id(/) = generate-id($vUpdates)"/>
    
       <xsl:variable name="vOtherDoc" select=
        "$vmainDoc[$vIsThisUpdate]
        |
         $vUpdates[not($vIsThisUpdate)]
        "/>
    
        <xsl:variable name="vCustId" select="../../CustId"/>
        <xsl:variable name="vCustName" select="../../CustName"/>
        <xsl:variable name="vAddrId" select="AddressesId"/>
    
        <xsl:variable name="vOtherNodeId">
         <xsl:for-each select="$vOtherDoc">
           <xsl:value-of select=
           "generate-id(key('kAddrById',
                            concat($vCustId,'+', $vCustName,
                                   '+', $vAddrId)
                           )
                       )"/>
         </xsl:for-each>
        </xsl:variable>
        <xsl:apply-templates mode="selected"
         select="self::node()
                  [$vIsThisUpdate
                 or
                  (not($vIsThisUpdate) and not(string($vOtherNodeId)))
                  ]">
         <xsl:with-param name="pIsUpdating" select="$vIsThisUpdate"/>
         <xsl:with-param name="pOtherDoc" select="$vOtherDoc"/>
         <xsl:with-param name="pOtherNodeId"
                         select="string($vOtherNodeId)"/>
        </xsl:apply-templates>
     </xsl:template>
    
     <xsl:template match="Address" mode="selected">
      <xsl:param name="pIsUpdating"/>
      <xsl:param name="pOtherDoc" select="/.."/>
      <xsl:param name="pOtherNodeId"/>
    
      <xsl:variable name="vStatus">
       <xsl:choose>
         <xsl:when test="$pIsUpdating and not($pOtherNodeId)">New</xsl:when>
           <xsl:when test="$pIsUpdating">
             <xsl:variable name="vOldStreet">
              <xsl:for-each select="$pOtherDoc">
                <xsl:value-of select=
                  "key('kNodeByGenId', $pOtherNodeId)/Street"/>
              </xsl:for-each>
             </xsl:variable>
    
             <xsl:choose>
               <xsl:when test=
               "Street = string($vOldStreet)">NoChange</xsl:when>
               <xsl:otherwise>Updated</xsl:otherwise>
             </xsl:choose>
           </xsl:when>
           <xsl:otherwise>Deleted</xsl:otherwise>
       </xsl:choose>
      </xsl:variable>
    
      <Address>
        <Address status="{$vStatus}"/>
        <xsl:apply-templates/>
      </Address>
     </xsl:template>
    </xsl:stylesheet>
    

    when applied on the provided XML document:

    <Customer>
        <CustId>1</CustId>
        <CustName>Acme</CustName>
        <Addresses>
            <Address>
                <AddressesId>1</AddressesId>
                <Street>123 Main</Street>
            </Address>
            <Address>
                <AddressesId>2</AddressesId>
                <Street>345 Main</Street>
            </Address>
            <Address>
                <AddressesId>4</AddressesId>
                <Street>888 Goner St.</Street>
            </Address>
        </Addresses>
    </Customer>
    

    and having at c:/temp/delete/CustomersUpdates.xml this XML document (slightly changed from the provided in order to have the first address end up with status “NoChange”):

    <Customer>
        <CustId>1</CustId>
        <CustName>Acme</CustName>
        <Addresses>
            <Address>
                <AddressesId>1</AddressesId>
                <Street>123 Main</Street>
            </Address>
            <Address>
                <AddressesId>2</AddressesId>
                <Street>999 Updated St.</Street>
            </Address>
            <Address>
                <AddressesId>3</AddressesId>
                <Street>3999 New St.</Street>
            </Address>
        </Addresses>
    </Customer>
    

    produces the wanted, correct result:

    <Customer>
       <CustId>1</CustId>
       <CustName>Acme</CustName>
       <Addresses>
          <Address>
             <Address status="NoChange"/>
             <AddressesId>1</AddressesId>
             <Street>123 Main</Street>
          </Address>
          <Address>
             <Address status="Updated"/>
             <AddressesId>2</AddressesId>
             <Street>999 Updated St.</Street>
          </Address>
          <Address>
             <Address status="New"/>
             <AddressesId>3</AddressesId>
             <Street>3999 New St.</Street>
          </Address>
          <Address>
             <Address status="Deleted"/>
             <AddressesId>4</AddressesId>
             <Street>888 Goner St.</Street>
          </Address>
       </Addresses>
    </Customer>
    

    II. XSLT 2.0 solution:

    <xsl:stylesheet version="2.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:my="my:my" exclude-result-prefixes="my">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:key name="kAddrById" match="Address"
      use="concat(../../CustId, '+', ../../CustName,
                  '+', AddressesId)"/>
    
     <xsl:param name="pUpdatesPath" select=
      "'file:///c:/temp/delete/CustomersUpdates.xml'"/>
    
     <xsl:variable name="vUpdates" select=
       "document($pUpdatesPath)"/>
    
     <xsl:variable name="vmainDoc" select="/"/>
    
     <xsl:template match="node()|@*">
      <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
     </xsl:template>
    
     <xsl:template match="Addresses">
      <Addresses>
       <xsl:apply-templates select="Address | $vUpdates/*/*/*">
        <xsl:sort select="AddressesId" data-type="number"/>
       </xsl:apply-templates>
      </Addresses>
     </xsl:template>
    
     <xsl:template match="Address[root() is $vUpdates]">
        <xsl:variable name="vOtherDoc" select="$vmainDoc"/>
    
        <xsl:variable name="vOtherNode" select="my:OtherNode(., $vOtherDoc)"/>
    
        <xsl:variable name="vStatus" select=
         "concat('New'[not($vOtherNode)],
                 'NoChange'[$vOtherNode 
                           and current()/Street eq $vOtherNode/Street],
                 'Updated'[$vOtherNode and current()/Street ne $vOtherNode/Street]
                 )"/>
        <xsl:apply-templates select="self::node()" mode="selected">
         <xsl:with-param name="pStatus" select="$vStatus"/>
        </xsl:apply-templates>
     </xsl:template>
    
      <xsl:template match="Address[not(root() is $vUpdates)]">
       <xsl:variable name="vOtherDoc" select="$vUpdates"/>
    
        <xsl:variable name="vOtherNode" select="my:OtherNode(., $vOtherDoc)"/>
    
        <xsl:apply-templates select="self::node()[not($vOtherNode)]" mode="selected">
          <xsl:with-param name="pStatus" select="'Deleted'"/>
        </xsl:apply-templates>
      </xsl:template>
    
     <xsl:template match="Address" mode="selected">
      <xsl:param name="pStatus"/>
    
      <Address>
        <Address status="{$pStatus}"/>
        <xsl:apply-templates/>
      </Address>
     </xsl:template>
    
     <xsl:function name="my:OtherNode" as="element()?">
       <xsl:param name="pThis" as="element()"/>
       <xsl:param name="pOtherDoc" as="document-node()"/>
    
        <xsl:sequence select=
         "key('kAddrById',
              concat($pThis/../../CustId,'+', $pThis/../../CustName,
                     '+', $pThis/AddressesId
                     ),
                     $pOtherDoc
              )"/>
     </xsl:function>
    </xsl:stylesheet>
    

    when this transformation is applied on the same documents, the same correct result is produced.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Firstly, there are 3 separate images. A chick, the word 'delivery' and 'foodstant' I
Firstly yes i know that ints are more native than bytes and that all
Firstly, this is for a class so there are limitations on what we can
Firstly is it possible to use objective C to find out if the user's
Firstly, I have search Stack Overflow for the answer, but I have not found
Firstly I've looked at a lot of posts on Stackoverflow but I don't see
Firstly, let me say that I know that http://sphinxsearch.com/docs/current.html#conf-sql-attr-string says strings can only be
Firstly, there have some tag links in my main page. click each one, post
Firstly, is there a way to use document.write() inside of JQuery's $(document).ready() method? If
Firstly, I have done my research and already know that JavaScript = client JSPs

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.