Re: [xsl] numbering output and other newbie issues

Subject: Re: [xsl] numbering output and other newbie issues
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Mon, 27 Feb 2006 12:41:01 -0500
Dear Terry,

Your new problem suggests you might want to match at the level of Items, not of Answers, since you want one listing for every Item, not for every answer as you first had it.

It's not really complicated at all -- actually it's about as simple as your first problem.

But to solve it, you need to understand what the processor is doing with your templates.

I'd suggest running a stylesheet that has absolutely no templates at all. Then introduce a template just to match something out near the branches or leaves of the tree (the Item or Answer will do), *without* your template matching the root (that selects your elements from the three), and see if you can explain what is happening when you run that.

Once you figure this out (and there's lots of helpful literature on the topic), problems like your new one become trivially easy.

To give you another way into this mystery, here's a stylesheet that *almost* does what you're asking for. When you can explain how this works, you'll be in good shape:

<xsl:stylesheet version="1.0"

<xsl:strip-space="Story GrammarQuiz"/>

<xsl:template match="GrammarTopic"/>

<xsl:template match="Directions"/>

<xsl:template match="Item">
  <xsl:number level="any" format="1.&#9;"/>
  <xsl:apply-templates select="Answer"/>


The addition of just a single template to this will get you what you've asked for.

But you'll need to understand why this works before it becomes obvious what that template should do.

Then, XSLT becomes easy and fun. Without this understanding, it remains bothersome and intractable. Nor will the XSLT Cookbook or any "bottom-up" learning approach (copying code and modifying it) help very much till you have this "Aha".

I gave you more hints in the last email, too.


At 09:48 PM 2/24/2006, you wrote:
Thanks Wendell,

Your suggestion worked like a charm. I added some tabs and newline characters as needed for my purposes. There is one more aspect of this project that has me really puzzled. Suppose that some quiz items have more than one answer. I changed item 3 of the the XML file to represent this situation.

<GrammarTopic>Finding Subjects</GrammarTopic>
<Directions>Underline the subject in each sentence. If the subject is an understood you, write you.?</Directions>
<Item>1. Find your seats quickly.<Answer>you</Answer></Item>
<Item>2. During open auditions there are very few empty <Answer>seats</Answer> in the auditorium.</Item>
<Item>3. Have <Answer>you</Answer> and <Answer>Marsha</Answer> heard the director's name?</Item>

Here is my current stylesheet.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl=""; version="1.0">

<xsl:template match="/">
                <xsl:apply-templates select="Story/GrammarTopic" />
                <xsl:apply-templates select="Story/GrammarQuiz/Item/Answer"/>

<xsl:template match="Answer">
  <xsl:number level="any" format="1.&#9;"/>

This works great except, of course, it numbers all the answers consecutively. The output looks like this:

Finding Subjects
        1.      you
        2.      seats
        3.      you
        4.      Marsha

What I want to do now is get both answers for item 3 ("you" and "Marsha") together on one line like this.

Finding Subjects
        1.      you
        2.      seats
        3.      you, Marsha

I have a feeling that this will involve some complicated routines. I have reviewed The XSLT Cookbook and several other resources, but I am not even sure where to start.

Again, any help will be greatly appreciated.


Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.      
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