-1

I have two xmls

  1. a.xml
  2. b.xml

Below is the a.xml file

<proj_details>
    <proj_detail>
        <Username>Username</Username>
        <ID>ID</ID>
        <Place>Ocean</Place>
        <City>Urban</City>
        <!-- some more fields -->
    </proj_detail>
    <proj_detail>
        <Username>abc@abc.com</Username>
        <ID>QoChiX5FlqU7VB2MKw5Aa</ID>
        <Place>Road</Place>
        <City>Rural</City>
        <!-- some more fields -->
    </proj_detail>
    <proj_detail>
        <Username>abc@abc.com</Username>
        <ID>uzGjRVxnZPtB9sfiXtga</ID>
        <Place>Mud</Place>
        <City>SemiRural</City>
        <!-- some more fields -->
    </proj_detail>
    <proj_detail>
        <Username>abc@abc.com</Username>
        <ID>YChiX5FluqhuyVB2OPw5Aa</ID>
        <Place>Sea</Place>
        <City>Sealine</City>
        <!-- some more fields -->
    </proj_detail>
    <proj_detail>
        <Username>abc@abc.com</Username>
        <ID>RD37oO2jgW5p6f76W8up</ID>
        <Place>Lane</Place>
        <City>Leftsidelane</City>
        <!-- some more fields -->
    </proj_detail>
    </proj_details>

    <!-- more fields    -->

and below is the b.xml

    <FilebDetail>
        <Number>34</Number>
        <CONS>abc@abc.com</CONS>
        <PROD>xyz@xyz.com</PROD>
        <ID>QoChiX5FlqU7VB2MKw5Aa</ID>
        <Name>Directory/notepad</Name>
        <Count>43</Count>
        <time>43550.53333336</time>
        <!-- some more fields-->
    </FilebDetail>
    <FilebDetail>
        <Number>35</Number>
        <CONS>abc@abc.com</CONS>
        <PROD>cvb@cvb.com</PROD>
        <ID>uzGjRVxnZPtB9sfiXtga</ID>
        <Name>Speed/morning</Name>
        <Count>42</Count>
        <time>43550.5778</time>
        <!-- some more fields-->
    </FilebDetail>
    <FilebDetail>
        <Number>31</Number>
        <CONS>abc@abc.com</CONS>
        <PROD>cvb@cvb.com</PROD>
        <ID>RD37oO2jgW5p6f76W8up</ID>
        <Name>Weatherincity</Name>
        <Count>12</Count>
        <time>43550.498611111114</time>
        <!-- some more fields-->
    </FilebDetail>

    </FilebDetails>

Now, I want to get the output somewhat like below

        <proj_detail>
        <Number>34</Number>
        <CONS>abc@abc.com</CONS>
        <PROD>xyz@xyz.com</PROD>
        <ID>QoChiX5FlNyqe1FuqU7VB2M</ID>
        <Name>Directory/notepad</Name>
        <Count>12</Count>
        <time>43550.498611111114</time>
        <Place>Road</Place>
        <City>Rural</City>
         <!-- some more fields -->
        </proj_detail>

        <proj_detail>
        <Number>35</Number>
        <CONS>abc@abc.com</CONS>
        <PROD>cvb@cvb.com</PROD>
        <ID>uzGjRVxnxmP00zrnZPtB9sM</ID>
        <Name>Speed/morning</Name>
        <Count>42</Count>
        <time>43550.5778</time>
        <Place>Mud</Place>
        <City>SemiRural</City>
        </proj_detail>
         <!-- some more fields -->
        <proj_detail>
        <Number>31</Number>
        <CONS>abc@abc.com</CONS>
        <PROD>cvb@cvb.com</PROD>
        <ID>RD37oO2jg4rlerxW5p6f76</ID>
        <Name>Weatherincity</Name>
        <Count>12</Count>
        <time>43550.498611111114</time>
        <Place>Lane</Place>
        <City>Leftsidelane</City>
        </proj_detail>
        <!-- some more fields -->
        </proj_details>

In both files ID is common and that is what needs to be compared. Once its compared, then we the corresponding fields from both the files are to be printed.

I am pretty new in xslt. Any xslt version would be ok to use.

Any guidance in this would be really helpful.

Thanks

2437850
  • 287
  • 2
  • 10

1 Answers1

0

If you have a one-to-one relationship, the merging will be straightforward. Running this XSLT 2.0 stylesheet with a.xml as input and b.xml as parameter:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:param name="update"/>

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

    <xsl:template match="proj_detail">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
            <xsl:apply-templates 
              select="$update/FilebDetails/FilebDetail[ID = current()/ID]/(* except ID)"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

Do note: You have to be careful about the mechanism your XSLT processor has for passing nodes as parameters, otherwise you will need to use the document() function.

From this basic transformation you can add optimizations like the use of key() function (you will have to use the XSLT 2.0 -arity 3- key() function). Also you could declare that key by ID and "field" name assuming that $update parameter is an update stream and thus you will be selecting the last ones, etc.

Alejandro
  • 1,882
  • 6
  • 13