XSLT: xsl:variable : help!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tjerk Wolterink

    XSLT: xsl:variable : help!

    The following code does not work:

    <xsl:variable name="width" select="form:im age-width"/>
    <xsl:if test="$width>$m ax_image_width" >
    <xsl:variable name="width" select="$max_im age_width"/>
    </xsl:if>
    <img class="admin" width="{$width} " src="{form:imag e-url}" />

    The width attribute of img is always "", the $max_image_widt h 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_widt h=200;
    $width=[some value selected from xml file]

    if($width>$max_ image_width) {
    $width=$max_ima ge_width
    }

    print $width;

  • Joris Gillis

    #2
    Re: XSLT: xsl:variable : help!

    > <xsl:variable name="width" select="form:im age-width"/>[color=blue]
    > <xsl:if test="$width>$m ax_image_width" >
    > <xsl:variable name="width" select="$max_im age_width"/>
    > </xsl:if>
    > <img class="admin" width="{$width} " src="{form:imag e-url}" />
    >[/color]
    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:imag e-width &gt; $max_image_widt h">
    <xsl:value-of select="$max_im age_width"/>
    </xsl:if>
    <xsl:if test="form:imag e-width &lt;= $max_image_widt h">
    <xsl:value-of select="form:im age-width"/>
    </xsl:if>
    </xsl:variable>


    --
    Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041)
    Ceterum censeo XML omnibus esse utendum

    Comment

    • Tjerk Wolterink

      #3
      Re: XSLT: xsl:variable : help!

      Joris Gillis wrote:
      [color=blue][color=green]
      >> <xsl:variable name="width" select="form:im age-width"/>
      >> <xsl:if test="$width>$m ax_image_width" >
      >> <xsl:variable name="width" select="$max_im age_width"/>
      >> </xsl:if>
      >> <img class="admin" width="{$width} " src="{form:imag e-url}" />
      >>[/color]
      > 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:imag e-width &gt; $max_image_widt h">
      > <xsl:value-of select="$max_im age_width"/>
      > </xsl:if>
      > <xsl:if test="form:imag e-width &lt;= $max_image_widt h">
      > <xsl:value-of select="form:im age-width"/>
      > </xsl:if>
      > </xsl:variable>
      >
      >[/color]

      Ok ive this:

      <xsl:variable name="width">
      <xsl:choose>
      <xsl:when test="form:imag e-width &gt; $max_image_widt h">
      <xsl:value-of select="$max_im age_width"/>
      </xsl:when>
      <xsl:otherwis e>
      <xsl:value-of select="form:im age-width"/>
      </xsl:otherwise>
      </xsl:choose>
      </xsl:variable>

      <img class="admin" width="{$width} " src="{form:imag e-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

      Comment

      • Tjerk Wolterink

        #4
        Re: XSLT: xsl:variable : help!

        Tjerk Wolterink wrote:
        [color=blue]
        > Joris Gillis wrote:
        >[color=green][color=darkred]
        >>> <xsl:variable name="width" select="form:im age-width"/>
        >>> <xsl:if test="$width>$m ax_image_width" >
        >>> <xsl:variable name="width" select="$max_im age_width"/>
        >>> </xsl:if>
        >>> <img class="admin" width="{$width} " src="{form:imag e-url}" />
        >>>[/color]
        >> 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:imag e-width &gt; $max_image_widt h">
        >> <xsl:value-of select="$max_im age_width"/>
        >> </xsl:if>
        >> <xsl:if test="form:imag e-width &lt;= $max_image_widt h">
        >> <xsl:value-of select="form:im age-width"/>
        >> </xsl:if>
        >> </xsl:variable>
        >>
        >>[/color]
        >
        > Ok ive this:
        >
        > <xsl:variable name="width">
        > <xsl:choose>
        > <xsl:when test="form:imag e-width &gt; $max_image_widt h">
        > <xsl:value-of select="$max_im age_width"/>
        > </xsl:when>
        > <xsl:otherwis e>
        > <xsl:value-of select="form:im age-width"/>
        > </xsl:otherwise>
        > </xsl:choose>
        > </xsl:variable>
        >
        > <img class="admin" width="{$width} " src="{form:imag e-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[/color]


        Wait: it did work, the element form:image-width was nameed form:width , so stupid fault.

        Comment

        • Joris Gillis

          #5
          Re: XSLT: xsl:variable : help!

          > 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

          Comment

          • Morris M. Keesan

            #6
            Re: XSLT: xsl:variable : help!

            On Wed, 24 Nov 2004 18:11:54 GMT, "Joris Gillis" <roac@pandora.b e>
            wrote:[color=blue][color=green]
            >> Can't is use > in place of &gt; ??? and for &lt; is < not a possibility?[/color]
            >
            >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>[/color]

            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 -- keesan@alum.bu. edu

            Comment

            • Tjerk Wolterink

              #7
              Re: XSLT: xsl:variable : help!

              Morris M. Keesan wrote:[color=blue]
              > On Wed, 24 Nov 2004 18:11:54 GMT, "Joris Gillis" <roac@pandora.b e>
              > wrote:
              >[color=green][color=darkred]
              >>>Can't is use > in place of &gt; ??? and for &lt; is < not a possibility?[/color]
              >>
              >>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>[/color]
              >
              >
              > 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">[/color]

              But do all xml parses and xslt processor work with both &gt; and > ?
              I mean: is it valid to use > in stead of &gt;

              Comment

              • Richard Tobin

                #8
                Re: XSLT: xsl:variable : help!

                In article <co5lev$g9n$1@n etlx020.civ.utw ente.nl>,
                Tjerk Wolterink <tjerk@wolterin kwebdesign.com> wrote:
                [color=blue]
                >But do all xml parses and xslt processor work with both &gt; and > ?
                >I mean: is it valid to use > in stead of &gt;[/color]

                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

                Comment

                • Morris M. Keesan

                  #9
                  Re: XSLT: xsl:variable : help!

                  On 25 Nov 2004 22:19:19 GMT, richard@cogsci. ed.ac.uk (Richard Tobin)
                  wrote:[color=blue]
                  >In article <co5lev$g9n$1@n etlx020.civ.utw ente.nl>,
                  >Tjerk Wolterink <tjerk@wolterin kwebdesign.com> wrote:
                  >[color=green]
                  >>But do all xml parses and xslt processor work with both &gt; and > ?
                  >>I mean: is it valid to use > in stead of &gt;[/color]
                  >
                  >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).[/color]

                  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 -- keesan@alum.bu. edu

                  Comment

                  • Richard Tobin

                    #10
                    Re: XSLT: xsl:variable : help!

                    In article <mhtcq0lgmerfeb ptcp1d8guedc21j kujhd@4ax.com>,
                    Morris M. Keesan <keesan@alum.bu .edu> wrote:
                    [color=blue]
                    >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.[/color]

                    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

                    Comment

                    • Morris M. Keesan

                      #11
                      Re: XSLT: xsl:variable : help!

                      On 26 Nov 2004 00:39:22 GMT, richard@cogsci. ed.ac.uk (Richard Tobin)
                      wrote:
                      [color=blue]
                      >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.[/color]

                      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 -- keesan@alum.bu. edu

                      Comment

                      Working...