9.2 Alphabetical Lists

Instead of decimal numbers, you can also use alphabetical characters in lists generated by the number element. To get this to work, just change the digit in the format attribute value to a single letter a for lowercase and A for uppercase. For example, alpha.xsl, shown in Example 9-9, uses a lowercase a.

Example 9-9. A stylesheet for alphabetical formatting
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/>      <xsl:template match="provinces">    <xsl:apply-templates select="name"/>  </xsl:template>      <xsl:template match="name">   <xsl:number format="&#32;a.&#32;&#32;"/>   <xsl:value-of select="."/>   <xsl:text>&#10;</xsl:text>  </xsl:template>     </xsl:stylesheet>

Preceding and following the lowercase letter a are spaces defined by decimal character references (&#32;). You also could have used literal characters (just plain spaces) or hexadecimal character references (the hex reference for a space is &#x20;). I use character references here in format because I think it's easier to see where the whitespaces are rather than the literal spaces, but you're free to choose which way you want to do it it's a matter of personal style.

When you transform canada.xml against alpha.xsl with:

xalan canada.xml alpha.xsl

you get Example 9-10.

Example 9-10. The results of using the number element with alphabetical formatting
 a.  Alberta  b.  British Columbia  c.  Manitoba  d.  New Brunswick  e.  Newfoundland and Labrador  f.  Northwest Territories  g.  Nova Scotia  h.  Nunavut  i.  Ontario  j.  Prince Edward Island  k.  Quebec  l.  Saskatchewan  m.  Yukon

9.2.1 Using Uppercase

If you prefer uppercase characters in your list, use an uppercase A in format instead of a lowercase a, as shown in upper-alpha.xsl, which is not shown here but is in examples/ch09. When you transform canada.xml with upper-alpha.xsl, you will see this generate a list ordered with uppercase letters rather than lowercase.

9.2.2 Longer Alphabetical Lists

What happens when an alphabetical list is longer than the English alphabet, that is, longer than 26 items? XSLT generates repeat characters, that is, lowercase, a, b, c...x, y, z is followed by aa, ab, ac, then followed by ba, bb, bc, then ca, cb, cc, and so on.

In Example 9-11, the document us.xml alphabetically lists all 50 states of the United States of America.

Example 9-11. A list of all the United States in XML
<?xml version="1.0"?>     <us>  <state>Alabama</state>  <state>Alaska</state>  <state>Arizona</state>  <state>Arkansas</state>  <state>California</state>  <state>Colorado</state>  <state>Connecticut</state>  <state>Delaware</state>  <state>Florida</state>  <state>Georgia</state>  <state>Hawaii</state>  <state>Idaho</state>  <state>Illinois</state>  <state>Indiana</state>  <state>Iowa</state>  <state>Kansas</state>  <state>Kentucky</state>  <state>Louisiana</state>  <state>Maine</state>  <state>Maryland</state>  <state>Massachusetts</state>  <state>Minnesota</state>  <state>Michigan</state>  <state>Mississippi</state>  <state>Missouri</state>  <state>Montana</state>  <state>Nebraska</state>  <state>Nevada</state>  <state>New Hampshire</state>  <state>New Jersey</state>  <state>New Mexico</state>  <state>New York</state>  <state>North Carolina</state>  <state>North Dakota</state>  <state>Oklahoma</state>  <state>Oregon</state>  <state>Ohio</state>  <state>Pennsylvania</state>  <state>Rhode Island</state>  <state>South Carolina</state>  <state>South Dakota</state>  <state>Tennessee</state>  <state>Texas</state>  <state>Utah</state>  <state>Vermont</state>  <state>Virginia</state>  <state>Washington</state>  <state>West Virginia</state>  <state>Wisconsin</state>  <state>Wyoming</state> </us>

The stylesheet us.xsl uses number to list the states in us.xml using lowercase letters:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/>      <xsl:template match="us">    <xsl:apply-templates select="state"/>  </xsl:template>      <xsl:template match="state">   <xsl:number format="&#x0020;a.&#x0009;"/>   <xsl:value-of select="."/>   <xsl:text>&#10;</xsl:text>  </xsl:template>     </xsl:stylesheet>

The format attribute in this stylesheet uses two longer hexadecimal character references, one for space (&#x0020;) and another for tab (&#x0009;). The number 32 in decimal is equivalent to 20 in hexadecimal; the numbers 0 through 9 are represented identically in decimal and hexadecimal. You can drop the leading zeros if you want, and write the references as &#x20; and &#x9;.

Apply the stylesheet us.xsl to the document us.xml with:

xalan us.xml us.xsl

and you will see the following results in Example 9-12.

Example 9-12. An alphabetically ordered list of all the U.S. states
 a.     Alabama  b.     Alaska  c.     Arizona  d.     Arkansas  e.     California  f.     Colorado  g.     Connecticut  h.     Delaware  i.     Florida  j.     Georgia  k.     Hawaii  l.     Idaho  m.     Illinois  n.     Indiana  o.     Iowa  p.     Kansas  q.     Kentucky  r.     Louisiana  s.     Maine  t.     Maryland  u.     Massachusetts  v.     Minnesota  w.     Michigan  x.     Mississippi  y.     Missouri  z.     Montana  aa.    Nebraska  ab.    Nevada  ac.    New Hampshire  ad.    New Jersey  ae.    New Mexico  af.    New York  ag.    North Carolina  ah.    North Dakota  ai.    Oklahoma  aj.    Oregon  ak.    Ohio  al.    Pennsylvania  am.    Rhode Island  an.    South Carolina  ao.    South Dakota  ap.    Tennessee  aq.    Texas  ar.    Utah  as.    Vermont  at.    Virginia  au.    Washington  av.    West Virginia  aw.    Wisconsin  ax.    Wyoming


Learning XSLT
Learning XSLT
ISBN: 0596003277
EAN: 2147483647
Year: 2003
Pages: 164

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net