XSLT: xsl:variable : help!

T

Tjerk Wolterink

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;
 
J

Joris Gillis

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

Tjerk Wolterink

Joris said:
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:eek:therwise>
<xsl:value-of select="form:image-width"/>
</xsl:eek:therwise>
</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
 
T

Tjerk Wolterink

Tjerk said:
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:eek:therwise>
<xsl:value-of select="form:image-width"/>
</xsl:eek:therwise>
</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.
 
J

Joris Gillis

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

Morris M. Keesan

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">
 
T

Tjerk Wolterink

Morris said:
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;
 
R

Richard Tobin

Tjerk Wolterink said:
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
 
M

Morris M. Keesan

Tjerk Wolterink said:
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.
 
R

Richard Tobin

Morris M. Keesan said:
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
 
M

Morris M. Keesan

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.
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top