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

Re: Newbie needs xsl

Subject: Re: Newbie needs xsl
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Wed, 21 Feb 2001 09:00:48 +0000
xsl text xa xsl text
Hi Shailendra,

> I tried decimal-format but could not go far. Can someone please
> suggest an xsl?

Assuming your Source.xml is actually:

<customer_name id="A1"
               revenue="100000"
               office1="3000 sq_ft"
               office2="2000 sq_ft">
  <customer_name id="A2"
                 revenue="80000"
                 office1="560 sq_ft" />
</customer_name A1>

(the source you gave wasn't even close to well-formed XML, so I've had
to make a lot of guesses.  If what you have is different, then do
correct the above.)

For each customer, you first want to give their name, held in the 'id'
attribute:

<xsl:template match="customer_name">
   <!-- Customer Name -->
   <xsl:text>Customer Name: </xsl:text><xsl:value-of select="@id" />
   ...
</xsl:template>

Then their revenue.  At the moment you have it in cents and you need
it formatted in dollars.  So you need to divide the number you have by
100.  Then you can just output that value and I think it will give the
format that you want, but you could alternatively use format-number()
with something like:

  format-number(@revenue div 100, '#0.00')

Anyway, adding the revenue involves:

<xsl:template match="customer_name">
   <!-- Customer Name -->
   <xsl:text>Customer Name: </xsl:text><xsl:value-of select="@id" />
   <!-- Revenue -->
   <xsl:text>&#xA;   Revenue: $</xsl:text>
   <xsl:value-of select="@revenue div 100" />
   ...
</xsl:template>

[Note: the &#xA; in the above gives you a new line.]

For Office1 and Office2, you want the number given before the space,
followed by the string 'sq feet'.  There might not be an Office2, so
you need to check whether there is one with a xsl:if before you start
adding output for it:

<xsl:template match="customer_name">
   <!-- Customer Name -->
   <xsl:text>Customer Name: </xsl:text><xsl:value-of select="@id" />
   <!-- Revenue -->
   <xsl:text>&#xA;   Revenue: $</xsl:text>
   <xsl:value-of select="@revenue div 100" />
   <!-- Office 1 -->
   <xsl:text>&#xA;   Office1: </xsl:text>
   <xsl:value-of select="substring-before(@office1, ' ')" />
   <xsl:text>sq feet</xsl:text>
   <!-- Office 2 -->
   <xsl:if test="@office2">
      <xsl:text>&#xA;   Office2: </xsl:text>
      <xsl:value-of select="substring-before(@office2, ' ')" />
      <xsl:text>sq feet</xsl:text>
   </xsl:if>
   ...
</xsl:template>

Finally, if you have a customer_name element nested inside a
customer_name element, then you want to output that one.  You can
simply apply templates to it.  The customer_name element for the
inner customer will be matched by the same template.  If it doesn't
have any content, then there will be nothing to apply templates to and
the process will stop:

<xsl:template match="customer_name">
   <!-- Customer Name -->
   <xsl:text>Customer Name: </xsl:text><xsl:value-of select="@id" />
   <!-- Revenue -->
   <xsl:text>&#xA;   Revenue: $</xsl:text>
   <xsl:value-of select="@revenue div 100" />
   <!-- Office 1 -->
   <xsl:text>&#xA;   Office1: </xsl:text>
   <xsl:value-of select="substring-before(@office1, ' ')" />
   <xsl:text>sq feet</xsl:text>
   <!-- Office 2 -->
   <xsl:if test="@office2">
      <xsl:text>&#xA;   Office2: </xsl:text>
      <xsl:value-of select="substring-before(@office2, ' ')" />
      <xsl:text>sq feet</xsl:text>
   </xsl:if>
   <!-- More Customers -->
   <xsl:apply-templates />
</xsl:template>

This might not be quite what you're after because of differences in
indentation and so on with the inner customer, but I'm not even sure
that you want textual output, so this is as far as I'll go for now.
If you need more help with the details then let us know.

I hope that helps,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/



 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.