Re: [xsl] Help with recursion (was: my brain is hurting)

Subject: Re: [xsl] Help with recursion (was: my brain is hurting)
From: "Joris Gillis" <roac@xxxxxxxxxx>
Date: Sat, 15 Jan 2005 12:16:09 +0100
Hi,

Tempore 11:04:32, die 01/15/2005 AD, hinc in xsl-list@xxxxxxxxxxxxxxxxxxxxxx scripsit Chris Picton <chrisp@xxxxxxxxxxxxx>:

the 'useparent' attribute specifies if that user or group should inherit its settings from its parent (1), or if it should specify its own (0). I am trying to get a list of users per group who should use the settings defined by that group.

I iterate over each group name, and run the transform with <param name='group'/>, passing in each group name in turn.

I'm not quite sure what yis the purpose of this parameter.

In the above case, user 'phil' is not inheriting setting from the parent, and as such wont appear in the list for any group.
User 'chris' is inheriting from group 'othergroup', which is in turn inheriting from group 'default'. So, the user list for group 'default' should contain the user 'chris'.
The user list list for group 'othergroup' should be empty, as it has no settings of its own.

Starting from this XML:


<?xml version="1.0" encoding="UTF-8"?>

<root>
  <settings>
   <user name="chris">
     <module useparent="1" >
         ...empty...
     </module>
   </user>
   <user name="phil">
     <module useparent="0" >
         ...module settings...
     </module>
   </user>
   <group name="default">
     <module useparent="0" >
         ...module settings...
     </module>
   </group>
   <group name="othergroup">
     <module useparent="1" >
         ...empty...
     </module>
   </group>
 </settings>

  <users>
    <user name="chris" parent="othergroup">
      ...
    </user>
    <user name="phil" parent="othergroup">
      ...
    </user>
    <group name="default" parent="">
      ...
    </group>
    <group name="othergroup" parent="default">
      ...
    </group>
  </users>
</root>

,the following stylesheet:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">
<xsl:output method="html" indent="yes"/>

<xsl:key name="settingGroup" match="settings/group[.//@useparent='1']" use="@name"/>
<xsl:key name="settingUser" match="settings/user[.//@useparent='1']" use="@name"/>
<xsl:key name="userGroupByName" match="users/group" use="@name"/>
<xsl:key name="userGroupByParent" match="users/group" use="@parent"/>
<xsl:key name="userUserByParent" match="users/user" use="@parent"/>


<xsl:template match="root">
	<xsl:apply-templates select="settings/group" mode="settings"/>
</xsl:template>

<xsl:template match="group" mode="settings">
<h1>Group: <xsl:value-of select="@name"/></h1>
<ul>
<xsl:apply-templates select="key('userGroupByParent',@name)" mode="users"/>
</ul>
</xsl:template>


<xsl:template match="group[.//@useparent='1']" mode="settings-silent">
<xsl:apply-templates select="key('userGroupByParent',@name)" mode="users"/>
</xsl:template>


<xsl:template match="group" mode="users">
<xsl:apply-templates select="key('userUserByParent',@name)" mode="users"/>
<xsl:apply-templates select="key('settingGroup',@name)" mode="settings-silent"/>
</xsl:template>


<xsl:template match="user" mode="users">
<xsl:if test="count(key('settingUser',@name))=1">
	<li><xsl:value-of select="@name"/></li>
</xsl:if>
</xsl:template>

</xsl:stylesheet>


will produce this result:


<h1>Group: default</h1>
<ul>
	<li>chris</li>
</ul>
<h1>Group: othergroup</h1>
<ul></ul>

As far as I understood your question, this should be the output you're looking for.


regards, -- Joris Gillis (http://www.ticalc.org/cgi-bin/acct-view.cgi?userid=38041) Deserta faciunt et innovationem appelant

Current Thread