Solved

Muenchian grouping and knowing selects inside a group

Posted on 2006-11-21
2
268 Views
Last Modified: 2012-06-21
Hello experts,

I've had good advice re: grouping already, and below I've worked it OK for strains, but I just can't get (the correct) multiple rows per expt/strain combination! I guess I'm still not understanding completely. I would ideally like a good explaination of how/why a solution works because I really need to learn fully for myself.

I've included my desired result along with the xml & xsl below. What I get is all the plates for the current experiment listed under each strain, instead of just the ones for that strain.

Many thanks,

Gary

--------desired result--------

22303
  TA 100
    162, 37, 180
    109, 130, 145
  TA 102
    8, 48, 196
    65, 27, 84
22304
  TA 100
    179, 168, 3
    35, 9, 161
  TA 102
    15, 196, 146
    26, 81, 60

--------xml--------

<PLATE_SUMMARY>
  <EXPT_ID>22303</EXPT_ID>
  <STRAIN_CODE>TA 100</STRAIN_CODE>
  <COUNTS>162, 37, 180</COUNTS>
</PLATE_SUMMARY>
<PLATE_SUMMARY>
  <EXPT_ID>22303</EXPT_ID>
  <STRAIN_CODE>TA 100</STRAIN_CODE>
  <COUNTS>109, 130, 145</COUNTS>
</PLATE_SUMMARY>
<PLATE_SUMMARY>
  <EXPT_ID>22303</EXPT_ID>
  <STRAIN_CODE>TA 102</STRAIN_CODE>
  <COUNTS>8, 48, 196</COUNTS>
</PLATE_SUMMARY>
<PLATE_SUMMARY>
  <EXPT_ID>22303</EXPT_ID>
  <STRAIN_CODE>TA 102</STRAIN_CODE>
  <COUNTS>65, 27, 84</COUNTS>
</PLATE_SUMMARY>
<PLATE_SUMMARY>
  <EXPT_ID>22304</EXPT_ID>
  <STRAIN_CODE>TA 100</STRAIN_CODE>
  <COUNTS>179, 168, 3</COUNTS>
</PLATE_SUMMARY>
<PLATE_SUMMARY>
  <EXPT_ID>22304</EXPT_ID>
  <STRAIN_CODE>TA 100</STRAIN_CODE>
  <COUNTS>35, 9, 161</COUNTS>
</PLATE_SUMMARY>
<PLATE_SUMMARY>
  <EXPT_ID>22304</EXPT_ID>
  <STRAIN_CODE>TA 100</STRAIN_CODE>
  <COUNTS>123, 178, 30</COUNTS>
</PLATE_SUMMARY>
<PLATE_SUMMARY>
  <EXPT_ID>22304</EXPT_ID>
  <STRAIN_CODE>TA 102</STRAIN_CODE>
  <COUNTS>15, 196, 146</COUNTS>
</PLATE_SUMMARY>
<PLATE_SUMMARY>
  <EXPT_ID>22304</EXPT_ID>
  <STRAIN_CODE>TA 102</STRAIN_CODE>
  <COUNTS>26, 81, 60</COUNTS>
</PLATE_SUMMARY>

--------xsl--------

<?xml version='1.0' encoding='utf-8'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:key name="EXPT" match="PLATE_SUMMARY" use="EXPT_ID"/>
<xsl:key name="STRAIN" match="PLATE_SUMMARY" use="STRAIN_CODE"/>
<!--ROOT TEMPLATE-->
<xsl:template match="/">
<html>
<head>
  <title>Study <xsl:value-of select="STUDY/STUDY_CODE" /></title>
