<xsl:when test=""> help with xslt

R

Rupa

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:eek:therwise> SOMETHING HERE THAT I HAVEN'T SORTED OUT YET
</xsl:eek:therwise>
</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!
Rupa
 
P

Patrick TJ McPhee

% <descr>
% <xsl:choose> <xsl:value-of select="body"> </xsl:value-of select>

This value-of is out of place and not well-formed.

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

The value of the test attribute is an XPath expression. You don't
use value-of, you just use the expression directly:

<xsl:when test="body/@size_in_chars > 1042"></xsl:when>

Note the use of @ to get at the attributes axis. The path here is
relative to the current node at the time the xsl:choose is evaluated.
You could use an absolute path (perhaps /body/@size_in_chars), or
a different relative path, depending on what the current node is.

% 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>

You might want to read an introduction to XPath and XSLT. The XPath
spec is fairly readable.


<xsl:when test="body/@size_in_chars > 1042">
<xsl:value-of select='substr(body, 1, 1024)'/>
</xsl:when>

Again, `body' is a relative path and so this won't work if the current
node isn't right.

If all you want to do is truncate if necessary, you could do away with
the xsl:choose and simply use substr() on everything.
 
P

Patrick TJ McPhee

%
% There is no substr function in XSLT....

Excuse me. That should be substring().
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top