473,387 Members | 1,592 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

XSLT: xsl:variable : help!

The following code does not work:

<xsl:variable name="width" select="form:image-width"/>
<xsl:if test="$width>$max_image_width">
<xsl:variable name="width" select="$max_image_width"/>
</xsl:if>
<img class="admin" width="{$width}" src="{form:image-url}" />

The width attribute of img is always "", the $max_image_width variable is set to 200
and $width is initially set to the width of the image.

The purpose of this code is to bound the image width in an html page to a maximum length.
But the code does not work.

Please help!

Pseudo-code of the xsl code:

$max_image_width=200;
$width=[some value selected from xml file]

if($width>$max_image_width) {
$width=$max_image_width
}

print $width;

Jul 20 '05 #1
10 3785
> <xsl:variable name="width" select="form:image-width"/>
<xsl:if test="$width>$max_image_width">
<xsl:variable name="width" select="$max_image_width"/>
</xsl:if>
<img class="admin" width="{$width}" src="{form:image-url}" />

A variable cannot be declared twice in the same scope in XSLT.
And the 'xsl:if' and 'xsl:variable' nodes must be inversed:

<xsl:variable name="width">
<xsl:if test="form:image-width &gt; $max_image_width">
<xsl:value-of select="$max_image_width"/>
</xsl:if>
<xsl:if test="form:image-width &lt;= $max_image_width">
<xsl:value-of select="form:image-width"/>
</xsl:if>
</xsl:variable>
--
Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041)
Ceterum censeo XML omnibus esse utendum
Jul 20 '05 #2
Joris Gillis wrote:
<xsl:variable name="width" select="form:image-width"/>
<xsl:if test="$width>$max_image_width">
<xsl:variable name="width" select="$max_image_width"/>
</xsl:if>
<img class="admin" width="{$width}" src="{form:image-url}" />

A variable cannot be declared twice in the same scope in XSLT.
And the 'xsl:if' and 'xsl:variable' nodes must be inversed:

<xsl:variable name="width">
<xsl:if test="form:image-width &gt; $max_image_width">
<xsl:value-of select="$max_image_width"/>
</xsl:if>
<xsl:if test="form:image-width &lt;= $max_image_width">
<xsl:value-of select="form:image-width"/>
</xsl:if>
</xsl:variable>


Ok ive this:

<xsl:variable name="width">
<xsl:choose>
<xsl:when test="form:image-width &gt; $max_image_width">
<xsl:value-of select="$max_image_width"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="form:image-width"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>

<img class="admin" width="{$width}" src="{form:image-url}" />
But is does not work, the width atribute stays empty, so $width is not assigned a value.

Can't is use > in place of &gt; ??? and for &lt; is < not a possibility?

I think the parser can see whether > is part of markup or an expression
Jul 20 '05 #3
Tjerk Wolterink wrote:
Joris Gillis wrote:
<xsl:variable name="width" select="form:image-width"/>
<xsl:if test="$width>$max_image_width">
<xsl:variable name="width" select="$max_image_width"/>
</xsl:if>
<img class="admin" width="{$width}" src="{form:image-url}" />

A variable cannot be declared twice in the same scope in XSLT.
And the 'xsl:if' and 'xsl:variable' nodes must be inversed:

<xsl:variable name="width">
<xsl:if test="form:image-width &gt; $max_image_width">
<xsl:value-of select="$max_image_width"/>
</xsl:if>
<xsl:if test="form:image-width &lt;= $max_image_width">
<xsl:value-of select="form:image-width"/>
</xsl:if>
</xsl:variable>


Ok ive this:

<xsl:variable name="width">
<xsl:choose>
<xsl:when test="form:image-width &gt; $max_image_width">
<xsl:value-of select="$max_image_width"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="form:image-width"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>

<img class="admin" width="{$width}" src="{form:image-url}" />
But is does not work, the width atribute stays empty, so $width is not
assigned a value.

