xsl-list
[Top] [All Lists]

RE: Problems selecting the right nodes.

2003-09-25 13:20:52
If you define a key like this:
<xsl:key name="branch_services" match="branch" use="service/@id"/>

Then you can find out how many branches have a particular service like this:
count(key('branch_services',@id))

And so if the count is less than the total number of branches, then not
every branch has that service.

So, adding some muenchian grouping to get unique services, I think this
should give you the nodeset you need:


<?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="services" match="service" use="@id"/>
<xsl:key name="branch_services" match="branch" use="service/@id"/>

<xsl:variable name="branches" select="count(//branch)"/>

<xsl:variable name="service_nodes" select="//service[generate-id(.) =
generate-id(key('services'
,@id)[1])][count(key('branch_services',@id)) &lt; $branches]"/>

<xsl:template match="/">
<doc>
<xsl:for-each select="$service_nodes">
        <xsl:copy-of select="."></xsl:copy-of>  
</xsl:for-each>
</doc>
</xsl:template>

</xsl:stylesheet>



Hope this helps,
David.
--
David McNally            Moody's Investors Service
Software Engineer        99 Church St, NY NY 10007 
David(_dot_)McNally(_at_)Moodys(_dot_)com            (212) 553-7475 



-----Original Message-----
From: Adam van den Hoven [mailto:list(_at_)adamvandenhoven(_dot_)com] 
Sent: Thursday, September 25, 2003 12:26 PM
To: XSL Mailing List
Subject: [xsl] Problems selecting the right nodes. 


I have a nodeset or arbitrary length that contains a set of 
tags which 
look something like:

<branch id="br1">
   <service id="service1" />
   <service id="service2" />
   <service id="service3" />
</branch>
<branch id="br2">
    <service id="service1" />
    <service id="service4" />
</branch>

Now what I want is a nodeset that contains (uniquely) all the 
services 
that do not occur in all the branches. In this case I want 
service 2, 3 
and 4 but not 1

My original solution was iffy. I ended up creating a tokenized string 
("Service2|Service3|Service4" in this case) which I then ran 
through a 
recursive named template which did a lot of substring-before and 
substring-after. Since what I cared about are the ids, that was fine. 
However, the input file is occasionally very large so this can take a 
very long time. I want to find a better solution.

If possible, I think I'd like to avoid using extension 
functions, unless 
there is an obvious performance boost.

Any thoughts?
Adam


 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



<Prev in Thread] Current Thread [Next in Thread>