[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Can I add new element?

Subject: Re: Can I add new element?
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Tue, 01 Aug 2000 21:09:34 +0100
add position to element xsl
Yuko,

>I'm trying to generage output into wml.  When a return from database is more
>than 7, new element is added and also insert a new card.  

You say that you've figured out how to split the titles into cards, but
don't know how to insert a 'More' choice when you need to.  How you do it
exactly is  dependent on how you're dividing your input up into cards in
the first place.  Basically, you need to check whether there is another
'title' element to process after the last one you processed.  If there is,
then you want to output a 'More' choice; if not, then you don't.

Here's how I've approached the problem - hopefully the solution will map
onto yours fairly easily.

The first thing I did is create a parameter to hold the magic number 7,
just in case you want to change it in the future:

<xsl:param name="group-size" select="'7'" />

When you have a grouping problem, you need to:
(a) identify the first thing in each group and
(b) identify all the other things in the group, based on knowing the first one

When you're grouping into groups of a certain size based on position, you
can find the first things in each group by looking at the position() of the
thing mod the size of the group.  The first in each group will have a value
of '1', the second a value of '2' and so on.  You want to apply templates
to only these things, in your case 'title' elements:

<xsl:template match="book">
  <wml>
    <xsl:apply-templates select="title[(position() mod $group-size) = 1]" />
  </wml>
</xsl:template>

Now, a 'title'-matching template will only be processed on the first title
in each group.  When the 'title'-matching is processed, the current node
list is comprised of only those title elements that are first in each
group: the position() of the title element within this list gives you the
number of the card to use.

The group consists of the current title (the first in the group) plus the
next 6 titles, in other words, the following sibling titles whose position
is less than the size of the group.  You have a choice for each of those.
Then, if there is a 7th title element following the first in the group,
then that title element is going to be in a new card - in that case, you
need a 'More' choice.

<xsl:template match="title">
  <xsl:variable name="card-no" select="position()" />
  <card id="{$card-no}">
    <select>
      <xsl:for-each select=". | following-sibling::title[position() &lt;
$group-size]">
        <choice><xsl:value-of select="." /></choice>
      </xsl:for-each>
      <xsl:if test="following-sibling::title[position() = $group-size]">
        <choice onpick="#{$card-no + 1}">More</choice>
      </xsl:if>
    </select>
  </card>
</xsl:template>

These templates have been tested and work in SAXON given your output.

I hope that helps,

Jeni

Dr Jeni Tennison
Epistemics Ltd * Strelley Hall * Nottingham * NG8 6PE
tel: 0115 906 1301 * fax: 0115 906 1304 * email: jeni.tennison@xxxxxxxxxxxxxxxx


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.