Mega Code Archive

 
Categories / XML / XSLT StyleSheet
 

Following-sibling

File: Data.xml <?xml version="1.0"?> <PLAY>   <TITLE>Nothing</TITLE>   <FM>     <P>       A     </P>     <P>       T     </P>   </FM>   <PERSONAE>     <TITLE>D</TITLE>     <PERSONA>D</PERSONA>     <PGROUP>       <PERSONA>C</PERSONA>       <PERSONA>B</PERSONA>       <GRPDESCR>f</GRPDESCR>     </PGROUP>     <PERSONA>F</PERSONA>     <PERSONA>H</PERSONA>     <PERSONA>B</PERSONA>     <PGROUP>       <PERSONA>MARGARET</PERSONA>       <PERSONA>URSULA</PERSONA>       <GRPDESCR>gentlewomen attending on Hero.</GRPDESCR>     </PGROUP>     <PERSONA>Messengers, Watch, Attendants, &amp;c.</PERSONA>   </PERSONAE> </PLAY> File: Transform.xslt <?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"   version="2.0">   <xsl:template match="SCENE">     <xsl:variable name="flat">       <NL />       <xsl:apply-templates mode="phase1" />     </xsl:variable>     <xsl:apply-templates       select="$flat/NL[following-sibling::node()]" mode="phase2" />   </xsl:template>   <xsl:template match="SPEECH" mode="phase1">     <speaker>       <xsl:value-of select="SPEAKER" />     </speaker>     <xsl:copy-of select="node() except SPEAKER" />   </xsl:template>   <xsl:template match="NL" mode="phase2">     <line>       <xsl:apply-templates         select="following-sibling::node()[1][not(self::NL)]" mode="phase2" />     </line>   </xsl:template>   <xsl:template match="node()" mode="phase2">     <xsl:copy-of select="." />     <xsl:apply-templates       select="following-sibling::node()[1][not(self::NL)]" mode="phase2" />   </xsl:template> </xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?>   Nothing               A                 T                D     D            C       B       f          F     H     B            MARGARET       URSULA       gentlewomen attending on Hero.          Messengers, Watch, Attendants, &amp;c.