Skip to content
This repository has been archived by the owner on Aug 22, 2020. It is now read-only.

XSLT Exercise 3 #566

Closed
BMT45 opened this issue Oct 29, 2018 · 3 comments
Closed

XSLT Exercise 3 #566

BMT45 opened this issue Oct 29, 2018 · 3 comments

Comments

@BMT45
Copy link
Collaborator

BMT45 commented Oct 29, 2018

<xsl:template match="body">
        <tr>
        <td>
            <xsl:apply-templates select="//string"/>
       </td>
        <td>
            <xsl:value-of select="count(//string)"/>
        </td>
    </tr>
        <tr>
             <td>
                <xsl:apply-templates select="//fs[f[@select='Yes']]/f[@select='Yes']/@n"/>
            </td>
            <td>
                <xsl:value-of select="//fs[f[@select='Yes']]/f[@select='Yes']/@n"/>
            </td>
        </tr>
    </xsl:template>
 When I processed this set of code, it will put everything that was found in one table cell and I don't know what I did wrong to get it that way.   I understand how to get the values of each row but I don't know how to isolate them to organize them neatly on the table.
@ebeshero
Copy link
Owner

ebeshero commented Oct 29, 2018

@BMT45 You're getting a single table cell (a "data dump" of everything in one <td> element because you've written this in the special "boilerplate" template that matches on the document node: <xsl:template match="/">. Because there's just one document node, <xsl:apply-templates> runs once in that context and gets ALL the data from every one of the <f> elements you've reached.

Instead, you really want to output a new table row for every <fs> element. To do that, try a new template rule that matches on every <fs> that has a child <f select="Yes"> inside. That's a special template rule that you can design to output a new <tr> (table row) every time it meets an <fs> that contains Yes responses. Because there are like 11 or 12 of those, you'll generate each table row every time the template match applies.

You'll need to restructure the code you wrote for the template matching on the document node. The only table row you'd process there is just ONE, a row that would be your header cells for labels, the one @ghbondar showed in class today:

<tr>
    <th>Number</th>
    <th>Question</th>
    <!--more header cells here to label what will be each column's contents -->
   <th>....</th>
</tr>

Still inside that template match on the document node, you then just want to <xsl:apply-templates select="descendant::fs[f[@select='Yes']]"/>. That will select those elements as the next thing to process at this point.

<xsl:template match="/">

<table>
     <tr>
        <th>....</th>
     </tr>
<xsl:apply-templates select="descendant::fs[f[@select='Yes']]"/>

</table>
</xsl:template>

Then go write a new template rule that matches on the fs[f[@select='Yes']] so it generates a <tr> (with <td> cells inside) every time XSLT finds one of these elements that matches the condition.
Does that make sense?

@BMT45
Copy link
Collaborator Author

BMT45 commented Oct 29, 2018

`    <xsl:template match="/">
    <html>
        <head>
            <title>Sanitary Conditions of Workshops and Factories in New York City</title>
        </head>
        <body>
            <table border="1">
                <tr>
                    <th>Number</th>
                    <th>Question</th>
                    <th>Yes</th>
                    <th>Yes but fined</th>
                    <th>No</th>
                    <th>Blank</th>
                    <th>Total</th>
                </tr>
                <xsl:apply-templates select="//fs/descendant::f[@select='Yes']"/>
            </table>
        </body>
      </html>
     </xsl:template>
    <xsl:template match="fs[f[@select='Yes']]">
        
        
        
        
    </xsl:template>
</xsl:stylesheet>`

So sort of like this to start it off? Then would each table row be put in a different xsl:template element?

@ebeshero
Copy link
Owner

ebeshero commented Oct 29, 2018

@BMT45 Yes! That's a good start! 👍

@ebeshero ebeshero mentioned this issue Oct 31, 2018
@ebeshero ebeshero closed this as completed Jan 6, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants