This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Newbie needs xsl


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]