Can't is use > in place of &gt; ??? and for &lt; is < not a possibility?

I think the parser can see whether > is part of markup or an expression

Wait: it did work, the element form:image-width was nameed form:width , so stupid fault.
Jul 20 '05 #4
> Can't is use > in place of &gt; ??? and for &lt; is < not a possibility?

According to the XPath 1.0 recommandation:

<cite>
XPath expressions often occur in XML attributes. The grammar specified in this section applies to the attribute value after XML 1.0 normalization. So, for example, if the grammar uses the character <, this must not appear in the XML source as < but must be quoted according to XML 1.0 rules by, for example, entering it as &lt;
</cite>

--
Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041)
Ceterum censeo XML omnibus esse utendum
Jul 20 '05 #5
On Wed, 24 Nov 2004 18:11:54 GMT, "Joris Gillis" <ro**@pandora.be>
wrote:
Can't is use > in place of &gt; ??? and for &lt; is < not a possibility?


According to the XPath 1.0 recommandation:

<cite>
XPath expressions often occur in XML attributes. The grammar specified in this section applies to the attribute value after XML 1.0 normalization. So, for example, if the grammar uses the character <, this must not appear in the XML source as < but must be quoted according to XML 1.0 rules by, for example, entering it as &lt;
</cite>


On the other hand, there's no technical reason to use &gt; instead of >.
Some people like to use &gt; for symmetry with <, or so they don't have
to remember which one needs to be escaped.

Personally, for readability I prefer to turn the expression around to
avoid escaping, e.g.

<xsl:if test="$x >= $y"> instead of <xsl:if test="$y &lt; $x">
--
Morris M. Keesan -- ke****@alum.bu.edu

Jul 20 '05 #6
Morris M. Keesan wrote:
On Wed, 24 Nov 2004 18:11:54 GMT, "Joris Gillis" <ro**@pandora.be>
wrote:
Can't is use > in place of &gt; ??? and for &lt; is < not a possibility?


According to the XPath 1.0 recommandation:

<cite>
XPath expressions often occur in XML attributes. The grammar specified in this section applies to the attribute value after XML 1.0 normalization. So, for example, if the grammar uses the character <, this must not appear in the XML source as < but must be quoted according to XML 1.0 rules by, for example, entering it as &lt;
</cite>

On the other hand, there's no technical reason to use &gt; instead of >.
Some people like to use &gt; for symmetry with <, or so they don't have
to remember which one needs to be escaped.

Personally, for readability I prefer to turn the expression around to
avoid escaping, e.g.

<xsl:if test="$x >= $y"> instead of <xsl:if test="$y &lt; $x">


But do all xml parses and xslt processor work with both &gt; and > ?
I mean: is it valid to use > in stead of &gt;
Jul 20 '05 #7
In article <co**********@netlx020.civ.utwente.nl>,
Tjerk Wolterink <tj***@wolterinkwebdesign.com> wrote:
But do all xml parses and xslt processor work with both &gt; and > ?
I mean: is it valid to use > in stead of &gt;


Yes, in the context of attribute values there is no difference between
the two.

The only case where there is a difference is when the > could terminate
a CDATA section: ]]> is different from ]]&gt; (and ]]> is disallowed in
content when it doesn't end a CDATA section).

-- Richard
Jul 20 '05 #8
On 25 Nov 2004 22:19:19 GMT, ri*****@cogsci.ed.ac.uk (Richard Tobin)
wrote:
In article <co**********@netlx020.civ.utwente.nl>,
Tjerk Wolterink <tj***@wolterinkwebdesign.com> wrote:
But do all xml parses and xslt processor work with both &gt; and > ?
I mean: is it valid to use > in stead of &gt;


Yes, in the context of attribute values there is no difference between
the two.

The only case where there is a difference is when the > could terminate
a CDATA section: ]]> is different from ]]&gt; (and ]]> is disallowed in
content when it doesn't end a CDATA section).


