Re: [xsl] Joining xml documents on the fly

Subject: Re: [xsl] Joining xml documents on the fly
From: "James A. Robinson" <jim.robinson@xxxxxxxxxxxx>
Date: Thu, 04 Jun 2009 08:44:39 -0700
> I want to break this into two groups based on the current project_id.
> So if I'm viewing project_id=1
> I want two groups of people
> 
> Project Staff:
> <people>
>     <staff staff_id="123456789" alpha_key="A" sort_key="AberyBrian">
>         <display_name>Brian H Abery</display_name>
>     </staff>
> </people>
> 
> Non-Project-Staff
> <people>
>     <staff staff_id="987654321" alpha_key="A" sort_key="AlbusDeb">
>         <display_name>Deb Albus</display_name>
>     </staff>
>     <staff staff_id="456321789" alpha_key="A" sort_key="AltmanJason">
>         <display_name>Jason R Altman</display_name>
>     </staff>
> </people>
> 
> I've got the following variables in my document:
> <xsl:variable name="staff" select="document($ref_staff)/people"/>
> <xsl:variable name="staff_roles" 
> select="document($ref_project_staff_role)/project_staff_roles/project_staff_role[@role_id='staff']"/>
> <xsl:variable name="project_staff" 
> select="$staff_roles[@project_id=$[project_id]/staff"/>
> <xsl:variable name="non_project_staff" select="?????"/>
> 
> How do I even go about thinking about this? I've tried googling it but I 
> have no idea what to search on. I've tried various iterations of keys, 
> etc but I just can't get it.

I don't know how important @role_id as you indicate in your code above
is to your solution, but just looking at it from the point of view of
set operations on staff ids and project ids:

    <!-- staff ids of those people involved in project $project-id -->
    <xsl:variable name="project-staff"
      select="distinct-values($projects/project_staff_roles/project_staff_role[@project_id eq $project-id]/@staff_id)" />

    <!-- staff ids of all people not involved in project $project-id -->
    <xsl:variable name="non-project-staff"
      select="distinct-values($people/people/staff[@staff_id != $project-staff]/@staff_id)" />

e.g. (you can replace the variables with document() calls, of course),

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; 
      xmlns:xs="http://www.w3.org/2001/XMLSchema";
      exclude-result-prefixes="xs"
      version="2.0">
  
  <xsl:variable name="people">
    <people>
      <staff staff_id="123456789" alpha_key="A" sort_key="AberyBrian">
        <display_name>Brian H Abery</display_name>
      </staff>
      <staff staff_id="987654321" alpha_key="A" sort_key="AlbusDeb">
        <display_name>Deb Albus</display_name>
      </staff>
      <staff staff_id="456321789" alpha_key="A" sort_key="AltmanJason">
        <display_name>Jason R Altman</display_name>
      </staff>
    </people>
  </xsl:variable>
  
  <xsl:variable name="projects">
    <project_staff_roles>
      <project_staff_role project_id="1" staff_id="123456789" role_id="staff">
        <staff staff_id="123456789" alpha_key="A" sort_key="AberyBrian">
          <display_name>Brian H Abery</display_name>
        </staff>
      </project_staff_role>
      <project_staff_role project_id="1" staff_id="123456789" role_id="director">
        <staff staff_id="123456789" alpha_key="A" sort_key="AberyBrian">
          <display_name>Brian H Abery</display_name>
        </staff>
      </project_staff_role>
      <project_staff_role project_id="2" staff_id="987654321" role_id="staff">
        <staff staff_id="987654321" alpha_key="A" sort_key="AlbusDeb">
          <display_name>Deb Albus</display_name>
        </staff>
      </project_staff_role>
    </project_staff_roles>
  </xsl:variable>

  <xsl:output indent="yes" />

  <xsl:template match="/">
    <xsl:param name="project-id" select="'1'" />

    <!-- staff ids of those people involved in project $project-id -->
    <xsl:variable name="project-staff"
      select="distinct-values($projects/project_staff_roles/project_staff_role[@project_id eq $project-id]/@staff_id)" />

    <!-- staff ids of all people not involved in project $project-id -->
    <xsl:variable name="non-project-staff"
      select="distinct-values($people/people/staff[@staff_id != $project-staff]/@staff_id)" />

    <people type="Project Staff" project_id="{$project-id}">
      <xsl:copy-of select="$people/people/staff[@staff_id = $project-staff]" />
    </people>

    <people type="Non-Project Staff" project_id="{$project-id}">
      <xsl:copy-of select="$people/people/staff[@staff_id = $non-project-staff]" />
    </people>
    
  </xsl:template>

</xsl:stylesheet>

produces

<people type="Project Staff" project_id="1">
   <staff staff_id="123456789" alpha_key="A" sort_key="AberyBrian">
      <display_name>Brian H Abery</display_name>
   </staff>
</people>
<people type="Non-Project Staff" project_id="1">
   <staff staff_id="987654321" alpha_key="A" sort_key="AlbusDeb">
      <display_name>Deb Albus</display_name>
   </staff>
   <staff staff_id="456321789" alpha_key="A" sort_key="AltmanJason">
      <display_name>Jason R Altman</display_name>
   </staff>
</people>


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
James A. Robinson                       jim.robinson@xxxxxxxxxxxx
Stanford University HighWire Press      http://highwire.stanford.edu/
+1 650 7237294 (Work)                   +1 650 7259335 (Fax)

Current Thread