Solved

Muenchian grouping and knowing selects inside a group

Posted on 2006-11-21
2
252 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now