View Single Post
  #1 (permalink)  
Old October 15th, 2007, 05:13 PM
cfflexguy cfflexguy is offline
Registered User
 
Join Date: Oct 2007
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default xslt beginner question

I have xml data that looks like this:

<?xml version="1.0"?>
<book name="psalms">
    <zs id="5919"/>
    <c id='1'/>
    <v id='1'/>
    <q level='1'>Oh, the joys of those who do not</q>
    <q level='2'>follow the advice of the wicked,</q>
    <q level='2'>or stand around with sinners,</q>
    <q level='2'>or join in with mockers.</q>
    <zs id="5920"/>
    <v id='2'/>
    <q level='1'>But they delight in the law of the <nd>lord</nd>,</q>
    <q level='2'>meditating on it day and night.</q>
    <v id='3'/>
    <q level='1'>They are like trees planted along the riverbank,</q>
    <q level='2'>bearing fruit each season.</q>
    <q level='1'>Their leaves never wither,</q>
    <q level='2'>and they prosper in all they do.</q>
    <v id='4'/>
    <q level='1'>But not the wicked!</q>
    <q level='2'>They are like worthless chaff, scattered by the wind.</q>
    <v id='5'/>
    <q level='1'>They will be condemned at the time of judgment.</q>
    <q level='2'>Sinners will have no place among the godly.</q>
    <v id='6'/>
    <q level='1'>For the <nd>lord</nd> watches over the path of the godly,</q>
    <q level='2'>but the path of the wicked leads to destruction.</q>
    <zs id="5923"/>
    <c id='2'/>
    <v id='1'/>
    <q level='1'>Why are the nations so angry?</q>
    <q level='2'>Why do they waste their time with futile plans?</q>
    <v id='2'/>
    <q level='1'>The kings of the earth prepare for battle;</q>
    <q level='2'>the rulers plot together</q>
    <q level='1'>against the <nd>lord</nd></q>
    <q level='2'>and against <nd>his</nd> anointed one.</q>
    <v id='3'/>
    <q level='1'>#8220;Let us break their chains,#8221; they cry,</q>
    <q level='2'>#8220;and free ourselves from slavery to God.#8221;</q>
</book>

that i am trying to transform to xhtml using xsl ... the problem is that we're storing the data by the section, and we have spanning / hanging tag issues if we treat sections, chapters, or verses as containers. in some instances, sections will span chapters or verses... and even in some cases, verses will span chapters where one verse will begin in one chapter, and end in another. so we really can't use sections, chapters, or verses as containers, only as markers.

my issue is that i am able to iterate over the xml if i treat chapter / verse as containers ...using xsl:for-each but i can't figure out how to do it without these nodes as containers.

this is the xsl i'm using:

<xsl:template match="book">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Document</title>
</head>
<body>




<xsl:apply-templates select="zs"/>
    <xsl:for-each select="c">
<span id="c_{normalize-space(@id)}" class="c-num"><xsl:value-of select="normalize-space(@id)"/><xsl:text></xsl:text></span>

        <xsl:for-each select="v">
<span id='v_{normalize-space(@id)}'><xsl:value-of select="normalize-space(@id)"/><xsl:text></xsl:text></span>

            <xsl:for-each select="q">
<span class="q{normalize-space(@level)}"><xsl:value-of select="text()"/></span>

            </xsl:for-each>
        </xsl:for-each>
    </xsl:for-each>
</body>
</html>
</xsl:template>

<xsl:template match="zs">
<span class="zs"><xsl:value-of select="normalize-space(@id)"/><xsl:text></xsl:text></span>
</xsl:template>

</xsl:stylesheet>

The resulting xhtml needs to look like this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Untitled Document</title>
</head>
<body>
<span class="zs">5919</span>
<span class="zs">5923</span>
<span id="c_1" class="c-num">1</span>
<span id="v_1">1</span>
<span class="q1">Oh, the joys of those who do not</span>
<span class="q2">follow the advice of the wicked,</span>
<span class="q2">or stand around with sinners,</span>
<span class="q2">or join in with mockers.</span>
<span id="v_2">2</span>
<span class="q1">But they delight in the law of the <span class="nd">lord</span>,</span>
<span class="q2">meditating on it day and night.</span>
<span id="v_3">3</span>
<span class="q1">They are like trees planted along the riverbank,</span>
<span class="q2">bearing fruit each season.</span>
<span class="q1">Their leaves never wither,</span>
<span class="q2">and they prosper in all they do.</span>
<span id="v_4">4</span>
<span class="q1">But not the wicked!</span>
<span class="q2">They are like worthless chaff, scattered by the wind.</span>
<span id="v_5">5</span>
<span class="q1">They will be condemned at the time of judgment.</span>
<span class="q2">Sinners will have no place among the godly.</span>
<span id="v_6">6</span>
<span class="q1">For the <span class="nd">lord</span></span>
<span class="q2">but the path of the wicked leads to destruction.</span>
<span id="c_2" class="c-num">2</span>
<span id="v_1">1</span>
<span class="q1">Why are the nations so angry?</span>
<span class="q2">Why do they waste their time with futile plans?</span>
<span id="v_2">2</span>
<span class="q1">The kings of the earth prepare for battle;</span>
<span class="q2">the rulers plot together</span>
<span class="q1">against the <span class="nd">lord</span></span>
<span class="q2">and against <span class="nd">his</span> anointed one.</span>
<span id="v_3">3</span>
<span class="q1">&ldquo;Let us break their chains,&rdquo; they cry,</span>
<span class="q2">&ldquo;and free ourselves from slavery to God.&rdquo;</span>
</body>
</html>

thanks for any help you can give!
-jim

Reply With Quote