Results 1 to 7 of 7

Thread: [RESOLVED] XML - getting a child to use a for-each loop

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2006
    Posts
    28

    Resolved [RESOLVED] XML - getting a child to use a for-each loop

    I have Hamlet in XML. I'm to write a stylesheet. I'm stuck on the <persona> tag. Any help would be greatly appreciated. The "apply-template" doesn't seem to iterate through the <persona> tag and I can't get the for-each loop to work.
    Code:
    <XML>
    
    <?xml version="1.0" encoding="utf-8"?>
    <?xml-stylesheet type="text/xsl" href="hamlet.xsl"?>
    
    
    <PLAY>
    <TITLE>The Tragedy of Hamlet, Prince of Denmark</TITLE>
    
    <FM>
    <P>Text placed in the public domain by Moby Lexical Tools, 1992.</P>
    <P>SGML markup by Jon Bosak, 1992-1994.</P>
    <P>XML version by Jon Bosak, 1996-1998.</P>
    <P>This work may be freely copied and distributed worldwide.</P>
    </FM>
    
    
    <PERSONAE>
    <TITLE>Dramatis Personae</TITLE>
    
    <PERSONA>CLAUDIUS, king of Denmark. </PERSONA>
    <PERSONA>HAMLET, son to the late, and nephew to the present king.</PERSONA>
    <PERSONA>POLONIUS, lord chamberlain. </PERSONA>
    <PERSONA>HORATIO, friend to Hamlet.</PERSONA>
    <PERSONA>LAERTES, son to Polonius.</PERSONA>
    <PERSONA>LUCIANUS, nephew to the king.</PERSONA>
    What I have so far:

    Code:
    <?xml version="1.0"?>
    <xsl:stylesheet  version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    <xsl:template match="/">
        <html>
        <head><title><xsl:value-of select="PLAY/TITLE"/></title></head>
        <body>
        	<xsl:apply-templates select="PLAY"/>
        </body>
        </html>  
    </xsl:template>
    	
    <xsl:template match="PLAY">
    	<xsl:value-of select="FM" /><br /><br />
    	<xsl:value-of select="PERSONAE/TITLE" /><br />
    	<xsl:value-of select="PERSONAE/PERSONA"/>
    
    	
    </xsl:template>	
    </xsl:stylesheet>
    Last edited by penagate; Nov 27th, 2007 at 03:52 AM.

  2. #2
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: XML - getting a child to use a for-each loop

    Without the XML to test it, I would try something like this.
    Code:
    <?xml version="1.0"?>
    <xsl:stylesheet  version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    <xsl:template match="/">
        <html>
        <head><title><xsl:value-of select="PLAY/TITLE"/></title></head>
        <body>
        	<xsl:apply-templates select="PLAY"/>
        </body>
        </html>  
    </xsl:template>
    	
    <xsl:template match="PLAY">
    	<xsl:value-of select="FM" /><br /><br />
    	<xsl:value-of select="PERSONAE/TITLE" /><br />
    	<xsl:call-template name="PERSONA"/>
    </xsl:template>	
    
    
    <xsl:template name="PERSONA">
    	<xsl:for-each select="//PERSONAE/PERSONA">
    		<xsl:value-of select="."/><br />
    	</xsl:for-each>
    </xsl:template>	
    </xsl:stylesheet>

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2006
    Posts
    28

    Re: XML - getting a child to use a for-each loop

    That was great!! but I have one small snag...

    this is more of the xml file..it's long..your example gives me every <persona> tag..not just the ones in the <personae> tag....how do I compensate for that?

    <?xml version="1.0" encoding="utf-8"?>
    <?xml-stylesheet type="text/xsl" href="hamlet.xsl"?>


    <PLAY>
    <TITLE>The Tragedy of Hamlet, Prince of Denmark</TITLE>

    <FM>
    <P>Text placed in the public domain by Moby Lexical Tools, 1992.</P>
    <P>SGML markup by Jon Bosak, 1992-1994.</P>
    <P>XML version by Jon Bosak, 1996-1998.</P>
    <P>This work may be freely copied and distributed worldwide.</P>
    </FM>


    <PERSONAE>
    <TITLE>Dramatis Personae</TITLE>

    <PERSONA>CLAUDIUS, king of Denmark. </PERSONA>
    <PERSONA>HAMLET, son to the late, and nephew to the present king.</PERSONA>
    <PERSONA>POLONIUS, lord chamberlain. </PERSONA>
    <PERSONA>HORATIO, friend to Hamlet.</PERSONA>
    <PERSONA>LAERTES, son to Polonius.</PERSONA>
    <PERSONA>LUCIANUS, nephew to the king.</PERSONA>

    <PGROUP>
    <PERSONA>VOLTIMAND</PERSONA>
    <PERSONA>CORNELIUS</PERSONA>
    <PERSONA>ROSENCRANTZ</PERSONA>
    <PERSONA>GUILDENSTERN</PERSONA>
    <PERSONA>OSRIC</PERSONA>
    <GRPDESCR>courtiers.</GRPDESCR>
    </PGROUP>

    <PERSONA>A Gentleman</PERSONA>
    <PERSONA>A Priest. </PERSONA>

    <PGROUP>
    <PERSONA>MARCELLUS</PERSONA>
    <PERSONA>BERNARDO</PERSONA>
    <GRPDESCR>officers.</GRPDESCR>
    </PGROUP>

    <PERSONA>FRANCISCO, a soldier.</PERSONA>
    <PERSONA>REYNALDO, servant to Polonius.</PERSONA>
    <PERSONA>Players.</PERSONA>
    <PERSONA>Two Clowns, grave-diggers.</PERSONA>
    <PERSONA>FORTINBRAS, prince of Norway. </PERSONA>
    <PERSONA>A Captain.</PERSONA>
    <PERSONA>English Ambassadors. </PERSONA>
    <PERSONA>GERTRUDE, queen of Denmark, and mother to Hamlet. </PERSONA>
    <PERSONA>OPHELIA, daughter to Polonius.</PERSONA>
    <PERSONA>Lords, Ladies, Officers, Soldiers, Sailors, Messengers, and other Attendants.</PERSONA>
    <PERSONA>Ghost of Hamlet's Father. </PERSONA>
    </PERSONAE>

    <SCNDESCR>SCENE Denmark.</SCNDESCR>

    <PLAYSUBT>HAMLET</PLAYSUBT>

    <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>

    <SPEECH>
    <SPEAKER>FRANCISCO</SPEAKER>
    <LINE>Nay, answer me: stand, and unfold yourself.</LINE>
    </SPEECH>

    <SPEECH>
    <SPEAKER>BERNARDO</SPEAKER>
    <LINE>Long live the king!</LINE>
    </SPEECH>

    <SPEECH>
    <SPEAKER>FRANCISCO</SPEAKER>
    <LINE>Bernardo?</LINE>
    </SPEECH>

    <SPEECH>
    <SPEAKER>BERNARDO</SPEAKER>
    <LINE>He.</LINE>
    </SPEECH>

    <SPEECH>
    <SPEAKER>FRANCISCO</SPEAKER>
    <LINE>You come most carefully upon your hour.</LINE>
    </SPEECH>

    <SPEECH>
    <SPEAKER>BERNARDO</SPEAKER>
    <LINE>'Tis now struck twelve; get thee to bed, Francisco.</LINE>
    </SPEECH>

    <SPEECH>
    <SPEAKER>FRANCISCO</SPEAKER>
    <LINE>For this relief much thanks: 'tis bitter cold,</LINE>
    <LINE>And I am sick at heart.</LINE>
    </SPEECH>

    <SPEECH>
    <SPEAKER>BERNARDO</SPEAKER>
    <LINE>Have you had quiet guard?</LINE>
    </SPEECH>

    <SPEECH>
    <SPEAKER>FRANCISCO</SPEAKER>
    <LINE>Not a mouse stirring.</LINE>
    </SPEECH>

    <SPEECH>
    <SPEAKER>BERNARDO</SPEAKER>
    <LINE>Well, good night.</LINE>
    <LINE>If you do meet Horatio and Marcellus,</LINE>
    <LINE>The rivals of my watch, bid them make haste.</LINE>
    </SPEECH>

    <SPEECH>
    <SPEAKER>FRANCISCO</SPEAKER>
    <LINE>I think I hear them. Stand, ho! Who's there?</LINE>
    </SPEECH>


    <STAGEDIR>Enter HORATIO and MARCELLUS</STAGEDIR>

    <SPEECH>
    <SPEAKER>HORATIO</SPEAKER>
    <LINE>Friends to this ground.</LINE>
    </SPEECH>

    <SPEECH>
    <SPEAKER>MARCELLUS</SPEAKER>
    <LINE>And liegemen to the Dane.</LINE>
    </SPEECH>

    <STAGEDIR>Exeunt</STAGEDIR>
    </SCENE>
    </ACT>
    </PLAY>

  4. #4
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: XML - getting a child to use a for-each loop

    All the persona nodes that are appearing are sibling nodes of the parente node. You will notice the persona nodes inside the pgroup nodes are not appearing.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Oct 2006
    Posts
    28

    Re: XML - getting a child to use a for-each loop

    Your right...what a sneaky....!

    Now I got to figure out how to match a persona text in order to display it right...this....

    LUCIANUS, nephew to the king.
    A Gentleman
    A Priest.
    FRANCISCO, a soldier.
    REYNALDO, servant to Polonius.
    Players.
    Two Clowns, grave-diggers.
    FORTINBRAS, prince of Norway.
    A Captain.
    English Ambassadors.
    GERTRUDE, queen of Denmark, and mother to Hamlet.
    OPHELIA, daughter to Polonius.

    should be, I think

    Lucianus, nephew to the king.
    ..
    ..
    PLAYERS
    Two Clowns, grave-diggers.
    ..
    ..
    ENGLISH AMBASSADORS.
    Gertrude, queen of Denmark, andmother to Hamlet.
    ...

    Can I do something like if <persona> = players then all cap??

  6. #6
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: XML - getting a child to use a for-each loop

    Try restating the question. I'm not following what you are asking.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Oct 2006
    Posts
    28

    Re: XML - getting a child to use a for-each loop

    I'm trying to make it more readable...better presentation

Posting Permissions

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



Click Here to Expand Forum to Full Width