[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: document() merge DISTINCT -- a Simple Functional S
> 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" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:mytestElem="f:mytestElem" > <xsl:import href="foldl.xsl"/> <xsl:output indent="yes" omit-xml-declaration="yes"/> <mytestElem:mytestElem/> <xsl:template match="/"> <xsl:variable name="vFun-testElem" select="document('')/*/mytestElem:*[1]"/> <xsl:call-template name="foldl"> <xsl:with-param name="pFunc" select="$vFun-testElem"/> <xsl:with-param name="pA0" select="/.."/> <xsl:with-param name="pList" select="document('testDistinct1.xml')/p/person | document('testDistinct2.xml')/p/person | document('testDistinct3.xml')/p/person" /> </xsl:call-template> </xsl:template> <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"/> </xsl:if> </xsl:template> </xsl:stylesheet> When applied and if the contents of the three referenced xml files is as follows: testDistinct1: ------------- <p> <person name="name2" id="2" /> <person name="name3" id="8" /> <person name="name4" id="32" /> </p> testDistinct2: ------------- <p> <person name="name5" id="3" /> <person name="name6" id="8" /> <person name="name7" id="32" /> </p> testDistinct3: ------------- <p> <person name="name8" id="2" /> <person name="name9" id="3" /> <person name="name10" id="13" /> </p> 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: http://aspn.activestate.com/ASPN/Mail/Message/xsl-list/895664 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. Cheers, Dimitre Novatchev. __________________________________________________ Do You Yahoo!? Check out Yahoo! Shopping and Yahoo! Auctions for all of your unique holiday gifts! Buy at http://shopping.yahoo.com or bid at http://auctions.yahoo.com XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|