Displaying text from XSL variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • paragguptaiiita
    New Member
    • Feb 2008
    • 21

    Displaying text from XSL variable

    I have a XSL variable named $p = (0 0 0) (0.82 0 0) (1.63 -0.01 0) (2.63 -0.01 0) (3.63 -0.01 0) (4.63 -0.01 0) (5.63 -0.02 0) (6.63 -0.02 0) (7.63 -0.02 0) (8.6 -0.02 0)
    This line contains X,Y,Z coordinates of 10 points.
    I am using this code for displaying
    <td><font size="-2"><xsl:valu e-of select="$p"/></font></td>
    but i want to display each coordinate in a separate line.Like this....
    (0 0 0)
    (0.82 0 0)
    (1.63 -0.01 0)
    (2.63 -0.01 0)
    (3.63 -0.01 0)
    (4.63 -0.01 0)
    (5.63 -0.02 0)
    (6.63 -0.02 0)
    (7.63 -0.02 0)
    (8.6 -0.02 0)

    So plz help...
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    Do you want a seperate row <tr> for each, or just have <br/> tags after each one?

    You could call a parsing template:
    [code=xml]
    <xsl:call-template name="break">
    <xsl:with-param name="str" select="$p"/>
    <xsl:with-param name="breaker" select="'('"/>
    </xsl:call-template>
    ...
    <xsl:template name="break">
    <xsl:param name="str"/>
    <xsl:param name="breaker"/>
    <xsl:choose>
    <xsl:when test="substring-after($str, $breaker) = ''"/>
    <xsl:value-of select="$str"/>
    </xsl:when>
    <xsl:otherwis e>
    <xsl:value-of select="concat( substring-before($str, $breaker), $breaker)"/>
    <br/>
    <xsl:call-template name="break">
    <xsl:with-param name="str" select="substri ng-after($str, $breaker)"/>
    <xsl:with-param name="breaker" select="$breake r"/>
    </xsl:call-template>
    </xsl:otherwise>
    </xsl:template>

    [/code]

    Comment

    • paragguptaiiita
      New Member
      • Feb 2008
      • 21

      #3
      Hi jkmyoung,
      thanks for help, i want to use <br> tag, but the code given by you have some minor error (related to tagging), i corrected that as

      in line 8 <xsl:param name="breaker"\ >
      in line 18 <xsl:with-param name="breaker" select="$breake r)"/>
      after line 20 insert one more line </xsl:choose>
      in line 10 <xsl:when test="substring-after($str, $breaker) = ''"/>

      after these changes the error comes like this
      Keyword xsl:template may not be used here
      can you give me your mail id so that i can send you my XML and XSL file?
      plz help....

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        Sorry, the line should be: (without parentheses at the end)
        <xsl:with-param name="breaker" select="$breake r"/>

        The call-template should be inside some template, the other template has to be just under the root level

        [code=xml]
        <xsl:template match="root">
        ... some code ..
        .. some more code.
        <xsl:call-template .... />
        .. some more code
        </xsl:template>
        .. more templates.
        <xsl:template name="break"/>
        ...

        [/code]

        Comment

        • paragguptaiiita
          New Member
          • Feb 2008
          • 21

          #5
          Thanks jkmyoung
          it is working now....but having a minor problem
          in output it is displaying like this
          (
          0 0 0) (
          0.82 0 0) (
          1.63 -0.01 0) (
          2.63 -0.01 0) (
          3.63 -0.01 0) (
          4.63 -0.01 0) (
          5.63 -0.02 0) (
          6.63 -0.02 0) (
          7.63 -0.02 0) (
          8.6 -0.02 0) (

          i changed one line as
          <xsl:value-of select="concat( $breaker,substr ing-before($str, $breaker))"/>
          and now it is displaying like this
          (
          (0 0 0)
          (0.82 0 0)
          (1.63 -0.01 0)
          (2.63 -0.01 0)
          (3.63 -0.01 0)
          (4.63 -0.01 0)
          (5.63 -0.02 0)
          (6.63 -0.02 0)
          (7.63 -0.02 0)
          8.6 -0.02 0)

          in 1st line we are getting 1 extra small braces and in last line one braces is off.
          so plz help...

          Comment

          • pronerd
            Recognized Expert Contributor
            • Nov 2006
            • 392

            #6
            There is a potentially simpler option. Since what you are really wanting to do is add a br element after each closing ")". You should be able to use the translate() function to convert ")" to ")<br />". There is some trick to escaping element tags in the function strings that I do not re-call off hand.


            Code:
             
                <xsl:value-of select="translate(@someAttributeName,')', ')<br />')" />   
            
                <!--  or -->
            
                <xsl:value-of select="translate(@someAttributeName,')', ')&amp;lt;br /&amp;gt;')" />
            Last edited by pronerd; Mar 27 '08, 02:30 PM. Reason: Need to correct escape characters

            Comment

            • jkmyoung
              Recognized Expert Top Contributor
              • Mar 2006
              • 2057

              #7
              I used the wrong parentheses for breaker
              <xsl:with-param name="breaker" select="')'"/>

              sorry for my carelessness.

              Comment

              • paragguptaiiita
                New Member
                • Feb 2008
                • 21

                #8
                Thanks a lot.....
                can u give me ur mail id....?

                Comment

                • paragguptaiiita
                  New Member
                  • Feb 2008
                  • 21

                  #9
                  how can we insert the serial number in the beginning for displaying like this....
                  1. (0 0 0)
                  2. (0.82 0 0)
                  3. (1.63 -0.01 0)
                  4. (2.63 -0.01 0)
                  5. (3.63 -0.01 0)
                  6. (4.63 -0.01 0)
                  7. (5.63 -0.02 0)
                  8. (6.63 -0.02 0)
                  9. (7.63 -0.02 0)
                  10. (8.6 -0.02 0)

                  I tried this by declaring two XSL variables before template definition

                  <xsl:variable name="temp" select="1"/>
                  <xsl:variable name="one" select="1"/>

                  and then after these statements
                  </xsl:when>
                  <xsl:otherwis e>

                  I inserted these lines of code

                  <xsl:variable name="temp" select=" $temp + $one "/>
                  <xsl:value-of select="$temp"/>

                  but it didn't work, it comes like this

                  2 (0 0 0)
                  2 (0.82 0 0)
                  2 (1.63 -0.01 0)
                  2 (2.63 -0.01 0)
                  2 (3.63 -0.01 0)
                  2 (4.63 -0.01 0)
                  2 (5.63 -0.02 0)
                  2 (6.63 -0.02 0)
                  2 (7.63 -0.02 0)
                  2 (8.6 -0.02 0)

                  Comment

                  • pronerd
                    Recognized Expert Contributor
                    • Nov 2006
                    • 392

                    #10
                    Since 1 + 1 always equals 2 that is all that will ever return. Incrementing counters are difficult to do in XSL since it does not use variables in the normal way we think of them.

                    The only way I have ever been able to do an incrementing counter in XSL is to use it in a recursive template call which I do not think will work for you.


                    Code:
                        <xsl:template name="someTemplate">
                            <xsl:param name="counter" />
                    
                            <!--  Increment the counter -->
                            <xsl:variable name="newCounter">
                                <xsl:value-of select="$counter + 1"/>
                            </xsl:variable>
                    
                            <!--   Blah Blah Blah Do some stuff here-->
                    
                            <xsl:call-template name="someTemplate" >
                                <xsl:with-param name="outPutText" select="$newCounter" />
                            </xsl:call-template>
                    
                        </xsl:template>

                    Comment

                    • jkmyoung
                      Recognized Expert Top Contributor
                      • Mar 2006
                      • 2057

                      #11
                      With respect to above post, you are using a recursive template.
                      Add the param to the initial call:
                      <xsl:with-param name="counter" select="1"/>

                      Delcare it in the template:
                      <xsl:param name="counter"/>

                      Output it when needed:
                      <xsl:value-of select="$counte r"/>

                      And when doing the recursive call, add 1 to it.
                      <xsl:with-param name="counter" select="$counte r +1"/>

                      Comment

                      • paragguptaiiita
                        New Member
                        • Feb 2008
                        • 21

                        #12
                        Thanx a lot...its working...
                        now if i want round off the value upto 1 decimal place, then how can we do it.
                        i m using this line of code
                        <xsl:value-of select="concat ( substring-before( $x, '.' ) , '.' , substring ( substring-after ( $x, '.' ) , 1 ,2) )" />

                        but for point like (0 0 0),it is displaying ( .. ). It will not display 0 (zero) value.
                        so plz help....

                        Comment

                        • pronerd
                          Recognized Expert Contributor
                          • Nov 2006
                          • 392

                          #13
                          Originally posted by jkmyoung
                          With respect to above post, you are using a recursive template.
                          Add the param to the initial call:
                          <xsl:with-param name="counter" select="1"/>

                          Delcare it in the template:
                          <xsl:param name="counter"/>

                          Output it when needed:
                          <xsl:value-of select="$counte r"/>

                          And when doing the recursive call, add 1 to it.
                          <xsl:with-param name="counter" select="$counte r +1"/>
                          Isn't that exactly what I posted?

                          Comment

                          • jkmyoung
                            Recognized Expert Top Contributor
                            • Mar 2006
                            • 2057

                            #14
                            Your problem there is that it assumes that there is a decimal place. I suggest format-number() for this type of formatting:


                            Unfortunately, this also means parsing each of the insides of the brackets. I would suggest another template to parse the inside
                            So instead of
                            [code=xml]
                            <xsl:value-of select="$str"/>
                            and
                            <xsl:value-of select="concat( substring-before($str, $breaker), $breaker)"/>
                            [/code]
                            You'd have
                            [code=xml]
                            <xsl:call-template name="formatTri plet">
                            <xsl:with-param name="str" select="$str"/>
                            </xsl:call-template>
                            and
                            <xsl:call-template name="formatTri plet">
                            <xsl:with-param name="str" select="concat( substring-before($str, $breaker), $breaker)"/>
                            </xsl:call-template>
                            [/code]

                            Inside the formatTriplet function, get each of the numbers and format it like so:
                            [code=xml]<xsl:value-of select="format-number($number, '0.0')"/>[/code]

                            Comment

                            • paragguptaiiita
                              New Member
                              • Feb 2008
                              • 21

                              #15
                              Thanx.....Now i want to update the XML file....
                              i have a XML file, for that XML file i made a XSL file,this XSL file showing the data of XML file in a HTML form, data shown in text boxes, so that we can edit the data and a submit button is also present. Now when we click submit button,there is an action field that points to the ASP file,now i want to update the data of XML file.



                              I am using ASP till now, but the problem is if the value of the 'id' attribute is same for all field than problem comes. and other problem is-lets assume value of 'id' attribute is unique then if we want to update the value of both field_value as well as taborder, then how can we do this....

                              Comment

                              Working...