Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| Search | Today's Posts | Mark Forums Read
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old February 10th, 2006, 04:48 PM
Registered User
 
Join Date: Sep 2005
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default how to use call-template result in a test?

I'd like to call a template and then use the result in a test. I'm trying to make a change from this:

Code:
<xsl:if test="boolean(/Amt)">
  <xsl:element name="DamageAmt">            
    <xsl:call-template name="convertCurrency">
      <xsl:with-param name="currency" select="/Amt">
        </xsl:with-param>
      </xsl:call-template>
    </xsl:element>
</xsl:if>
The convertCurrency template basically just strips out non-numeric characters. I'd like to use the result of that in the 'if test' clause so that if there is no non-numeric data left, the <DamageAmt> element is not output at all. As it stands now, it can create an empty <DamageAmt> element, which errors out because the xsd states that if the element is there, it can't be empty.

I inherited maintenance of this and my knowledge of XSLT is very patchy. I can kind of follow what it's doing and make minor changes but can't figure out how to do something new and different like this.

Thanks
  #2 (permalink)  
Old February 10th, 2006, 05:23 PM
Authorized User
 
Join Date: Nov 2004
Location: , , .
Posts: 81
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to jkmyoung
Default

2 functions I can think of off hand.
1. number()
2. translate()

Say you store the result from call-template in a variable a

<xsl:if test="number($a) = $a">
<xsl:if test=" '' = translate($a, '1234567890', ' ')">

====
<xsl:variable name="a">
  <xsl:call-template name="convertCurrency">
    <xsl:with-param name="currency" select="/Amt"/>
  </xsl:call-template>
</xsl:variable>
<xsl:if test=" '' = translate($a, '1234567890', ' ')">
  <xsl:element name="DamageAmt"/>
</xsl:if>

  #3 (permalink)  
Old February 10th, 2006, 07:21 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Wrap the call-template in a variable:

<xsl:variable name="x">
  <xsl:call-template>
    ...
  </xsl:call-template>
</xsl:variable>

and you can then use $x as a string value, e.g.

<xsl:if test="starts-with($x, 'abc')"

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
  #4 (permalink)  
Old February 13th, 2006, 02:37 PM
Registered User
 
Join Date: Sep 2005
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the replies. I think I'm half-way there, but something still isn't working. I've wrapped the call-template in a variable. I think that's working, because I tested it by sending it a value of 'abc123' and it output the '123'. But it's the test that isn't yet working.

<xsl:variable name="dmgAmt">
  <xsl:call-template name="convertCurrency">
    <xsl:with-param name="currency" select="/Amt">
    </xsl:with-param>
  </xsl:call-template>
</xsl:variable>
<xsl:if test="string-length(normalize-space($dmgAmt)) &gt; 0">
  <xsl:element name="DamageAmt">
    <xsl:value-of select="$dmgAmt"/>
  </xsl:element>
</xsl:if>

I've also tried

<xsl:if test="number($dmgAmt) = $dmgAmt">

Both times, it produced an empty <DamageAmt> tag, when I wanted no <DamageAmt> tag at all.

  #5 (permalink)  
Old February 13th, 2006, 02:58 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Can you show a complete example please (cut down if necessary)? I can't see here what your input is, what the called template is doing, what the output is, or what output you expect.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
  #6 (permalink)  
Old February 13th, 2006, 03:25 PM
Registered User
 
Join Date: Sep 2005
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here's the template:

<xsl:template name="convertCurrency">
        <xsl:param name="currency"/>
        <xsl:variable name="tempCurr" select="substring-before($currency, '.')"/>
        <xsl:choose>
            <xsl:when test="string-length($tempCurr) &gt; 0">
                <xsl:value-of select='translate(substring($tempCurr, 1, 9),"AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWw XxYyZz+-,$","")'/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select='translate(substring($currency, 1, 9),"AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWw XxYyZz+-,$","")'/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

Input snippet:

<Amt>TBD</Amt>

Output snippet:

<DamageAmt></DamageAmt>

Both the input and output snippets are nested in other tags. The desired result in this case would be to NOT have the empty <DamageAmt> tags output at all.

  #7 (permalink)  
Old February 13th, 2006, 06:54 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I can't see the problem in the code you've shown me. But I'm suspicious of things like "the input snippet is nested in other tags". In that case if (boolean(/Amt)) isn't going to work. There's either a bug in your processor, or there's a bug in the code you're not showing me. The latter, from experience, is more likely. As I asked before, please supply a complete example that I can run at home.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference


Similar Threads
Thread Thread Starter Forum Replies Last Post
anyway to call a template with a lower precedence? ojasrege XSLT 3 November 30th, 2007 12:13 PM
xsl template, next result or next 10 results ge_or_gi XSLT 8 April 2nd, 2007 07:01 AM
Using Parameter to call another template mikevn123 XSLT 1 October 27th, 2006 03:08 PM
Using result of one template in another rhajikhani XSLT 1 June 4th, 2006 04:56 PM
import / call / template match Kabe XSLT 3 February 26th, 2004 11:31 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.