Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Luke Johnson 61 posts 80 karma points
    Aug 23, 2011 @ 19:43
    Luke Johnson
    0

    link xslt files - how to call a xslt variable from a different xslt file

    Is there a way to declare an XSLT variable in one XSLT file, and call that variable in a different XSLT file?

    I have a series of SQL database tables, all of which point to the same database. I would like to store the name o fthe database as a variable so that I can call the variable in each of my SQL query strings. This way, should we ever change from one database to another, I only have to change the value of my database name variable rather than going through every single XSLT file to change the database name.

    Here's a snippet of what I'd like to do:

    In one XSLT file, I want to store the name of my database:

    <xsl:variable name = "dbName" select = "Web_Database" />

    Then, in several XSLT files used throughout the website, I would like to replace "Web_Database" with $dbName:

      <xsl:variable name "variable_name" select="SQL:GetDataSet('$dbName', 'SELECT column_name FROM table_name', 'item')"/>

    How do I transmit a variable from one XSLT file to another?

     

  • Lennart Stoop 304 posts 842 karma points
    Aug 23, 2011 @ 20:11
    Lennart Stoop
    0

    Hi Luke,

    I guess it would be possible if you create one XSLT file holding the variable and include it in all the XSLT files that access the database.

    On the other hand, I think a nicer solution would be to store the database name in a content node and read it from there.

    Another option is to create your own XSLT helper method which fetches the database name from wherever (xml, web service, hardcoded, ..) although the former option is much easier :-)

     

    Grtz

    L

  • Chriztian Steinmeier 2798 posts 8787 karma points MVP 7x admin c-trib
    Aug 23, 2011 @ 20:54
    Chriztian Steinmeier
    0

    Hi Luke,

    As you've probably found out, you can't include/import an XSLT file that has a global variable/param with the same name as a global in the "master" XSLT file - the way you usually tackle this, is to define the variable globally in the master, and then in the included file(s), use a local variable in each template. Then use the <xsl:with-param /> instruction when calling/applying templates - this way, you've modularized the components and the templates don't depend on any outside stuff. TL;DR:

    <!-- Master.xslt -->
    <xsl:stylesheet
        version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    >
    
        <xsl:variable name="dbName" select="'MovieStarAddressesInHollywood'" />
    
        <xsl:template match="/">
            <!-- do stuff -->
    
            <!-- Call included template -->
            <xsl:call-template name="GetNumberOfRowsInTable">
                <xsl:with-param name="db" select="$dbName" />
            </xsl:call-template>
        </xsl:template>
    
        <xsl:include href="Helper.xslt" />
    
    </xsl:stylesheet>
    
    
    <!-- Helper.xslt -->
    <xsl:stylesheet
        version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    >
    
        <xsl:template name="GetNumberOfRowsInTable">
            <xsl:param name="db" />
    
            <!-- do stuff -->
    
        </xsl:template>
    
    </xsl:stylesheet>

    /Chriztian

Please Sign in or register to post replies

Write your reply to:

Draft