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
    Sep 06, 2011 @ 21:59
    Luke Johnson
    0

    Join SQL tables in XSLT

    What is the best way to join SQL tables in XSLT? I am familiar with joining SQL tables in regular ASP (such as SELECT column_name FROM table_name1 LEFT JOIN table_name2 ON table_name1.column_name=table_name2.column_name), but am having trouble framing it properly in XSLT.

    <xsl:variable name "getAuthor" select="SQL:GetDataSet('DBname', 'SELECT ArticleID, Author, StaffID FROM web_Articles, LEFT JOIN web_Staff ON web_Articles.Author = web_Staff.StaffID, 'getAuthor')"/>  

    Is there something wrong here that I'm not seeing?

    Thanks,

    Luke

  • Petr Snobelt 923 posts 1535 karma points
    Sep 06, 2011 @ 22:14
    Petr Snobelt
    0

    I think there is "," before left join, try remove it.

  • Luke Johnson 61 posts 80 karma points
    Sep 06, 2011 @ 22:45
    Luke Johnson
    0

    No luck. Here is the fuller code if it helps to see what I am trying to accomplish.

    I have two tables that deal with news stories. One (web_Articles) holds the story content (images, text, author id, etc). The other (web_Staff) holds author information. An "Author" column in web_Articles holds the an int value equal to the "AuthorID" in web_Staff. I would rather show "Julie Cole" as author of the story, rather than "2". 

    When I use the below code, I get the following error:

    System.Xml.Xsl.XslLoadException: Expected token ')', found 'getAuthor'. 
    ...es.Author = web_Staff.StaffID, ' -->getAuthor<-- ') An error occurred at C:\inetpub\wwwroot\UT\xslt\634509170863705620_temp.xslt(21,5). 
    at System.Xml.Xsl.XslCompiledTransform.LoadInternal(Object stylesheet, XsltSettings settings, XmlResolver stylesheetResolver) 
    at umbraco.presentation.webservices.codeEditorSave.SaveXslt(String fileName, String oldName, String fileContents, Boolean ignoreDebugging)

        <xsl:variable name "storyID" select "umbraco.library:RequestQueryString('id')" />
        <!-- Grabs the id number from URL -->

        <xsl:variable name "newsStory" select="SQL:GetDataSet('DBname', concat('SELECT ArticleID, ArticleType, ImageFile1, ImageFile2, ImageFile3, ImageFile1Caption, ImageFile2Caption, ImageFile3Caption, Display, ArticleStartDate, Content, Title, Author FROM web_Articles WHERE ArticleID = ', $storyID), 'newsStory')"/>  
        <!-- gets SQL data from external database tables by the id number in the URL -->

        <xsl:variable name "getAuthor" select="SQL:GetDataSet('DBname', 'SELECT Author, FirstName, LastName, StaffID FROM web_Articles LEFT JOIN web_Staff ON web_Articles.Author = web_Staff.StaffID, 'getAuthor')"/>  
        <!-- attempting to grab the Author's first and last name from the Staff table -->

        <!--<xsl:variable name = "getAuthor" select="SQL:GetDataSet('DBname', concat('SELECT ArticleID, Author, StaffID FROM web_Articles WHERE ArticleID = ', $storyID), LEFT JOIN web_Staff ON web_Articles.Author = web_Staff.StaffID, 'getAuthor')"/>  -->
         <!-- Here is the same query, but with concat, drawing the ID from the URL -->

        <xsl:template match="/">

          <xsl:for-each select "$newsStory//newsStory">
          <h1><xsl:value-of select "Title" disable-output-escaping="yes" /></h1>
            
          <span id "share_newsstory">
            <span class "publish_info">
              <xsl:for-each select "$getAuthor//getAuthor">
                <xsl:value-of select "FirstName" />&nbsp;<xsl:value-of select "LastName" />
              </xsl:for-each<xsl:value-of select="umbraco.library:FormatDateTime(ArticleStartDate, 'MMM d, yyyy')"
    /></span>
      <!-- $getAuthor has its own for-each in order to enact the $getAuthor variable.
            I don't know if this is the right way to do it. -->
          
    </span>
            
          <xsl:value-of select "Content" disable-output-escaping="yes" />
            
        </xsl:for-each>
      </xsl:template>

    </xsl:stylesheet>

    I'm not sure what to try next. Any guidance would be most appreciated!

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Sep 07, 2011 @ 01:01
    Chriztian Steinmeier
    1

    Hi Luke - you're missing an apostrophe in that call to GetDataSet() - just after web_Staff.StaffID:

    <xsl:variable name = "getAuthor" select="SQL:GetDataSet('DBname', 'SELECT Author, FirstName, LastName, StaffID FROM web_Articles LEFT JOIN web_Staff ON web_Articles.Author = web_Staff.StaffID', 'getAuthor')" />

    Please note that there really is no way to "join SQL tables in XSLT" - you're doing the joining in SQL - just getting the data as XML for use in XSLT. 

    /Chriztian

  • Luke Johnson 61 posts 80 karma points
    Sep 07, 2011 @ 17:53
    Luke Johnson
    0

    Thanks, Chriztian! That set me on the right track. Based off of that string, I got the concat to work as well:

    <xsl:variable name "newsStory" select="SQL:GetDataSet('DBname', concat('SELECT column, names, from, both, tables FROM web_Articles INNER JOIN web_Staff ON web_Articles.Author=web_Staff.StaffID WHERE web_Articles.ArticleID = ', $storyID), 'newsStory')" />    
Please Sign in or register to post replies

Write your reply to:

Draft