Search code examples
xmlxslttei

Outputting HTML from multiple for-each XSLT segments


I want to show all the lines for a particular character in a stageplay. For that I need to nest multiple for-each statements to draw out the correct data.

An excerpt from the XML file I am working with:

<TEI xml:id="E850003-105">
    <text n="E850003-105">
        <front>
            <pb n="370"/>
            <div>
                <head>The persons of the play</head>
                <list>
                    <item>LORD WINDERMERE.</item>
                    <item>LORD DARLINGTON.</item>
                    <item>LORD AUGUSTUS LORTON.</item>
                    <item>MR. DUMBY.</item>
                    <item>MR. CECIL GRAHAM.</item>
                    <item>MR. HOPPER.</item>
                    <item>PARKER, Butler.</item>
                </list>
                <list>
                    <item>LADY WINDERMERE.</item>
                    <item>THE DUCHESS OF BERWICK.</item>
                    <item>LADY AGATHA CARLISLE.</item>
                    <item>LADY PLYMDALE.</item>
                    <item>LADY STUTFIELD.</item>
                    <item>LADY JEDBURGH.</item>
                    <item>MRS. COWPER-COWPER.</item>
                    <item>MRS. ERLYNNE.</item>
                    <item>ROSALIE, Maid.</item>
                </list>
            </div>
        </front>
        <body>
            <head>Lady Windermere's Fan</head>
            <div1 n="1" type="act">
                <head>ACT ONE</head>
                <stage type="setting">SCENE:<view>
                        <emph>Morning-room of Lord Windermere's house in Carlton House Terrace,
                            London. The action of the play takes place within twenty-four hours,
                            beginning on a Tuesday afternoon at five o'clock, and ending the next
                            day at 1.30 p.m. TIME: The present. Doors C. and R. Bureau with books
                            and papers R. Sofa with small tea-table L. Window opening on to terrace
                            L. Table R. LADY WINDERMERE is at table R., arranging roses in a blue
                            bowl.</emph>
                    </view>
                </stage>
                <stage type="entrance">
                    <emph>Enter</emph> PARKER.</stage>
                <sp>
                    <speaker>PARKER</speaker>
                    <p>Is your ladyship at home this afternoon?</p>
                </sp>

I need to output all the lines spoken by Lord Darlington, meaning the speaker element in the sp elements needs to have that value. So far I have this code in my xsl file:

<xsl:template match="/">
        <html>
            <head>
                <title>LORD DARLINGTON LINES</title>
            </head>
            <body>
                <ul>
                    <xsl:for-each select="/TEI/text/body/div1">
                        <xsl:for-each select="stage">
                            <xsl:for-each select="sp[speaker = 'LORD DARLINGTON']">
                                <li><xsl:value-of select="p"/></li>
                            </xsl:for-each>
                        </xsl:for-each>
                    </xsl:for-each>
                </ul>               
            </body>
        </html>
    </xsl:template>

But for my output I just get a body tag with an empty ul tag. I don't see what is wrong in my code. Does any unexpected problems arise when nesting for-each loops like this in XSLT?


Solution

  • Isn't this simply:

        <xsl:template match="/">
            <html>
                <head>
                    <title>LORD DARLINGTON LINES</title>
                </head>
                <body>
                    <ul>
                        <xsl:for-each select="//sp[speaker = 'LORD DARLINGTON']">
                           <li><xsl:value-of select="p"/></li>
                        </xsl:for-each>
                    </ul>               
                </body>
            </html>
        </xsl:template>