Subject: RE: [xsl] merge tree structure with no redundancy From: "McNally, David" <David.McNally@xxxxxxxxxx> Date: Fri, 2 Aug 2002 13:10:56 -0400 |
When in doubt, reach for keys and the Meunchian technique, both well described in the faq. Basically you need two keys - "me" which is used to figure out whether each node is the first, in document order, with a particular @type and @name pair. The second, "parent", is used to process all the nodes who are children of a particular @type and @name pair, regardless of where they are in the file. So basically the node template decides if the node is the first with its type/name - if it isn't it does nothing. If it is, it outputs the element and attributes, and then applies templates to all nodes that are "children" of that node, based on the "parent" key. I think this works properly, but handle with care... <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:key name="parent" match="node" use="concat(parent::node/@type,'::',parent::node/@name)"/> <xsl:key name="me" match="node" use="concat(@type,'::',@name)"/> <xsl:template match="a"> <a> <xsl:apply-templates select="subscribers/subscriber/path/node"/> </a> </xsl:template> <xsl:template match="node"> <xsl:variable name="mykey" select="concat(@type,'::',@name)"/> <xsl:choose> <xsl:when test="generate-id(.) = generate-id(key('me',$mykey)[1])"> <xsl:element name="{name()}"> <xsl:copy-of select="@*"/> <xsl:if test="not(node)"> <xsl:attribute name="id"> <xsl:value-of select="ancestor::subscriber/id/@id"/> </xsl:attribute> </xsl:if> <xsl:apply-templates select="key('parent',concat(@type,'::',@name))"/> </xsl:element> </xsl:when> </xsl:choose> </xsl:template> </xsl:stylesheet> Note that this will not work if type/name pairs can be reused in different type/name "parents". I.e.: <node type="T1" name="N1"> <node type="T2" name="N2"> </node> ... <node type="T1" name="N2"> <node type="T2" name="N2"><!-- same type and name as a previous node, but different "parent" --> </node> would be bad, and you'd need to combine lots of "parent" information into the "me" key. D:\Work\xsl>type subscriber.xml <?xml version="1.0" encoding="ISO-8859-1"?> <a> <subscribers> <subscriber> <id="1" /> <path> <node type="T1" name="N1"> <node type="T2" name="N2"> <node type="T3" name="N3" /> </node> </node> </path> </subscriber> <subscriber> <id="2" /> <path> <node type="T1" name="N1"> <node type="T2" name="N2"> <node type="T3" name="N4" /> </node> </node> </path> </subscriber> </subscribers> </a> D:\Work\xsl>saxon subscriber.xml subscriber.xslt <?xml version="1.0" encoding="UTF-8"?> <a> <node type="T1" name="N1"> <node type="T2" name="N2"> <node type="T3" name="N3" id="1"/> <node type="T3" name="N4" id="2"/> </node> </node> </a> Hope this helps, David. -- David McNally Moody's Investors Service Software Engineer 99 Church St, NY NY 10007 David.McNally@xxxxxxxxxx (212) 553-7475 > -----Original Message----- > From: Benoit Guyon [mailto:ben@xxxxxxxxxx] > Sent: Friday, August 02, 2002 11:43 AM > To: 'XSL-List@xxxxxxxxxxxxxxxxxxxxxx' > Subject: [xsl] merge tree structure with no redundancy > > > hello, > > I have a XML file with nodes that are representing a tree > structure. i want > to modify this file (with a XSL stylesheet) to get a new XML > file with a > unified tree structure that don't have redundancy. > > the original file : > > <?xml version="1.0" encoding="ISO-8859-1"?> > <a> > <subscribers> > <subscriber> > <id="1" /> > <path> > <node type="T1" name="N1"> > <node type="T2" name="N2"> > <node type="T3" name="N3" /> > </node> > </node> > </path> > </subscriber> > <subscriber> > <id="2" /> > <path> > <node type="T1" name="N1"> > <node type="T2" name="N2"> > <node type="T3" name="N4" /> > </node> > </node> > </path> > </subscriber> > </subscribers> > </a> > > the wanted file : > > <?xml version="1.0" encoding="ISO-8859-1"?> > <a> > <node type="T1" name="N1"> > <node type="T2" name="N2"> > <node type="T3" name="N3" id="1" /> > <node type="T3" name="N4" id="2" /> > </node> > </node> > </a> > > I don't know how to make that ... can i test output to detect > if a node > (identified with type and name atributes) is > already present ? > > when i'm in the second <subscriber> node, how can i detect > that i already > wrote the ancestor nodes, and how can i write the final node > under the right > ancestor ? > > does anybody have an idea or a general algorithm to help me ? > > best regards > ben > > PS : i use MSXML, i don't have the saxon:path() :/ ;) > > XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list > --------------------------------------- The information contained in this e-mail message, and any attachment thereto, is confidential and may not be disclosed without our express permission. If you are not the intended recipient or an employee or agent responsible for delivering this message to the intended recipient, you are hereby notified that you have received this message in error and that any review, dissemination, distribution or copying of this message, or any attachment thereto, in whole or in part, is strictly prohibited. If you have received this message in error, please immediately notify us by telephone, fax or e-mail and delete the message and all of its attachments. Thank you. Every effort is made to keep our network free from viruses. You should, however, review this e-mail message, as well as any attachment thereto, for viruses. We take no responsibility and have no liability for any computer virus which may be transferred via this e-mail message. XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
[xsl] merge tree structure with no , Benoit Guyon | Thread | RE: [xsl] merge tree structure with, McNally, David |
[xsl] XML to XML transformation, Subramanian Subraman | Date | Re: [xsl] XML to XML transformation, G. Ken Holman |
Month |