p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   XSLT (http://p2p.wrox.com/forumdisplay.php?f=86)
-   -   xslt beginner question (http://p2p.wrox.com/showthread.php?t=62911)

cfflexguy October 15th, 2007 05:13 PM

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


mhkay October 15th, 2007 05:29 PM

Your post suggests that you're playing with parallel or overlap markup structures and I'm afraid that's not a game for beginners.

However, you really haven't described your problem clearly enough. I can only guess at the relationship of terms like "chapter" and "section" to elements in your source; I can't tell what the level numbers or ids mean; and I can't work out the logic that makes you put ids 5919 and 5923 at the start of your output but not 5920. Superficially, though, there seems to be a very direct mapping from the <v> and <q> elements in your input to <span> elements in your output.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference

samjudson October 16th, 2007 03:30 AM

I'm not sure if this is what you are trying to produce:

Code:

<xsl:template match="book">
<div>Book: <xsl:value-of select="@name"/></div>
<div>
<xsl:apply-templates/>
</div>
</xsl:template>

<xsl:template match="zs"><span class="zs">Section <xsl:value-of select="@id"/></span></xsl:template>
<xsl:template match="c"><span class="c_{@id}">Chapter <xsl:value-of select="@id"/></span></xsl:template>
<xsl:template match="v"><span class="v_{@id}">Verse <xsl:value-of select="@id"/></span></xsl:template>
<xsl:template match="q"><span class="q_{@level}"><xsl:apply-templates/></span></xsl:template>
<xsl:template match="nd"><span class="nd"><xsl:apply-templates/></span></xsl:template>

/- Sam Judson : Wrox Technical Editor -/

cfflexguy October 16th, 2007 08:51 AM

there are a couple of issues that i'm needing to address.

1) you are correct in saying that i'm dealing with some overlap markup structures. one of the reasons i'm using markers for section / chapter / verse is that throughout the text, there are instances where verses will span chapters, and sections will span several chapters or verses within a chapter. on the xhtml side, i really don't think i need to know much more than where a section, chapter, verse begins... so i opted out of containers for them because of overlap / span issues.

2) i'm needing to actually store the text in smaller chunks, and so i've considered allowing the sections as a container to do that, the idea being that there are fewer instances where sections have overlap issues than there are at the chapter / verse level, and i can address those as i bump into them by managing the xml data manually.

3) all of this text is actually coming from proprietary formatted files from the publishers. none of it is in xml to start with. i've already done a lot of work moving those files to xml using regex and string manipulation. *fun*

-------------------------

so the structure 'should' be:

section(zs)/chapter(c)/verse(v)/content(q)/...

<q> is a tag used for poetry... the 'level' is the level of indention of the poetry. i'm using css classes to manage this within the xhtml post transform. not all of the text actually contains poetry, and so not all of it will have a <q> tag... but for psalms, it is what is used most often.

there is additional markup beyond the <q> tag for additional formatting of the text, as well as things like footnotes which fall within the text.

this is an example with the <nd> tag... which is 'name of deity':

<q level='2'>and against <nd>his</nd> anointed one.</q>

the 'id' attributes within the xml are different depending on the tag. the id for <zs> (section) is a number that i've applied to the section to identify it with a row in a table within my database. each section is a range of verses.

the id for chapter (c) and verse (v) are actually the chapter and verse numbers. i could have used:

<c>1<v>1<q level="1">content</q></v><v>2<q level="2">content of second verse</q></v></c>

as containers ... but needed to address the overlapping verses that occur at the end / beginning of some chapters, so i just used a marker for chapter / verse instead, and used the 'id' attribute so that i could still output the value.

what i'm trying to work out right now ... is that i don't want the sections (<zs>) to appear at the top of the content, i actually want them to appear within the content at the appropriate marker... and ... i can't figure out how to transform the additional markup within the <q> tags. (<nd>, <f> ... etc... ) it keeps coming back blank.

sorry so long winded!
-jim


Quote:

quote:Originally posted by mhkay
 Your post suggests that you're playing with parallel or overlap markup structures and I'm afraid that's not a game for beginners.

However, you really haven't described your problem clearly enough. I can only guess at the relationship of terms like "chapter" and "section" to elements in your source; I can't tell what the level numbers or ids mean; and I can't work out the logic that makes you put ids 5919 and 5923 at the start of your output but not 5920. Superficially, though, there seems to be a very direct mapping from the <v> and <q> elements in your input to <span> elements in your output.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference

cfflexguy October 16th, 2007 09:11 AM

amazing... it looks like that probably produces what i need!

-jim


Quote:

quote:Originally posted by samjudson
 I'm not sure if this is what you are trying to produce:

Code:

<xsl:template match="book">
<div>Book: <xsl:value-of select="@name"/></div>
<div>
<xsl:apply-templates/>
</div>
</xsl:template>

<xsl:template match="zs"><span class="zs">Section <xsl:value-of select="@id"/></span></xsl:template>
<xsl:template match="c"><span class="c_{@id}">Chapter <xsl:value-of select="@id"/></span></xsl:template>
<xsl:template match="v"><span class="v_{@id}">Verse <xsl:value-of select="@id"/></span></xsl:template>
<xsl:template match="q"><span class="q_{@level}"><xsl:apply-templates/></span></xsl:template>
<xsl:template match="nd"><span class="nd"><xsl:apply-templates/></span></xsl:template>

/- Sam Judson : Wrox Technical Editor -/


All times are GMT -4. The time now is 05:33 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.