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 6875705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:23:14+00:00 2026-05-27T04:23:14+00:00

I have to merge two xml documents using xslt 1.0. Each xml document has

  • 0

I have to merge two xml documents using xslt 1.0. Each xml document has some articles with their headline and publication date into one. The condition is that the articles in the new xml document should be sorted in ascending order by date.
Following are document samples:
doc1.xml

The logjam moves
05-05/2002
Some text

Anti-Semitism in Europe
12-05/2002
some more text

And doc2.xml document is

<document>
  <article>
    <head>Launch Year A Success For Alliance</head>
    <text>
        <paragraph>para text</paragraph>
        <paragraph>para text</paragraph>
        <paragraph>para text</paragraph>
    </text>
    <date>
        <day>17</day>
        <month>05</month>
        <year>2002</year>
    </date>
    <source>Alliance</source>
    <portal>Finance</portal>
    <ID number="27"/>
  </article>
  <article>
    <head>ISA Savers Could Lose Tax Relief</head>
    <text>
        <paragraph>para text</paragraph>
        <paragraph>para text</paragraph>
        <paragraph>para text</paragraph>
        <paragraph>para text</paragraph>
    </text>
    <date>
        <day>10</day>
        <month>05</month>
        <year>2002</year>
    </date>
    <source>Money</source>
    <portal>Finance</portal>
    <ID number="26"/>
  </article>
</document>

and the desired output is:

<document>
  <article>
<head>The logjam moves</head>
      <date>
    <day>05</day>
    <month>05</month>
    <year>2002</year>
</date>
    <text>Some text </text>
  </article>
  <article>
<head>ISA Savers Could Lose Tax Relief</head>
<text> para text  para text para text para text </text>
<date>
  <day>10</day>
  <month>05</month>
  <year>2002</year>
    </date>
  </article>
  <article>
<head>Anti-Semitism in Europe</head>
    <date>
  <day>12</day>
      <month>05</month>
      <year>2002</year>
    </date>
    <text>some more text</text>
  </article>
  <article>
    <head> Launch Year A Success For Alliance </head>
    <text> para text  para text para text para text     </text>
    <date>
      <day>17</day>
      <month>05</month>
      <year>2002</year>
    </date>
  </article>
</document>

And my stylesheet is:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:param name="doc1File">doc1.xml</xsl:param>
  <xsl:variable name="doc1" select="document($doc1File)" as="document-node()"/>
  <xsl:param name="doc2File">doc2.xml</xsl:param>
  <xsl:variable name="doc2" select="document($doc2File)" as="document-node()"/>
  <xsl:template match="/">
    <xsl:for-each select ="$finance/document/article">
      <xsl:value-of select="head"/>
      <xsl:value-of select="text"/>
      <xsl:value-of select="date"/>
    </xsl:for-each>
    <xsl:for-each select ="$economist/document/ARTICLE">
      <xsl:value-of select="headline"/>
      <xsl:value-of select="text"/>
      <xsl:value-of select="date"/>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

As you can see, I have only been able to print the values of elements. I am unable to figure out how to

  1. Parse the differently mentioned date in doc1.xml and
  2. Merge the two documents, sorted on their publication date

