Altova Mailing List Archives


Re: [xsl] Newbie needs xsl

From: Jeni Tennison <mail@---------------->
To:
Date: 2/20/2001 11:53:00 PM
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

Disclaimer

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.