Re: [xsl] One nodeset, multiple branches

Subject: Re: [xsl] One nodeset, multiple branches
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Wed, 19 Dec 2001 11:30:37 -0500
Eric,

You have the problem half solved. It's not really an XPath thing, exactly; it's in having to establish a relation between each <group> that contains <right> descendants, with the users assigned to that group.

As you have seen,

user[@username='evitiello]/rights/right

(or, more comprehensively but less efficiently, user[@username='evitiello]//right)

will get the user's own 'proper' rights (when the user is you :-).

To get the rights associated with groups assigned to the user, is of course a bit trickier. First:

<xsl:variable name="usergroups" select="user[@username='evitiello']//group/@name"/>

Gets the @name attributes on the groups belonging to your user (you) into a variable.

Then, get rights associated with these groups:

//group[@name=$usergroups]//right

(Again, this is comprehensive but not maximally efficient for your case.)

This relies on the fact that a node-set (here, a group/@name) is equal to another node-set (here, your variable, a collection of names) if any node in one node-set has a string value equal to any node in the other.

Combining these two XPaths does everything you need except de-duplicating the list. To do that, I'm afraid you'll have to iterate over the node-sets and check each node's preceding:: or following:: axis. This will get expensive if your data set is large.

Another approach, potentially much more efficient, is to use keys ... and that will let you de-duplicate as well (maybe I'll tackle that later if Jeni doesn't first ;-).

I don't think this XPath is over your head; it's just a bit hard to see around corners until you learn to use other features of the language (node-sets bound to variables; keys) as mirrors.

Cheers,
Wendell

At 09:22 AM 12/19/01, you wrote:
Hi.

I have the following XML:

<?xml version="1.0"?>
<access>
        <users>
                <user username="evitiello">
                        <realName>Eric Vitiello</realName>
                        <rights>
                                <right module="news" access="add"/>
                                <right module="page" access="edit"/>
                        </rights>
                        <userGroups>
                                <group name="administrators"/>
                        </userGroups>
                </user>
        </users>
        <groups>
                <group name="marketing">
                        <rights>
                                <right module="news" access="add"/>
                        </rights>
                </group>
                <group name="administrators">
                        <rights>
                                <right module="news" access="add"/>
                                <right module="news" access="edit"/>
                                <right module="news" access="delete"/>
                                <right module="page" access="add"/>
                        </rights>
                </group>
        </groups>
</access>

I would like to be able to return a nodeset that contains all of the "rights" that a given user has.


======================================================================
Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
  Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================


XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list



Current Thread