0

Okay so I have this so far to create multiple tables.

<xsl:for-each select="report/issue">
            <table id="@name" class="idwb sortable">


            <tr>
                <th class="center">Filename</th>
                <th class="center">Level</th>
                <th class="center">GID</th> 
                <th class="center">Message</th>         
                <th class="center">XPath</th>
                <th class="center">Line Number</th>
                <th class="center">Help</th>                
            </tr>

                <!--xsl:sort select="@filename" order="descending" data-type="text" /-->
                <tr onmouseover="this.style.backgroundColor='#f5f6be';"
                    onmouseout="this.style.backgroundColor= '';">

                    <xsl:attribute name="class">alt_0</xsl:attribute>

                    <td class="center">

                            <a href="{@infocenterURL}"><xsl:value-of select="@filename" /></a>

                    </td>

                    <td align="center">
                        <xsl:value-of select="@level" />
                    </td>

                    <td align="center">
                        <xsl:value-of select="@gid" />
                    </td>
                    <td align="center">
                        <xsl:value-of select="@message" />
                    </td>

                    <td align="center">
                        <xsl:value-of select="@xpath" />
                    </td>

                    <td align="center">
                        <xsl:value-of select="@linenum" />
                    </td>
                    <td alight="center">

                    <a href="{@helplink}">More</a>

                    </td>

                </tr>                   
                </table>
                <br />
                <br />
            </xsl:for-each>             

Nothing that's going to set the world on fire. The issue is this creates a table for every entry, but I want to only make tables for every filename and level and all the entries regarding that filename and level would go under there. Is there anyway to currently do this without using javascript?

XML Example

<issue filename="file.html" 
       gid="506" 
       helplink="www.somewhere.com" 
       infocenterURL="www.somewhere.com" 
       level="Potential Violation" 
       linenum="49" 
       message="stuff nneeds to happen" 
       xpath="/html/body/div[3]/img"/>

What I need to happen is that for every file name there is, I need a table with all the issues that match the same filename and the same violation level. The violation levels are fixed to 5 and I know all of them. But the file names are dynamic.

1 Answers1

0

If you want to do something "for every report" (instead of "for every issue"), then saying <xsl:for-each select="report/issue"> is not going to work.

But I have a few other remarks, too:

  • First off, don't do style changes in response to mouse position with JavaScript. Ever. Use the CSS :hover pseudo class for that, it has been created just for that purpose.
  • The same goes for alternating row colors. CSS has :nth-child(odd) and :nth-child(even) for that.
  • Next, don't use <xsl:for-each>. Use template matching. You will get more modularized, less deeply nested code this way, with templates that you don't need to scroll several screenfuls just to see what they are doing.
  • Explicitly using <thead> and <tbody> is a good idea sometimes.
  • For the sake of consistency: Decide whether you want align="center" or class="center".

Keeping that in mind...

<xsl:template match="/">
  <xsl:apply-templates select="report" />
</xsl:template>

<!-- <report> becomes <table>... -->
<xsl:template match="report">
  <table id="@name" class="idwb sortable">
    <thead>
      <tr>
          <th class="center">Filename</th>
          <th class="center">Level</th>
          <th class="center">GID</th> 
          <th class="center">Message</th>         
          <th class="center">XPath</th>
          <th class="center">Line Number</th>
          <th class="center">Help</th>                
      </tr>
    </thead>
    <tbody>
      <xsl:apply-templates select="issue">
        <xsl:sort select="@filename" order="descending" data-type="text" />
      </xsl:apply-templates>
    </tbody>
  </table>
</xsl:template>

<!-- <issue> becomes <tr>... -->
<xsl:template match="issue">
  <tr class="alt_0">
    <td class="center"><a href="{@infocenterURL}"><xsl:value-of select="@filename" /></a></td>
    <td class="center"><xsl:value-of select="@level" /></td>
    <td class="center"><xsl:value-of select="@gid" /></td>
    <td class="center"><xsl:value-of select="@message" /></td>
    <td class="center"><xsl:value-of select="@xpath" /></td>
    <td class="center"><xsl:value-of select="@linenum" /></td>
    <td class="center"><a href="{@helplink}">More</a></td>
  </tr>
</xsl:template>
Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • Nope it must be every issue. I should of posted an example of the XML, check first post again. – user2983601 Dec 02 '13 at 15:55
  • Ah, I see, so you want to *group by* `@filename`? That's of course something else entirely. The answer to that lies in literally hundreds if not thousands of StackOverflow answers that explain Muenchian Grouping (in case of XSLT 1.0) or the use of `` (in case of XSLT 2.0). – Tomalak Dec 02 '13 at 16:00
  • So if I want to group by filename and violation level that wouldn't be an issue? – user2983601 Dec 02 '13 at 16:02
  • No, it wouldn't. It's a little harder in XSLT 1.0, but definitely possible. Grouping is generally easier in XSLT 2.0. There are several examples of multilevel grouping for both language versions here, too. (If you try and get stuck, you can either update this question with your attempt and ping me or write a new question - I'm just trying to avoid writing *yet another* example of XSLT grouping from scratch.) – Tomalak Dec 02 '13 at 16:07
  • Thanks you've been great but one last question, XSLT 1.0 vs 2.0 compatibility, if I move my code to 2.0 is there any major issues or should I just learn to group with 1.0 – user2983601 Dec 02 '13 at 16:09
  • I think most of your code will just work. There are a few new concepts to learn in XSLT 2.0, though. If you have just a small amount of XSLT to convert and a 2.0-capable processor available, go for it. If your codebase is larger or has external dependencies... It's up to you. 2.0 definitely a lot more powerful than 1.0. Anyway, a while ago I've written a long explanation with regard to grouping and `` in general, maybe it helps you. http://stackoverflow.com/a/955527/18771 (this deals with XSLT 1.0) – Tomalak Dec 02 '13 at 16:16