p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   XSLT (http://p2p.wrox.com/forumdisplay.php?f=86)
-   -   how to use call-template result in a test? (http://p2p.wrox.com/showthread.php?t=38336)

briforge February 10th, 2006 04:48 PM

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

jkmyoung February 10th, 2006 05:23 PM

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>


mhkay February 10th, 2006 07:21 PM

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

briforge February 13th, 2006 02:37 PM

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.


mhkay February 13th, 2006 02:58 PM

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

briforge February 13th, 2006 03:25 PM

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.


mhkay February 13th, 2006 06:54 PM

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


All times are GMT -4. The time now is 08:49 AM.

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