By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,077 Members | 1,790 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,077 IT Pros & Developers. It's quick & easy.

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

P: n/a
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!
Rupa
Jul 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
In article <df*************************@posting.google.com> ,
Rupa <ru*********@metier.com> wrote:

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

Patrick TJ McPhee
East York Canada
pt**@interlog.com
Jul 20 '05 #2

P: n/a

There is no substr function in XSLT...

Unregistered
-----------------------------------------------------------------------
Posted via http://www.forum4designers.co
-----------------------------------------------------------------------
View this thread: http://www.forum4designers.com/message29485.htm

Jul 20 '05 #3

P: n/a
In article <Gu**********@mail.forum4designers.com>,
Unregistered <Gu**********@mail.forum4designers.com> wrote:
%
% There is no substr function in XSLT....

Excuse me. That should be substring().

--

Patrick TJ McPhee
East York Canada
pt**@interlog.com
Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.