Results 1 to 5 of 5

Thread: [RESOLVED] XSLT v 1.0: Function

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Mobile, AL, USA
    Posts
    600

    Resolved [RESOLVED] XSLT v 1.0: Function

    I'm new to XSLT and haven't been able to figure out a simple way to do this.

    I want to create a function (subroutine, procedure, whatever you want to call it ... ) for code reusability. What's the best way to do this?

    Please keep in mind that I'm using version 1 of XSLT.

    Thanks!
    OneSource

  2. #2
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196

    Re: XSLT v 1.0: Function

    I think templates might be whatyour looking for.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Mobile, AL, USA
    Posts
    600

    Question DeadEyes ...

    thanks. I'm already using
    VB Code:
    1. <xsl:template match= ... >
    in my stylesheet. Are you referring to something different? Could you (or anyone else) please provide a simple example of code reuse (with templates or otherwise)?

    OneSource

  4. #4
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196

    Re: XSLT v 1.0: Function

    Here is an example of using a template to create a label with a text box.
    Now while the template is within the same stylesheet it could be placed anywhere and called from any other stylesheet you want.
    Code:
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
      <html>
      <body>
        <table border="0">
        <xsl:for-each select="gui/textbox">
        <tr>
          <td>
            <xsl:call-template name="lbltextbox">
              <xsl:with-param name="txtlabel"><xsl:value-of select="lbl"/></xsl:with-param>
              <xsl:with-param name="txtcontent"><xsl:value-of select="desc"/></xsl:with-param>
            </xsl:call-template>
          </td>
        </tr>
        </xsl:for-each>
        </table>
      </body>
      </html>
    </xsl:template>
    
    <!-- this is the reusable part -->
    <xsl:template name="lbltextbox">
      <xsl:param name="txtlabel"/>
      <xsl:param name="txtcontent"/>
      <label><xsl:value-of select="$txtlabel"/></label>
      <input type="text">
        <xsl:attribute name="value">
          <xsl:value-of select="$txtcontent"/>
          </xsl:attribute>
      </input>
    </xsl:template>
    
    </xsl:stylesheet>
    Code:
    <?xml version="1.0" encoding="windows-1250"?>
    <?xml-stylesheet type="text/xsl" href="sample.xsl"?>
    <gui>
     <textbox>
      <lbl>item1</lbl>
      <desc>1.50</desc>
     </textbox>
     <textbox>
      <lbl>item2</lbl>
      <desc>3.50</desc>
     </textbox>
     </gui>

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Mobile, AL, USA
    Posts
    600

    Thumbs up Thanks ....

    DeadEyes ... I can now SEE the light!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width