This is the mail archive of the mailing list .

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: document() merge DISTINCT -- a Simple Functional Solution

> I have some input files  with the /person/@id attribute being unique in each
> file (and /project).
> input.xml
> --------------------------------------------------------------
> <project name="some-name">
>     <person id="1" name="name1"/>
>     <person id="5" name="other-name"/>
>     <preson id="20" name ="another-name"/>
> </project>
> ------------------------------------------------
> I want to merge these files so that I get a list of all <person> that are in
> any <project> but the preson/@id should be unique, that is, no <person>
> element should be listed twice.

In a functional programming language (e.g. Haskell) one would define:

addDistinct         :: Eq a => [a] -> a -> [a]
addDistinct ls x  | x `elem` ls   = ls
                  | otherwise     = ls ++ [x]

makeDistinct    :: Eq a => [a] -> [a]
makeDistinct ls  = foldl addDistinct [] ls 

The first function appends an element to a list, only if this element does not
already belong to that list.

The second function uses the first to make from a list another one, which contains
only the distinct elements of the first list.

This can directly be translated in XSLT as follows:

<xsl:stylesheet version="1.0"
    <xsl:import href="foldl.xsl"/>
    <xsl:output indent="yes" omit-xml-declaration="yes"/>
    <xsl:template match="/">
      <xsl:variable name="vFun-testElem"

      <xsl:call-template name="foldl">
        <xsl:with-param name="pFunc" select="$vFun-testElem"/>
        <xsl:with-param name="pA0" select="/.."/>
        <xsl:with-param name="pList"
                              | document('testDistinct2.xml')/p/person
                              | document('testDistinct3.xml')/p/person"
    <xsl:template match="mytestElem:*">
      <xsl:param name="arg1" select="/.."/>
      <xsl:param name="arg2" select="/.."/>
      <xsl:variable name="vList" select="$arg1/@id"/>
      <xsl:variable name="vList2" select="$arg1/*/@id"/>
      <xsl:variable name="vElem" select="$arg2/@id"/>

      <xsl:copy-of select="$arg1"/>
      <xsl:if test="not($arg2/@id = $arg1/*/@id)">
        <xsl:copy-of select="$arg2"/>

When applied and if the contents of the three referenced xml files is as follows:

    <person name="name2" id="2" />
    <person name="name3" id="8" />
    <person name="name4" id="32" />

    <person name="name5" id="3" />
    <person name="name6" id="8" />
    <person name="name7" id="32" />

    <person name="name8" id="2" />
    <person name="name9" id="3" />
    <person name="name10" id="13" />

The result is:

<person name="name2" id="2" />
<person name="name3" id="8" />
<person name="name4" id="32" />
<person name="name5" id="3" />
<person name="name10" id="13" />

Note that here I'm re-using the "foldl" template:

We are passing to foldl() the list of elements to be made distinct and a small
function (template), which when called with a list and an element adds the element
to the end of the list only when the element is not already represented in the list.

By passing as parameter different such functions, we can solve any "make distinct"
problem, without having to re-implement the general solution.

Hope this helped.

Dimitre Novatchev.

Do You Yahoo!?
Check out Yahoo! Shopping and Yahoo! Auctions for all of
your unique holiday gifts! Buy at
or bid at

 XSL-List info and archive:

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]