简体   繁体   中英

XSL create multiple tables using multiple conditions

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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM