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

Rupa (archive)

Usenet Archive
Joined
Jan 7, 2004
Messages
1
Reaction score
0
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
 

Patrick TJ McPhee

Usenet Archive
Joined
Apr 2, 2000
Messages
269
Reaction score
0
% <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.
 

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

No members online now.

Forum statistics

Threads
548,208
Messages
3,104,798
Members
249,594
Latest member
henrywalsh

Latest Threads

Top