Filter a Muenchian Group

Discussion in 'XML' started by la.brunning@googlemail.com, Dec 6, 2006.

  1. Guest

    Hello,

    I am trying to filter a Muenchian grouping I have created. I think I am
    fairly close, but I am not quite sure where to apply my filter; its
    been some time since I did any work with XSLT. An example of my XML;

    <?xml version="1.0" encoding="UTF-8"?>
    <?xml-stylesheet href="Structure.xsl" type="text/xsl"?>
    <dataroot xmlns:eek:d="urn:schemas-microsoft-com:eek:fficedata">
    <CostCentre>
    <ServiceOrder>1</ServiceOrder>
    <Service>Directorate Management Team</Service>
    <ServiceDivisionOrder>10</ServiceDivisionOrder>
    <ServiceDivisionName>DMT</ServiceDivisionName>
    <SectionOrder>A</SectionOrder>
    <Section>Service Management and Development</Section>
    <CostCentreID>6000</CostCentreID>
    </CostCentre>
    <CostCentre>
    <ServiceOrder>2</ServiceOrder>
    <Service>Commissioning and Partnership Services</Service>
    <ServiceDivisionOrder>20</ServiceDivisionOrder>
    <ServiceDivisionName>Commissioning and Partnership
    Services</ServiceDivisionName>
    <SectionOrder>A</SectionOrder>
    <Section>Commissioning and Partnerships</Section>
    <CostCentreID>600N</CostCentreID>
    </CostCentre>

    There are several cost centres in each section, several sections in
    each service division and several service divisions in each service. I
    would like to group by service, service division and then section. So
    far I have managed to get the following XSL;

    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:key name="AllServices" match="CostCentre" use="Service"/>
    <xsl:key name="AllServiceDivisions" match="CostCentre"
    use="concat(Service ';' ServiceDivisionName)"/>
    <xsl:template match="/">
    <html>
    <body>
    <ol>
    <xsl:for-each
    select="//CostCentre[generate-id(.)=generate-id(key('AllServices',Service))]">
    <xsl:sort select="ServiceOrder" data-type="number" />
    <li><xsl:value-of select="Service" /></li>
    <xsl:variable name="currentService">
    <xsl:value-of select="Service"/>
    </xsl:variable>
    <ul>
    <xsl:for-each
    select="//CostCentre[generate-id(.)=generate-id(key('AllServiceDivisions',concat(Service
    ';' ServiceDivisionName)))] ">
    <xsl:sort select="ServiceDivisionOrder" data-type="number" />
    <li><xsl:value-of select="ServiceDivisionName" /></li>
    </xsl:for-each>
    </ul>
    </xsl:for-each>
    </ol>
    </body>
    </html>
    </xsl:template>
    </xsl:stylesheet>

    I *think* I have made the variable correctly, but am I totally lost at
    where to apply the filter (using $currentService). Currently, as you
    can imagine, I get all service divisions in each service, rather than
    just the service divisions for that service. I have tried several
    syntaxes and several locations but with no such luck. Once I get this
    right, I believe I can crack the rest. Can anyone shed any light on
    where and how I should apply the filter??? Thanks.
     
    , Dec 6, 2006
    #1
    1. Advertising

  2. It'd help if you'd been a bit more specific about how you wanted to
    filter this. Since you said you want to filter based on $currentService,
    and that's being set from the value of the <Service> element, I presume
    your goal is to select only those <CostCentre>s which have a particular
    <Service> value.

    There are multiple ways you can do this. For example, you might apply
    that filtering before you spend time sorting. You can do that by simply
    replacing all references to CostCentre with
    CostCentre[Service=$interestingService] -- selecting only the
    interesting CostCentre(s) -- before applying the other predicates.

    Here's one flavor of that, applying minimal changes to your existing
    code. Note that I'm taking advantage of the fact that you can apply more
    than one predicate, just to keep this modular and easy to read; you
    could of course combine this test into the existing select predicate
    instead.

    <xsl:variable name="interestingService"
    select="'Directorate Management Team'"/>
    <xsl:for-each
    select="//CostCentre[Service=$interestingService][generate-id(.)=generate-id(key('AllServices',Service))]">
    <xsl:sort select="ServiceOrder" data-type="number" />
    <li><xsl:value-of select="Service" /></li>
    <xsl:variable name="currentService">
    <xsl:value-of select="Service"/>
    </xsl:variable>
    <ul>
    <xsl:for-each
    select="//CostCentre[Service=$interestingService][generate-id(.)=generate-id(key('AllServiceDivisions',concat(Service
    ';' ServiceDivisionName)))] ">
    <xsl:sort select="ServiceDivisionOrder" data-type="number" />
    <li><xsl:value-of select="ServiceDivisionName" /></li>
    </xsl:for-each>
    </ul>
    </xsl:for-each>
    </ol>


    Of course if you're only going to select one specific Service value,
    there's no real need to include that value in your Muenchen keying...
    but I'm trying to minimize changes to your existing code
     
    Joe Kesselman, Dec 7, 2006
    #2
    1. Advertising

  3. On Dec 7, 2:30 am, Joe Kesselman <> wrote:
    > It'd help if you'd been a bit more specific about how you wanted to
    > filter this. Since you said you want to filter based on $currentService,
    > and that's being set from the value of the <Service> element, I presume
    > your goal is to select only those <CostCentre>s which have a particular
    > <Service> value.


    Hello Joe,

    Sorry, looking back at my post today it is indeed not very clear what I
    want. I suppose a more accurate description would be "Grouping" rather
    than "Filtering", as that's what the filter is doing; I am creating a
    report for an intranet which groups together cost centres in the
    relevant areas; Section, Service Division and Service. The data is
    exported from an Access database.

    Many thanks for the reply; your solution worked perfectly, and slots
    into my code to give me just what I need. Hopefully I can now use this
    throughout the rest of the reports. Once again thanks for the clear and
    concise reply.

    Luke
     
    Luke Brunning, Dec 7, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Colin Mackenzie
    Replies:
    0
    Views:
    1,118
    Colin Mackenzie
    Jul 3, 2003
  2. Marrow
    Replies:
    2
    Views:
    1,256
    Marrow
    Jul 7, 2003
  3. Tristan Miller

    XSL grouping (Muenchian)

    Tristan Miller, Jan 31, 2005, in forum: XML
    Replies:
    2
    Views:
    566
    Philippe Poulard
    Jan 31, 2005
  4. reclusive monkey
    Replies:
    2
    Views:
    1,422
    reclusive monkey
    Aug 10, 2005
  5. Muenchian problem

    , May 9, 2006, in forum: XML
    Replies:
    1
    Views:
    435
    George Bina
    May 10, 2006
Loading...

Share This Page