www.webdeveloper.com
Results 1 to 2 of 2

Thread: newbie - problem with creating xslt style sheet to transform xml to xhtml

Hybrid View

  1. #1
    Join Date
    Oct 2009
    Posts
    1

    Angry newbie - problem with creating xslt style sheet to transform xml to xhtml

    Hi,

    I want to create a xslt 1.0 style sheet (my code so far see below) to transform an .xml (code see below)to an xhtml document that that uses a .css file (code see below) for final presentation in a web browser.

    The .css and the .xml are not to be modified in any way.

    As you can see, I managed to get the majority of the text to display – even the “stagedir” between the “Speech” instances displays, but where I hit a wall is the display of the “stagedir” when it comes up within the “Speech”.

    As you can see, there are two locations of the stagedir, one where it is within a line (i highlighted it in blue) and which is "Problem 1", and "Problem 2", where the stagedir sits between the lines (I highlighted it in red in the example above).

    xml code:

    Code:
    <?xml version="1.0"?>
    <?xml-stylesheet type="text/xsl" href="xhtml.xsl"?>
    
    <PLAY>
    	<TITLE>The Tragedy of Macbeth</TITLE>
    	<ACT>
     		<TITLE>ACT I</TITLE>
    		<CHARACTERS>
    			<GROUP DESCRIPTION="Generals">
    				<PERSON>MACBETH</PERSON>
    				<PERSON>BANQUO</PERSON>
    			</GROUP>
    	
    			<GROUP DESCRIPTION="Noblemen">
    				<PERSON>ROSS</PERSON>
    				<PERSON>ANGUS</PERSON>
    			</GROUP>
    		
    			<GROUP DESCRIPTION="Witches">
    				<PERSON>FIRST WITCH</PERSON>
    				<PERSON>SECOND WITCH</PERSON>
    				<PERSON>THIRD WITCH</PERSON>
    			</GROUP>
    		</CHARACTERS>
    
    
    <SCENE>
    
    	<TITLE>SCENE III.  A heath near Forres.</TITLE>
    
    	<STAGEDIR>Thunder. Enter the three Witches</STAGEDIR>
    				
    <SPEECH>
    <SPEAKER description="Witches">FIRST WITCH</SPEAKER>
    <LINE>Where hast thou been, sister?</LINE>
    </SPEECH>
    
    <SPEECH>
    <SPEAKER>SECOND WITCH</SPEAKER>
    <LINE>Killing swine.</LINE>
    </SPEECH>
    
    <SPEECH>
    <SPEAKER person="witch">THIRD WITCH</SPEAKER>
    <LINE>Sister, where thou?</LINE>
    </SPEECH>
    
    	<STAGEDIR>Drum within</STAGEDIR>
    
    <SPEECH>
    <SPEAKER person="witch">THIRD WITCH</SPEAKER>
    <LINE>A drum, a drum!</LINE>
    <LINE>Macbeth doth come.</LINE>
    </SPEECH>
    
    <SPEECH>
    <SPEAKER person="all">ALL</SPEAKER>
    <LINE>The weird sisters, hand in hand,</LINE>
    <LINE>Posters of the sea and land,</LINE>
    <LINE>Thus do go about, about:</LINE>
    <LINE>Thrice to thine and thrice to mine</LINE>
    <LINE>And thrice again, to make up nine.</LINE>
    <LINE>Peace! the charm's wound up.</LINE>
    </SPEECH>
    
    	<STAGEDIR>Enter MACBETH and BANQUO</STAGEDIR>
    
    <SPEECH>
    <SPEAKER person="generals">MACBETH</SPEAKER>
    <LINE>So foul and fair a day I have not seen.</LINE>
    </SPEECH>
    
    <SPEECH>
    <SPEAKER person="witch">THIRD WITCH</SPEAKER>
    <LINE>All hail, Macbeth, thou shalt be king hereafter!</LINE>
    </SPEECH>
    
    <SPEECH>
    <SPEAKER person="witch">FIRST WITCH</SPEAKER>
    <LINE>Hail!</LINE>
    </SPEECH>
    
    <SPEECH>
    <SPEAKER person="generals">MACBETH</SPEAKER>
    <LINE>Stay, you imperfect speakers, tell me more:</LINE>
    <LINE>By Sinel's death I know I am thane of Glamis;</LINE>
    <LINE>But how of Cawdor? the thane of Cawdor lives,</LINE>
    <LINE>A prosperous gentleman; and to be king</LINE>
    <LINE>Stands not within the prospect of belief,</LINE>
    <LINE>No more than to be Cawdor. Say from whence</LINE>
    <LINE>You owe this strange intelligence? or why</LINE>
    <LINE>Upon this blasted heath you stop our way</LINE>
    <LINE>With such prophetic greeting? Speak, I charge you.</LINE>
    </SPEECH>
    
    	<STAGEDIR>WITCHES vanish</STAGEDIR>
    
    <SPEECH>
    <SPEAKER person="generals">BANQUO</SPEAKER>
    <LINE>The earth hath bubbles, as the water has,</LINE>
    <LINE>And these are of them. Whither are they vanish'd?</LINE>
    </SPEECH>
    
    	<STAGEDIR>Enter ROSS and ANGUS</STAGEDIR>
    
    <SPEECH>
    <SPEAKER person="noblemen">ANGUS</SPEAKER>
    <LINE>We are sent</LINE>
    <LINE>To give thee from our royal master thanks;</LINE>
    <LINE>Only to herald thee into his sight,</LINE>
    <LINE>Not pay thee.</LINE>
    </SPEECH>
    
    <SPEECH>
    <SPEAKER person="generals">BANQUO</SPEAKER>
    <LINE>What, can the devil speak true?</LINE>
    </SPEECH>
    
    <SPEECH>
    <SPEAKER person="generals">MACBETH</SPEAKER>
    		<LINE><STAGEDIR>Aside</STAGEDIR>  Glamis, and thane of Cawdor!</LINE><LINE>The greatest is behind.</LINE>
    	<STAGEDIR>To ROSS and ANGUS</STAGEDIR><LINE>Thanks for your pains.</LINE>
    	<STAGEDIR>To BANQUO</STAGEDIR><LINE>Do you not hope your children shall be kings,</LINE>
    <LINE>When those that gave the thane of Cawdor to me</LINE>
    <LINE>Promised no less to them?</LINE>
    </SPEECH>
    
    
    <SPEECH>
    <SPEAKER person="generals">MACBETH</SPEAKER>
    		<LINE><STAGEDIR>Aside</STAGEDIR> Two truths are told,</LINE>
    <LINE>As happy prologues to the swelling act</LINE>
    <LINE>Of the imperial theme.--I thank you, gentlemen.</LINE>
    		<LINE><STAGEDIR>Aside</STAGEDIR>  This supernatural soliciting</LINE>
    <LINE>Cannot be ill, cannot be good: if ill,</LINE>
    <LINE>Why hath it given me earnest of success,</LINE>
    <LINE>Commencing in a truth? I am thane of Cawdor:</LINE>
    <LINE>If good, why do I yield to that suggestion</LINE>
    <LINE>Whose horrid image doth unfix my hair</LINE>
    <LINE>And make my seated heart knock at my ribs,</LINE>
    <LINE>Against the use of nature? Present fears</LINE>
    <LINE>Are less than horrible imaginings:</LINE>
    <LINE>My thought, whose murder yet is but fantastical,</LINE>
    <LINE>Shakes so my single state of man that function</LINE>
    <LINE>Is smother'd in surmise, and nothing is</LINE>
    <LINE>But what is not.</LINE>
    </SPEECH>
    
    <SPEECH>
    <SPEAKER person="generals">MACBETH</SPEAKER>
    		<LINE><STAGEDIR>Aside</STAGEDIR>  If chance will have me king, why, chance may crown me,</LINE>
    <LINE>Without my stir.</LINE>
    </SPEECH>
    
    <SPEECH>
    <SPEAKER person="generals">MACBETH</SPEAKER>	
    		<LINE><STAGEDIR>Aside</STAGEDIR>Come what come may,</LINE>
    <LINE>Time and the hour runs through the roughest day.</LINE>
    </SPEECH>
    
    <SPEECH>
    <SPEAKER person="generals">BANQUO</SPEAKER>
    <LINE>Worthy Macbeth, we stay upon your leisure.</LINE>
    </SPEECH>
    
    <SPEECH>
    <SPEAKER person="generals">MACBETH</SPEAKER>
    <LINE>Till then, enough. Come, friends.</LINE>
    </SPEECH>
    
    	<STAGEDIR>Exit</STAGEDIR>
    </SCENE>
    </ACT>
    </PLAY>
    css code:

    Code:
    body {font-family: Arial, sans-serif; color: #000000;}
    h1, h2, h3, h4 {font-family: "Comic Sans MS", cursive; }
    h3,h4 {font-weight: normal;}
    dt {font-weight: bold; }
    dd {font-family: "Times New Roman", serif; font-size: 1.2em; font-style: italic;}
    hr {color: #000000;}
    .act {background-color: #CCCFFF; margin:10px; padding: 10px; width: 75%;}
    .Generals {background-color: #CC99FF; padding-left: 5px;}
    .Noblemen {background-color: #FFFFFF; padding-left: 5px;}
    .Witches {background-color: #99FFCC; padding-left: 5px;}
    .All {background-color: #FF0000; padding-left: 5px;}
    .stagedirection {font-size: 0.85em; font-weight: bold; padding-left: 5px;}
    My xslt code so far:

    Code:
    
    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="xml" encoding="ISO-8859-1" version="1.0" 
        omit-xml-declaration="yes" media-type="text/html" indent="yes" />
    	
      <xsl:template match="/">
        <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
        <head>
          <title>An XSLT style sheet for Macbeth</title>
          <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    	  
          <link rel="stylesheet" type="text/css" href="macbeth.css"/>
        </head>
        <body>
    
    	<h1><xsl:value-of select="PLAY/TITLE" /></h1>
    
    	<h2><xsl:value-of select="PLAY/ACT/TITLE" /></h2>
    
    	<div class="act">
    
    	<h3><xsl:value-of select="PLAY/ACT/SCENE/TITLE" /></h3>
    
    	<hr />
    
    	<h4>Characters</h4> 
    
    	<xsl:for-each select="PLAY/ACT/CHARACTERS/GROUP">
    	<div>
    	    <xsl:choose>
    		  <xsl:when test="@DESCRIPTION='Generals'">
    			<xsl:attribute name="class">Generals</xsl:attribute>
    		  </xsl:when>
    		  <xsl:when test="@DESCRIPTION='Noblemen'">
    			<xsl:attribute name="class">Noblemen</xsl:attribute>
    		  </xsl:when>
    		  <xsl:when test="@DESCRIPTION='Witches'">
    			<xsl:attribute name="class">Witches</xsl:attribute>
    		  </xsl:when>
    		</xsl:choose>
    	
    		<h4><xsl:value-of select="@DESCRIPTION" /></h4>
    	
    		<p><xsl:apply-templates select="PERSON"/></p>
    
    	</div>
    	</xsl:for-each>
    	<hr />
    	
    	<xsl:for-each select="PLAY/ACT/SCENE">
    	<xsl:apply-templates select="STAGEDIR|SPEECH"/>
        	</xsl:for-each>
    
    	
    	
    	</div>
    	</body>
    	</html>
    	</xsl:template>
       	
    	<xsl:template match="PERSON">
       	<xsl:value-of select="."/><br />
       	</xsl:template>
      
      	<xsl:template match="STAGEDIR">
    	<xsl:attribute name="class">stagedirection</xsl:attribute>
    	<p>[<xsl:value-of select="." />]</p>
    	</xsl:template>
    	
    	<xsl:template match="SPEECH">
    	<dt>
    	    <xsl:choose>
    		  <xsl:when test="SPEAKER='FIRST WITCH'">
    			<xsl:attribute name="class">Witches</xsl:attribute>
    		  </xsl:when>
    		  <xsl:when test="SPEAKER='SECOND WITCH'">
    			<xsl:attribute name="class">Witches</xsl:attribute>
    		  </xsl:when>
    		  <xsl:when test="SPEAKER='THIRD WITCH'">
    			<xsl:attribute name="class">Witches</xsl:attribute>
    		  </xsl:when>
    		  <xsl:when test="SPEAKER='ALL'">
    			<xsl:attribute name="class">All</xsl:attribute>
    		  </xsl:when>
    		  <xsl:when test="SPEAKER='MACBETH'">
    			<xsl:attribute name="class">Generals</xsl:attribute>
    		  </xsl:when>
    		  <xsl:when test="SPEAKER='BANQUO'">
    			<xsl:attribute name="class">Generals</xsl:attribute>
    		  </xsl:when>
    		  <xsl:when test="SPEAKER='ROSS'">
    			<xsl:attribute name="class">Noblemen</xsl:attribute>
    		  </xsl:when>
    		  <xsl:when test="SPEAKER='ANGUS'">
    			<xsl:attribute name="class">Noblemen</xsl:attribute>
    		  </xsl:when>
    		 </xsl:choose>
    		 
    		 <p><xsl:value-of select="SPEAKER" /></p>
    		</dt>
    		<xsl:apply-templates select="LINE"/>
    	</xsl:template>
    	
    <xsl:template match="LINE">
    <dd><xsl:value-of select="."/></dd>
    </xsl:template>
    	
    	
    		
      
    </xsl:stylesheet>
    Any help would be really appreciated.

  2. #2
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    not sure 100&#37; what you need.
    some more specific selectors to play with:


    Code:
    <xsl:template match="//STAGEDIR"> any stagedir
    
    <xsl:template match="//LINE/STAGEDIR"> in-line stagedirs
    
    <xsl:template match="//SPEECH/STAGEDIR"> non in-line stagedirs...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles