Vote count:
0
I am learning XML and was going through some exercises I found online and am having difficulty with one small piece. I have the following XML that I want to use XSL to convert to into HTML. I am having trouble with the STAGEDIR element. It is a child element of SCENE and a sibling element to SPEECH and I would like it to appear in the HTML above the speaker's name and his or her line. What happens with the below code is that the same first contents of the STAGEDIR element is being repeated for each SPEECH element. Instead I want to return the contents of the preceding STAGEDIR element of its sibling SPEECH element.
Below is a snippet of the XML
<PLAY>
<TITLE>The Tragedy of Hamlet, Prince of Denmark</TITLE>
<FM>
<P>This work may be freely copied and distributed worldwide.</P>
</FM>
<ACT>
<TITLE>ACT I</TITLE>
<SCENE>
<TITLE>SCENE I. Elsinore. A platform before the castle.</TITLE>
<STAGEDIR>FRANCISCO at his post. Enter to him BERNARDO</STAGEDIR>
<SPEECH>
<SPEAKER>BERNARDO</SPEAKER>
<LINE>Who's there?</LINE>
</SPEECH>
<STAGEDIR>Enter HORATIO and MARCELLUS</STAGEDIR>
<SPEECH>
<SPEAKER>HORATIO</SPEAKER>
<LINE>Friends to this ground.</LINE>
</SPEECH>
</SCENE>
</ACT>
</PLAY>
Below is the XSL
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" />
<xsl:template match="PLAY">
<html>
<head>
<!--Head title is the play title-->
<title><xsl:value-of select="TITLE" /></title>
</head>
<body>
<div class="act">
<xsl:for-each select="ACT">
<h2><xsl:value-of select="TITLE" /></h2>
<xsl:for-each select="SCENE">
<h3><xsl:value-of select="TITLE" /></h3>
<xsl:apply-templates select="SPEECH" />
</xsl:for-each>
</xsl:for-each>
</div>
</body>
</html>
</xsl:template>
<xsl:template match="SPEECH">
<xsl:for-each select=".">
<xsl:if test="../STAGEDIR"> <!--THIS IS WHERE THE PROBLEM OCCURS-->
<p><em><xsl:value-of select="../STAGEDIR" /></em></p>
</xsl:if>
<h4 style="color: red"><xsl:value-of select="SPEAKER" />:</h4>
<p><xsl:if test="STAGEDIR">
(<em><xsl:value-of select="STAGEDIR" /></em>)
</xsl:if>
<xsl:value-of select="LINE" /></p>
</xsl:for-each>
</xsl:template>
Below is a small subset of the HTML
<div class="act">
<h2>ACT I</h2>
<h3>SCENE I. Elsinore. A platform before the castle.</h3>
<p><em> FRANCISCO at his post. Enter to him BERNARDO</em></p>
<h4 style="color: red"> BERNARDO:</h4>
<p>Who's there? </p>
<p><em>FRANCISCO at his post. Enter to him BERNARDO </em></p>
</div>
As you can see <p><em>FRANCISCO at his post. Enter to him BERNARDO </em></p>
is being repeated. In XSL, how can I loop through an element like I do in the XSL with the SPEECH element and return the contents of the preceding sibling element, in this case STAGEDIR.
XSL for-each to get contents of sibling element
Aucun commentaire:
Enregistrer un commentaire