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
- Parse the differently mentioned date in doc1.xml and
- Merge the two documents, sorted on their publication date
Please help. thanks in advance.
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
My result is