But, just to be clear, in a CDATA section, &gt; is 4 characters, not
one, and if you have the character sequence "]]>", you can't represent
it in a CDATA section. You'd have to end the CDATA, and put part of
that sequence in plain parsed character data, or in an adjacent CDATA.
--
Morris M. Keesan -- ke****@alum.bu.edu

Jul 20 '05 #9
In article <mh********************************@4ax.com>,
Morris M. Keesan <ke****@alum.bu.edu> wrote:
But, just to be clear, in a CDATA section, &gt; is 4 characters, not
one, and if you have the character sequence "]]>", you can't represent
it in a CDATA section. You'd have to end the CDATA, and put part of
that sequence in plain parsed character data, or in an adjacent CDATA.


Sorry, I was unclear and incomplete. Yes, you have to do something if
you want the sequence ]]> in a CDATA section. But you *also* have to
do something if you want the sequence ]]> *not* in a CDATA section:
it's not allowed to occur in ordinary content. This is not
well-formed:

<foo>]]></foo>

So if you're serializing text, you have to either keep track of
characters to ensure that you don't output ]]>, or (more simply)
just use &gt; all the time.

-- Richard
Jul 20 '05 #10
On 26 Nov 2004 00:39:22 GMT, ri*****@cogsci.ed.ac.uk (Richard Tobin)
wrote:
So if you're serializing text, you have to either keep track of
characters to ensure that you don't output ]]>, or (more simply)
just use &gt; all the time.


Sure. But if you're creating XML by hand, writing XPath expressions
inside the attribute value of an XSLT element, as I believe the original
poster was doing, there's no problem with including literal '>'
characters, and I believe that it makes the expressions easier to read.
--
Morris M. Keesan -- ke****@alum.bu.edu

Jul 20 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: timothy ma and constance lee | last post by:
Sir I am using the recursive template to generate the dynamic drop down list as below: <xsl:template name="generateDropdown"> <xsl:param name="length" /> <xsl:param name="refrid" />...
4
by: Gerald Aichholzer | last post by:
Hello NG, I have a XML file looking like: <!-- some elements here --> <plugin> <icon></icon> <!-- some elements here --> <object> <!-- some elements here -->
8
by: Gerald Aichholzer | last post by:
Hello NG, I try to apply the following template <xsl:template match="objectgroup"> <xsl:copy> <xsl:apply-templates select="*|@*"/> <xsl:variable name="object" select="."/> <xsl:for-each...
2
by: Ralph Snart | last post by:
I have a bunch of nodes called <product>. Some of them will have <gumball_id> child elements. How can I get the first 4 products that have gumball_ids ? The previous programmer wrote: ...
5
by: Patrick.O.Ige | last post by:
I have an xml and i'm trying to loop each node... When i do FOR EACH i seem not to get my desired result I want to loop through and get only the values that matches the question i specified with...
2
by: CK | last post by:
Hi Everyone, I have a custom web part in SharePoint. I want a cell in the table to be conditionally formatted. Using Front Page it's fairly easy to make general formatting chnages, like color,...
0
by: poli | last post by:
Hi, It is my first xslt document that I have to write. And I have to do it quick (1 day). PLease help me. I have this input xml file: <?xml version="1.0" encoding="UTF-8"?> <Node...
1
by: Paulson | last post by:
Hi guys I got a problem.I am using xml/xsl in asp.net. I know how to add update delete nodes in xml Files. But I dont know how to create a good stylesheet for it.I...
1
by: tkahn6 | last post by:
Hi! Ok so im having a very very rookie (i assume) problem. I want to take this: <?xml version="1.0" encoding="ISO-8859-1"?> <?xml-stylesheet type="text/xsl" href="xslTest.xsl"?> <dataTree> ...
4
by: a1suria | last post by:
Hi, there.. could you please show me some examples of XSLT codes to match XSD with XML .. the stylesheet should output Yes if matches the schema and No if doesn't match.. schema contains...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.