<xsl:when test="..."> xslt

Hi,

I'm trying to write an xslt to convert an email in xml format to a new xml format.

<descr>
<xsl:choose> <xsl:value-of select="body"> </xsl:value-of select>
<xsl:when test=" <xsl:value-of select="body/size_in_chars"> > 1042"></xsl:when>
<xsl:otherwise> SOMETHING HERE THAT I HAVEN'T SORTED OUT YET </xsl:otherwise>
</xsl:choose>
</descr>

My first problem is the test condition. I need to check whether the size of the body of the email is less than 1024. In the original xml document, it looks like this:
<body size_in_chars="someNumber"> body of email is here </body>

Is there a way for me to access the size_in_chars element and use it in my test?

My second problem is that if the body is greater than 1024, I need to truncate the body after that and select only the first 1024 characters for <descr>. Something like this:

<descr> <xsl:value-of select=truncate("body", 1024)> </xsl:value-of> </descr>

(I'm just using truncate() as a way to show what I need to do - don't know if there actually is a function to do that and what it would be called if it exists.)

Thanks a ton!
Rupa
:)
[1329 byte] By [rsuchak] at [2007-11-18 15:30:08]
# 1 Re: <xsl:when test="..."> xslt
<descr>
<xsl:choose> <xsl:value-of select="body"> </xsl:value-of select>
<xsl:when test=" <xsl:value-of select="body/size_in_chars"> > 1042"></xsl:when>
<xsltherwise> SOMETHING HERE THAT I HAVEN'T SORTED OUT YET </xsltherwise>
</xsl:choose>
</descr>

There are quite a few errors in this code. You might want to get an editor that does syntax highlighting, to help you catch some of these.

Firstly

<xsl:value-of select="body"> </xsl:value-of select> is not valid xml, the end tag can't contain attributes, so it should be
<xsl:value-of select="body"> </xsl:value-of>
Or more simply
<xsl:value-of select="body"/>

Second

<xsl:when test=" <xsl:value-of select="body/size_in_chars"> > 1042"></xsl:when>

Is invalid because attributes can't contain xml elements, and the charactes > < are not allowed in xml attributes or text nodes in general. What you want is something like
<xsl:when test="number(body/@size_in_chars) < 1024"><xsl:when>

In addition I would think that <xsl:value-of select="body"/> should have been placed inside the xsl:when element. I don't think it's valid in xslt, to have non xsl:when element in an xsl:choose element.
khp at 2007-11-10 3:28:27 >
# 2 Re: <xsl:when test="..."> xslt
Originally posted by rsuchak

My second problem is that if the body is greater than 1024, I need to truncate the body after that and select only the first 1024.
:)

You can use the substring function to cut up your original string http://www.w3.org/TR/xpath#function-substring
khp at 2007-11-10 3:29:29 >
# 3 Re: <xsl:when test="..."> xslt
thanks a ton!

you rock! :thumb:
rsuchak at 2007-11-10 3:30:28 >
# 4 Re: <xsl:when test="..."> xslt
from rsuchak via PM
for substring to work, it seems i either i have to have the string either explicitly stated, or have a variable containing it. this is how i've done it:

<xsl:value-of select="substring(entireBody, 0, 1024)"> </xsl:value-of>

where i have set entireBody to <xsl:value-of select="body"></xsl:value-of>

what if i didn't want to set a variable? is there a way to nest the <xsl:value-of> statements so that i can just put that directly into substring??
khp at 2007-11-10 3:31:33 >
# 5 Re: <xsl:when test="..."> xslt
As I stated in my post above you can't have xml elements in xml attributes, so you can't embed <xsl:value-of elements in select attribute values.

You have three options

1. String literals (as you stated)
2. xsl variable (as you stated)
3. xpath expressions in the arguments of the substring function should also be possible, like this

<xsl:value-of select="substring(body/text(),0,1024)"/>
khp at 2007-11-10 3:32:32 >
# 6 Re: <xsl:when test="..."> xslt
thanks a lot! :)
rsuchak at 2007-11-10 3:33:26 >