The Complete XSLT 2.0 Solution

Here’s how our final stylesheet looks in XSLT 2.0:

<?xml version="1.0"?>
<!-- masterdox5.xsl -->
<xsl:stylesheet version="2.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:states="http://www.usps.com/ncsc/lookups/abbreviations.html"
  exclude-result-prefixes="xs states">

  <xsl:variable name="purchase-orders"
    select="document(/report/po/@filename)/purchase-order"/>

  <xsl:function name="states:getStateName" as="xs:string">
    <xsl:param name="abbr" as="xs:string"/>
    <xsl:variable name="abbreviations" as="xs:string*"
      select="'AL', 'AK', 'AS', 'AZ', 'AR', 'CA', 'CO', 'CT',
  <!-- state abbreviations removed for brevity -->
              'WV', 'WI', 'WY'"/>
    <xsl:variable name="stateNames" as="xs:string*"
      select="'Alabama', 'Alaska', 'American Samoa','Arizona', 
  <!-- state names removed for brevity --> 'West Virginia', 'Wisconsin', 'Wyoming'"/> <xsl:variable name="index" select="if (count(index-of($abbreviations, $abbr)) gt 0) then subsequence(index-of($abbreviations, $abbr), 1, 1) else 0"/> <xsl:value-of select="if ($index gt 0) then string(subsequence($stateNames, $index, 1)) else ''"/> </xsl:function> <xsl:output method="html"/> <xsl:template match="/"> <html> <head> <title><xsl:value-of select="/report/title"/></title> </head> <body style="font-family: sans-serif;"> <h1> Selected Purchase Orders - <span style="font-style: italic; text-decoration: underline;">Grouped</span> by state name </h1> <xsl:for-each-group select="$purchase-orders" ...

Get XSLT, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.