Copied to clipboard

Flag this post as spam?

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


  • Kory Doszpoly 19 posts 39 karma points
    Sep 03, 2012 @ 17:43
    Kory Doszpoly
    0

    Reusing XSLT in Another XSLT

    Hi,

    I did my best to find a solution without having to post here, but nothing I found seemed to work. I'm running Umbraco 4.

    The Problem:

    I have a macro that nicely diaplays the output of my XSLT file. I need to add functionaly to my XSLT that will be used in other XSLTs as well. I thought that creating an XSLT file that has my reusable code and then including it my main XSLT would be the way to go. How do I go about this?

    Thanks.

  • Dan 1288 posts 3921 karma points c-trib
    Sep 03, 2012 @ 18:18
    Dan
    0

    Hi Kory,

    You can use includes in XSLT, e.g. <xsl:include href="uri-of-file-to-include"/>.

  • Kory Doszpoly 19 posts 39 karma points
    Sep 03, 2012 @ 19:47
    Kory Doszpoly
    0

    Yes, I tried that before, but at least now I'm getting someting. Once I removed <xsl:param name="currentPage"/> from the xslt to be included it display the string of text I put in there as a test. The problem now is that the rest of my xslt won't display. Right now it seems it's an either or proposition.

  • Fuji Kusaka 2203 posts 4220 karma points
    Sep 03, 2012 @ 20:21
    Fuji Kusaka
    0

    Hi Kory,

    Check out this article on Chriztian's blog :  <xsl:include> or <xsl:import>

    Hope it helps

    //fuji

  • Kory Doszpoly 19 posts 39 karma points
    Sep 03, 2012 @ 21:18
    Kory Doszpoly
    0

    Yes, I have seen that and found it utterly confusing thus unhelpful. It deals with media files and that adds a layer of complexity to the whole thing I don't need.

  • Kory Doszpoly 19 posts 39 karma points
    Sep 03, 2012 @ 21:43
    Kory Doszpoly
    0

    OK, in case I'm not being specific enough. Here is the code to my main xslt:

     <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
    <xsl:stylesheet
      version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:msxml="urn:schemas-microsoft-com:xslt"
      xmlns:umbraco.library="urn:umbraco.library" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets" xmlns:tagsLib="urn:tagsLib" xmlns:BlogLibrary="urn:BlogLibrary"
      exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets tagsLib BlogLibrary ">

    <xsl:output method="xml" omit-xml-declaration="yes" />

    <xsl:param name="currentPage"/>

    <!-- Don't change this, but add a 'contentPicker' element to -->
    <!-- your macro with an alias named 'source' -->
    <xsl:variable name="source" select="/macro/source"/>

    <xsl:template match="/">

    <!-- The fun starts here -->

      This is the main xslt.
      <br /><br />
      Insert sub xslt here:
      <br /><br /> 
      <!-- what do I put in here to include sub.xslt? -->
      
      
      
      <!-- end sub.xslt -->
    </xsl:template>

    </xsl:stylesheet>

    ... and this is what I want to include:


    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
    <xsl:stylesheet
      version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:msxml="urn:schemas-microsoft-com:xslt"
      xmlns:umbraco.library="urn:umbraco.library" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets" xmlns:tagsLib="urn:tagsLib" xmlns:BlogLibrary="urn:BlogLibrary"
      exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets tagsLib BlogLibrary ">

    <xsl:output method="xml" omit-xml-declaration="yes" />

    <xsl:param name="currentPage"/>

    <!-- Don't change this, but add a 'contentPicker' element to -->
    <!-- your macro with an alias named 'source' -->
    <xsl:variable name="source" select="/macro/source"/>

    <xsl:template match="/">

    <!-- The fun starts here -->
    This is the sub xslt.

    </xsl:template>

    </xsl:stylesheet>

    How do I do it?

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Sep 03, 2012 @ 21:52
    Chriztian Steinmeier
    0

    Hi Kory,

    Because of the way Umbraco serves the XML (using the $currentPage parameter) you will run into problems (as you have dicovered) when trying to reuse an existing macro in another macro.

    You can however fix the problem by refactoring the original macro to work as another user of the common functionality, which you then include in that macro too. That way, you'll have e.g., a "functions.xslt" and two macros that include this... the $currentPage parameter should be excluded from the "functions.xslt" but remain in the others. As long as you're not using "functions.xslt" on its own, you shouldn't have problems with "undefined parameter" et.al.  

    Depending on the approach your original macro, you may need to do some restructuring - we can help you with that, no problem - just let us know.

    /Chriztian

  • Kory Doszpoly 19 posts 39 karma points
    Sep 04, 2012 @ 02:58
    Kory Doszpoly
    0

    I'm really at the end of my rope on this one. I could use a little more than hints in the right direction at this point. Spent like 10 hours trying to figure this out.

    I found this article, which seems to describe exactly what I need, but I couldn't get it to work either. http://en.wikibooks.org/wiki/Umbraco/Reference/umbraco.library/RenderMacroContent

    Could someone take a look at this, and tell me if it's outdated info and what do I need to get it to work. Thanks.

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Sep 04, 2012 @ 07:57
    Chriztian Steinmeier
    0

    Hi Kory,

    The RenderContent() method is hideous because you need to use a very old-style syntax for specifying the data needed - *and* you have to do some escaping to use it from within an XSLT file... that said, here's how I think you should be able to call your "sub" XSLT from your main file (note that you need to change the SUB_MACRO_ALIAS_HERE part to the alias of the macro you're trying to include):

    <xsl:value-of select="umbraco.library:RenderMacroContent(concat('&lt;?UMBRACO_MACRO macroAlias=&quot;SUB_MACRO_ALIAS_HERE&quot; source=&quot;', $source, '&quot; /&gt;'), $currentPage/@id)" disable-output-escaping="yes" />

    Hope it helps - otherwise, come back...

    /Chriztian

  • Kory Doszpoly 19 posts 39 karma points
    Sep 04, 2012 @ 15:54
    Kory Doszpoly
    0

    Thanks for that, but it doesn't seem to work.

    I repleaced SUB_MACRO_ALIAS_HERE with the name of the macro I'm trying to include sub.xslt, but what I get is this displayed on the page: <Macro: (,)>

    Any ideas?

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Sep 04, 2012 @ 23:53
    Chriztian Steinmeier
    0

    Ah sorry,my bad — looks like there's a runat="server" attribute missing there... so:

    <xsl:value-of select="umbraco.library:RenderMacroContent(concat('&lt;?UMBRACO_MACRO macroAlias=&quot;SUB_MACRO_ALIAS_HERE&quot; source=&quot;', $source, '&quot; runat=&quot;server&quot; /&gt;'), $currentPage/@id)" disable-output-escaping="yes" />

    /Chriztian

  • Kory Doszpoly 19 posts 39 karma points
    Sep 05, 2012 @ 01:50
    Kory Doszpoly
    0

    Sorry, still no cigar. I get the same thing.

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Sep 06, 2012 @ 21:45
    Chriztian Steinmeier
    0

    Hi Kory,

    I saw the new post and wanted to check something: In the other post it sounds like you're using the filename (e.g. "sub.xslt") where you should be using the macro's alias (usually the same as the filename, but without ".xslt") - which one did you use?

    Other than that, it's pretty hard to say what else may be causing your macro not to work...

    /Chriztian

     

     

  • Kory Doszpoly 19 posts 39 karma points
    Sep 07, 2012 @ 01:00
    Kory Doszpoly
    0

    Holy shit!!! That worked. Chriztian, you are the shiznitz. Thanks a bunch.

Please Sign in or register to post replies

Write your reply to:

Draft