Please help. thanks in advance.

  • 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-05-27T04:23:15+00:00Added an answer on May 27, 2026 at 4:23 am

    I’ve got a solution for you. You need to break the problem down into several steps.

    The first is normalising your data you are importing so that when you come to merge and sort
    the elements you have a similar structure.

    The second is then select and sort from that those normalised documents.

    To do this I’ve used a number of modes to separate out the various transforms (
    you may find it easier to just ensure your source documents are all the same format
    to start). A gotcha at this point is you don’t want to match on the “/” of the
    imported documents – otherwise you end up applying the ‘/’ match we use to do the merge.

    Once you have your data sufficiently normalised, the sorting becomes
    relatively simple.

    Regards

        <xsl:stylesheet version="1.0"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:exsl="http://exslt.org/common"
                                        extension-element-prefixes="exsl">
    
            <xsl:param name="doc1File">./doc1.xml</xsl:param>
            <xsl:param name="doc2File">./doc2.xml</xsl:param>
    
            <xsl:variable name="doc1">
                <xsl:apply-templates select="document($doc1File)/*" mode="import1"/>
            </xsl:variable>
            <xsl:variable name="doc2">
                <xsl:apply-templates select="document($doc2File)/*" mode="import2"/>
            </xsl:variable>
    
            <!-- merging and sorting -->
            <xsl:template match="/">
                <document>
                    <xsl:apply-templates select="
                        exsl:node-set($doc1)/document/article|
                        exsl:node-set($doc2)/document/article">
                        <xsl:sort select="date/year"/>
                        <xsl:sort select="date/month"/>
                        <xsl:sort select="date/day"/>
                    </xsl:apply-templates>
                </document>
            </xsl:template>
    
            <xsl:template match="article">
                <xsl:copy>
                    <xsl:apply-templates select="head|text|date"/>
                </xsl:copy>
            </xsl:template>
    
            <xsl:template match="*">
                <xsl:copy>
                    <xsl:copy-of select="@*"/>
                    <xsl:apply-templates/>
                </xsl:copy>
            </xsl:template>
    
            <!-- transforming document 1 -->
            <xsl:template match="ARTICLE" mode="import1">
                <article>
                    <head><xsl:value-of select="headline"/></head>
                    <date>
                        <day><xsl:value-of select="substring(date,1,2)"/></day>
                        <month><xsl:value-of select="substring(date,4,2)"/></month>
                        <year><xsl:value-of select="substring(date,7,4)"/></year>
                    </date>
                    <xsl:apply-templates select="text" mode="import1"/>
                </article>
            </xsl:template>
    
            <xsl:template match="*" mode="import1">
                <xsl:copy>
                    <xsl:copy-of select="@*"/>
                    <xsl:apply-templates mode="import1"/>
                </xsl:copy>
            </xsl:template>
    
        <!-- transforming document 2 -->
    
            <xsl:template match="text" mode="import2">
                <xsl:copy>
                    <xsl:copy-of select="@*"/>
                    <xsl:apply-templates select="paragraph" mode="import2"/>
                </xsl:copy>
            </xsl:template>
            <xsl:template match="paragraph" mode="import2">
                <xsl:choose>
                    <xsl:when test="position() != last()">
                        <xsl:value-of select="normalize-space(.)"/><xsl:text> </xsl:text>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="normalize-space(.)"/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:template>
    
            <xsl:template match="*" mode="import2">
                <xsl:copy>
                    <xsl:copy-of select="@*"/>
                    <xsl:apply-templates mode="import2"/>
                </xsl:copy>
            </xsl:template>
    
        </xsl:stylesheet>
    

    My result is

    <?xml version="1.0"?>
    <document>
      <article>
        <head>The logjam moves</head>
        <date>
          <day>05</day>
          <month>05</month>
          <year>2002</year>
        </date>
        <text>Some text </text>
      </article>
      <article>
        <head>ISA Savers Could Lose Tax Relief</head>
        <text>para text para text para text para text</text>
        <date>
          <day>10</day>
          <month>05</month>
          <year>2002</year>
        </date>
      </article>
      <article>
        <head>Anti-Semitism in Europe</head>
        <date>
          <day>12</day>
          <month>05</month>
          <year>2002</year>
        </date>
        <text>some more text</text>
      </article>
      <article>
        <head>Launch Year A Success For Alliance</head>
        <text>para text para text para text</text>
        <date>
          <day>17</day>
          <month>05</month>
          <year>2002</year>
        </date>
      </article>
    </document>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the best/fastest way to merge two xml documents with ruby? I have
I have these two xml documents: XML 1: <outer> <foo> <foo-child name=fA special=true> content
I have two documents that I need to merge, that happen in a way
I have written code in Java to merge two xml file, the first file
I'm trying to merge two file that have the same structure, and some data
Using xslt/xpath, I need to be able to select the two elements which have
What is the easiest way to merge XML from two distinct DOM Documents? Is
I need to merge two XML documents, overwriting the overlapsed attributes and elements. For
I have an XML file and I would like to merge two different CONTACT
I have two array I need to merge, and using the Union (|) operator

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.