Combining Data Content

Okay, let's go back the other way. Suppose we need to combine the contents of two different Elements into one. Here are two examples.

First and Last Names to Full Name

This stylesheet does the exact reverse operation of the one in the previous example, combining first and last names into a full name string.

Stylesheet (CombineContent.xsl)
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" version="1.0" encoding="UTF-8"
      indent="yes"/>
  <xsl:template match="/SplitContent">
    <CombinedContent>
      <FullName>
        <xsl:value-of select="FirstName"/>
        <xsl:text> </xsl:text>
        <xsl:value-of select="LastName"/>
      </FullName>
    </CombinedContent>
  </xsl:template>
</xsl:stylesheet>

The use of the FullName start and end tags and the xsl:value-of Element should be fairly intuitive by now. The new thing added in this stylesheet is the xsl:text Element. Under normal circumstances whitespace is stripped from the content of XSLT Elements. There are ways to override this behavior. However, to specifically insert one or more space characters into an Element or Attribute content in the result tree we use xsl:text. Anything within xsl:text, including whitespace, is treated as literal text and written to the result tree.

Converting Time and Zone to ISO 8601

Here's a more involved and more real-world example that uses the same techniques shown above. Many legacy applications maintain the local time of an event. If they maintain the time zone at all, it is probably in a separate field. The ISO 8601 time formats represent local time with an offset from coordinated universal time. For example, 3:15 PM in the U.S. Eastern Standard time zone would be 15:15:00-05:00. For our example, suppose we're processing shipping or customs documents that indicate the time of arrival of an ocean vessel. Our application stores the time of this event using a 24-hour clock and a three-character code for the local time zone. Our ship arrived in New York Harbor at 3:15 in the afternoon.

We want to convert this:

Source (TimeAndZoneToISO8601.xml)
<?xml version="1.0" encoding="UTF-8"?>
<ConvertTime>
  <ArrivalTime>
    <HourOfDay>15:15</HourOfDay>
    <TimeZone>EST</TimeZone>
  </ArrivalTime>
</ConvertTime>

to this:

Result (TimeAndZoneInISO8601.xml)
<?xml version="1.0" encoding="UTF-8"?>
<ConvertedTime>
  <ArrivalTime>15:15:00-05:00</ArrivalTime>
</ConvertedTime>

Here's the stylesheet.

Stylesheet (TimeAndZoneToISO8601.xsl)
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" version="1.0" encoding="UTF-8"
      indent="yes"/>
  <xsl:template match="/ConvertTime/ArrivalTime">
  <ConvertedTime>
    <ArrivalTime>
      <xsl:value-of select="HourOfDay"/>
      <xsl:text>:00</xsl:text>
      <xsl:if test="TimeZone = 'EST'">-05:00</xsl:if>
      <xsl:if test="TimeZone = 'PST'">-08:00</xsl:if>
    </ArrivalTime>
  </ConvertedTime>
  </xsl:template>
</xsl:stylesheet>

We use xsl:text to output the seconds string “:00” to the result tree instead of just using the literal value, though we really don't have to. I've used it here since it seems a bit more readable to me than just inserting the literal value. The feature added in this stylesheet is the xsl:if Element. We perform conditional processing to add an eight-hour offset for West Coast ports using Pacific Standard Time and a five-hour offset for our East Coast ports. The test Attribute on xsl:if is an XPath expression that evaluates to a boolean data type. We test the string content of the TimeZone Element against a literal value. String values such as 'EST' in our example must be enclosed in single quotes since the whole Attribute value is in double quotes.

This is an example of using the procedural programming paradigm in a stylesheet that otherwise follows a rule-based paradigm. Note that the conditional support is fairly meager; there is no if/then/else structure. More complicated conditional processing such as the DOCASE are handled using the xsl:choose, xsl:when, and xsl:otherwise Elements.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset