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

Re: Formatting multi-line address block to skip blank

Subject: Re: Formatting multi-line address block to skip blank lines in address
From: JBryant@xxxxxxxxx
Date: Mon, 6 Jun 2005 14:27:55 -0500
multi line address
Hi, Jerry,

You can address this problem in at least a couple ways. The string-length 
function (to see if add2 and add3 have any content) lies at the heart of 
several possible solutions. (There's probably a way to do it without the 
string-length function, but I didn't explore that direction.)

Let's assume you have an XML source that looks like this:

<addressblocks>
  <addressblock>
    <name>Sam Spade</name>
    <add1>1 Casablanca Way</add1>
    <add2/>
    <add3/>
    <Location>Sam's Place</Location>
  </addressblock>
  <addressblock>
    <name>Sherlock Holmes</name>
    <add1>221 Baker Street</add1>
    <add2>Flat B</add2>
    <add3/>
    <Location>London</Location>
  </addressblock>
</addressblocks>

You could write a stylesheet like this one:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="addressblocks">
    <html>
      <body>
        <h1 style="text-align: center">Address Information</h1>
        <xsl:apply-templates/>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="addressblock">
    <hr/>
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="name">
    <h3>Name: <xsl:value-of select="."/></h3>
  </xsl:template>

  <xsl:template match="add1">
    <h3>Address</h3>
    <p style="text-indent: .25in"><xsl:value-of select="."/></p>
  </xsl:template>

  <xsl:template match="add2|add3">
    <xsl:if test="string-length(.) &gt; 0">
      <p style="text-indent: .25in"><xsl:value-of select="."/></p>
    </xsl:if>
  </xsl:template>

  <xsl:template match="Location">
    <h3>Location:<xsl:value-of select="."/></h3>
  </xsl:template>

</xsl:stylesheet>

You could also do a "look-ahead" maneuver from the add1 template, which 
would change the add1 and add2/add3 templates thus:

  <xsl:template match="add1">
    <h3>Address</h3>
    <p style="text-indent: .25in"><xsl:value-of select="."/></p>
    <xsl:if test="string-length(../add2) &gt; 0">
      <xsl:apply-templates select="../add2"/>
    </xsl:if>
    <xsl:if test="string-length(../add3) &gt; 0">
      <xsl:apply-templates select="../add3"/>
    </xsl:if>
  </xsl:template>

  <xsl:template match="add2|add3">
      <p style="text-indent: .25in"><xsl:value-of select="."/></p>
  </xsl:template>

And you could do this:

  <xsl:template match="add1">
    <h3>Address</h3>
    <p style="text-indent: .25in"><xsl:value-of select="."/></p>
    <xsl:if test="string-length(../add2) &gt; 0">
      <p style="text-indent: .25in"><xsl:value-of select="../add2"/></p>
    </xsl:if>
    <xsl:if test="string-length(../add3) &gt; 0">
      <p style="text-indent: .25in"><xsl:value-of select="../add3"/></p>
    </xsl:if>
  </xsl:template>

  <xsl:template match="add2|add3"/>

And another way (that doesn't involve tinkering with the add1 template):

  <xsl:template match="add2[string-length(.) &gt; 0]|add3[string-length(.) 
&gt; 0]">
    <p style="text-indent: .25in"><xsl:value-of select="."/></p>
  </xsl:template>

  <xsl:template match="add2[string-length(.) = 0]|add3[string-length(.) = 
0]"/>

I'm sure yet other ways exist. Since you mention blocks, I suppose you are 
producing FO output. However, HTML makes shorter examples, so I used HTML 
as the output.

I tested both the full stylesheet and the last variant with Saxon 8.4. I 
did not test the other possibilities,but they should work, barring typoes 
on my part.

HTH (and let me know, on the list, if not)

Jay Bryant
Bryant Communication Services
(presently consulting at Synergistic Solution Technologies)





"Jerry Johns" <JJohns@xxxxxxxxxxxxx> 
06/06/2005 01:52 PM
Please respond to
xsl-list@xxxxxxxxxxxxxxxxxxxxxx


To
<xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
cc

Subject
Spam: Formatting multi-line address block to skip blank lines in 
address






I have a 5 line address on a form. The 5 lines are:
 
Name
Add1
Add2
Add3
Location
 
I originally put these fields into separate text blocks; however, many
recipients don't have 3 address lines. Most have only 1, but I have to
accomodate more, if they are available.
 
Is there a way to combine all of these fields into a single text block
so that:
 
a) each field is on a separate line
b) if one of the fields is blank, then it is skipped
 
Example:
In this example, Add2 and Add3 have no data. The output would look like:
 
Name
Add1
Location
 
Thanks!!!
Jerry
 

Jerry L. Johns
Allied Business Systems, LLC

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.