This is the mail archive of the xsl-list@mulberrytech.com 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: merge tree structure with no redundancy


BTW, I typed the wrong copy of subscriber.xml.  It should be (note the
corrected id element):

<?xml version="1.0" encoding="ISO-8859-1"?>
<a>
  <subscribers>
    <subscriber>
      <id id="1"/>
      <path>
        <node type="T1" name="N1">
          <node type="T2" name="N2">
            <node type="T3" name="N3"/>
          </node>
        </node>
      </path>
    </subscriber>
    <subscriber>
      <id id="2"/>
      <path>
        <node type="T1" name="N1">
          <node type="T2" name="N2">
            <node type="T3" name="N4"/>
          </node>
        </node>
      </path>
    </subscriber>
  </subscribers>
</a>

David.

> -----Original Message-----
> From: McNally, David 
> Sent: Friday, August 02, 2002 1:11 PM
> To: 'xsl-list@lists.mulberrytech.com'
> Subject: RE: [xsl] merge tree structure with no redundancy
> 
> 
> 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>
> 
[snip]


---------------------------------------

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


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