Re: [xsl] numbering output and other newbie issues

Subject: Re: [xsl] numbering output and other newbie issues
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Wed, 01 Mar 2006 10:56:25 -0500
XSLers,

Does anyone have near to hand any links we can offer to Terry describing the XSLT processing model, or "push", or template matching, or whatever we want to call it? I know there are a multitude of them (I've even written it up myself more than once :-), but can't put my fingers on one suitable for a beginner. The topic seems to be in a Google blind spot -- both too narrow, and too broad, to yield to my pleas. Even Dave Pawson's XSL FAQ suffers from this difficulty: one doesn't know where to look, since this information is so basic it's implicit in just about everything.

Terry -- the short version, in response to your question, is that your theories aren't yet quite correct: the reality is both neater and easier than what you describe.

We'll see what links others can offer on the topic. (Oh: I notice that over on the thread "[xsl] numbering output and other newbie issues", Jon Gorman is helping with this exact same thing today: you could try following that if the context makes any sense.)

Given what they say, it may become clear to you why a template like

<xsl:template match="us">
  <us>
    <xsl:apply-templates/>
  </us>
</xsl:template>

will work, when an xsl:copy-of instruction will not -- and why it won't work if it doesn't, and how to fix it so it does.

If no one can come up with any favorite explanations, I'll do some more digging. We're in about Wave 12 or 14 of XSLT beginners here (assuming we've had two waves a year or so since 1999), and this information has been written up many times.

Cheers,
Wendell

At 10:33 PM 2/28/2006, you wrote:
Wendell,

Thanks for your kind input on my first attempt. Here is my current stage of the xsl. I changed the tags around a bit in the XML file, which I am editing in Adobe InCopy. I find that InCopy has a handy translator from text and paragraph styles to XML tags. Here is a snippet of the current XLM.

Story>
<NameLine>Name _________________________________ Date _______________ Class
_____________________</NameLine>
<topic>Recognizing Sentences and Fragments</topic>
<definition>A sentence is a group of words that expresses a complete thought. A group of words
that expresses an incomplete thought is a sentence fragment.</definition>


<directions>Label each group of words S if it is a sentence or F if it is a fragment.</directions>

<item answer="S"> 1. The fans at the football game cheered wildly</item>
<item answer="F"> 2. Because the weather turned cold.</item>
<item answer="F"> 3. Brought a blanket to the
game.</item>
<item answer="F"> 4. The quarterback for the winning team.</item>
<item answer="S"> 5. My family watched the game from the fifty-yard line.</item>
<NameLine>Name _________________________________ Date _______________ Class
_____________________</NameLine>
<topic>Finding Complete Subjects</topic>
<definition>The subject names the person, place, thing, or idea that the sentence is about.</definition>
<directions> Underline the complete subject in each sentence.</directions>
<item> 1. <answer>Young Albert Einstein</answer> showed an interest in math and
science.</item><item> 2. <answer>His grades in other subjects</answer> were
poor.</item><item> 3. <answer>The future scientist</answer> finished high school and
technical college in Switzerland.</item><item> 4. <answer>The Swiss patent office</answer>
hired Einstein in 1902.</item><item> 5. <answer>Scholarly journals</answer> gave Einstein a
forum for his ideas. </item><item> 6. <answer>A German physics journal</answer> published
some of his articles.</item><item> 7. <answer>These articles</answer> discussed radical
theories about the nature of matter.</item><item> 8. <answer>Publication of these
articles</answer> changed scientists' view of the universe.</item><item> 9. <answer>The
theory of relativity</answer> was Einstein's most important
contribution.</item><item>10. <answer>The Nobel Prize in physics</answer> was awarded to
Einstein in 1921.</item>


Here is the stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="1.0">
<xsl:strip-space elements="*"/>
<xsl:template match="NameLine"/>
<xsl:template match="item"/>
<xsl:template match="directions"/>
<xsl:template match="definition"/>


  <xsl:template match="topic">
  <xsl:text>&#10;</xsl:text>
    <xsl:text>&#10;</xsl:text>
    <xsl:apply-templates/>
    <xsl:text>&#10;</xsl:text>
    </xsl:template>

  <xsl:template match="item">
    <xsl:text>&#10;</xsl:text>
    <xsl:text>&#9;</xsl:text>
    <xsl:number count="item" level="any" from="topic" format="1.&#9;"/>
    <xsl:apply-templates select="answer | attribute::answer" />
   </xsl:template>
</xsl:stylesheet>

This gives me something close to the solution I am looking for. However, some of my answers have text that I have tagged with <us></us> for underscore like this:

<item> 1. Millions of Americans <answer><us>watch</us> displays of fireworks on the
Fourth of July.</answer>


I would like those tags to appear in the output. I have tried <xsl:copy-of select "us" /> in several positions, but the output doesn't change. Here is a snippet of the output:

Recognizing Sentences and Fragments

        1.      S
        2.      F
        3.      F
        4.      F
        5.      S
        6.      F
        7.      F
        8.      F
Finding Complete Subjects

        1.      Young Albert Einstein
        2.      His grades in other subjects
        3.      The future scientist
        4.      The Swiss patent office
        5.      Scholarly journals
        6.      A German physics journal
        7.      These articles
        8.      Publication of these
        articles
        9.      The
            theory of relativity
        10.     The Nobel Prize in physics

I am puzzled by the several random returns left in the output as in answers 8 and 9. But I am feeling good about the script. Here is my understanding of what I am doing in each section.

First I am stripping out white spaces from every element (or trying to!).
Then I am selecting NameLine, Item, definitions, and directions. I think I understand why I am selecting NameLine, definitions, and directions--so they don't appear in the output. I don't understand why I am selecting item. I will try commenting out that line to see if it changes the output.


Then I am selecting topic, adding a return before it and two returns after it. The apply-templates passes all this topic to output--is that right? If not to output, then it goes on...to the next template?

At any rate, I am then selecting the item together with the answers. I am numbering the items from the topic. Then I am selecting out the answers and answer attributes and adding returns and tabs in all the right places. The items themselves get left behind.

I think I got the aha you spoke of. That is, that when you match a node, you are removing it (perhaps to a processing cache of some kind). If you don't do anything with that node, then it remains in process until you call it out, so to speak. If you don't call it it out, it goes away to some cyber graveyard.

Am I getting close to a clearer understanding of the basic process? I have not been able to get at any of the reading you recommend. Many of the on-line sources seem to be about the development of the platform and not a patient training, as you suggest I should try to gain. I am asking for Learning XSLT by Micheal Fitgerald. It looks like a good beginner's guide.


======================================================================
Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
  Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================

Current Thread