</head>
<body>
  <xsl:for-each select="/STUDY/PROCESSED_DATA/PLATE_SUMMARY[generate-id() = generate-id(key('EXPT',EXPT_ID)[1])]">
    <h1><xsl:value-of select="EXPT_ID" /></h1>
    <xsl:for-each select="/STUDY/PROCESSED_DATA/PLATE_SUMMARY[generate-id() = generate-id(key('STRAIN',STRAIN_CODE)[1])]">
      <h2><xsl:value-of select="STRAIN_CODE" /></h2>
      <table border="1">
        <thead>
          <tr>
            <td>COUNTS</td>
          </tr>
        </thead>
        <xsl:for-each select="-------!!!!I DON'T KNOW!!!!-------">
          <tr>
            <td><xsl:value-of select="COUNTS" /></td> <!--SHOULD GET MULTIPLE ROWS HERE SPECIFIC TO CURRENT EXPT & STRAIN-->
          </tr>
        </xsl:for-each>
      </table>
    </xsl:for-each>
  </xsl:for-each>
</body>
</html>
</xsl:template>

0
Comment
Question by:theskintman
  • 2
2 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 17986616
Hi theskintman,

Here is your XSLT
<?xml version='1.0' encoding='utf-8'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html"/>
    <xsl:key name="EXPT" match="PLATE_SUMMARY" use="EXPT_ID"/>
    <xsl:key name="STRAIN" match="PLATE_SUMMARY" use="STRAIN_CODE"/>
    <!--ROOT TEMPLATE-->
    <xsl:template match="/">
        <html>
            <body>
                <xsl:for-each select="//PLATE_SUMMARY[generate-id() = generate-id(key('EXPT',EXPT_ID)[1])]">
                    <h1><xsl:value-of select="EXPT_ID" /></h1>
                    <xsl:variable name="thisExpt" select="EXPT_ID"></xsl:variable>
                    <xsl:for-each select="//PLATE_SUMMARY[generate-id() = generate-id(key('STRAIN',STRAIN_CODE)[EXPT_ID = $thisExpt][1])]">
                        <h2><xsl:value-of select="STRAIN_CODE" /></h2>
                        <table border="1">
                            <thead>
                                <tr>
                                    <td>COUNTS</td>
                                </tr>
                            </thead>
                            <xsl:for-each select="key('STRAIN',STRAIN_CODE)[EXPT_ID = $thisExpt]">
                                <tr>
                                    <td><xsl:value-of select="COUNTS" /></td>
                                </tr>
                            </xsl:for-each>
                        </table>
                    </xsl:for-each>
                </xsl:for-each>
            </body>
        </html>
    </xsl:template>
    </xsl:stylesheet>

Cheers!
0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 400 total points
ID: 17986670
theskintman,

you were almost there, and you seem to understand Muenchian OK.
It is a bit trickier if you are dealing with double keys and nested loops

- first you have to know that your outer loop creates a context of PLATE_SUMMARY
    so this <xsl:for-each select="/STUDY/PROCESSED_DATA/...
    can not work, since you don't have STUDY inside PLATE_SUMMARY
    note that I used <xsl:for-each select="//PLATE_SUMMARY[....  to start at the document root again,
    if that is not possible because you have to stay in a certain scope, you can change that to
    <xsl:for-each select="../PLATE_SUMMARY[

- using key(), you use the index of the entire series of PLATE_SUMMARY,
    that is why you have to repeat the EXPT_ID condition
    here is how to do that [generate-id() = generate-id(key('STRAIN',STRAIN_CODE)[EXPT_ID = $thisExpt][1])]">
    I use the same trick as the outer loop, but I added [EXPT_ID = $thisExpt][1] inside the generate-id
    so I am taking the first PLATE_SUMMARY that has this specific EXPT_ID and STRAIN_CODE
    here is your nested grouping solved

- now about repeating over the elements that have the same EXPT_ID and STRAIN_CODE
    I use the same technique, note that key() returns a nodeset that you can further restrict
    <xsl:for-each select="key('STRAIN',STRAIN_CODE)[EXPT_ID = $thisExpt]">

As you see, explanation is easy, because you were almost there

For explaining the Muenchian in general, I still thinks Teni's web reference says it all

cheers

Geert
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Mongodb hierarchy 2 59
Create html table using xsl 8 42
Convert XML to excel12book 5 31
xpath sql query 2008 8 58
The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question