Copied to clipboard

Flag this post as spam?

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


  • Bjarne Fyrstenborg 1284 posts 4038 karma points MVP 8x c-trib
    Nov 25, 2011 @ 18:09
    Bjarne Fyrstenborg
    0

    Include xslt in another xslt

    Hi..

    I have listed related products (other products in the category) on the product page, with the code below..
    But I would like to separate it to its own xslt file (relatedProducts.xslt), and the including the code on the product page (product_product.xslt) ...

    <xsl:variable name="relatedProducts" select="$currentPage/parent::ProductCategory/Product [@id != $currentPage/@id]"/>
        <xsl:if test="count($relatedProducts) > 0">
        <div id="relatedCategoryProducts">
        <h3>Produkter i samme kategori</h3>
            <ul id="relatedProducts">
              <xsl:for-each select="$relatedProducts">
                <xsl:variable name="relvariant" select="./descendant-or-self::Product [not(child::Product)][1]"/>
                <xsl:variable name="productLink" select="umbraco.library:NiceUrl(@id)" />
                <xsl:variable name="relatedprices" select="teacommerce:GetPrices($relvariant)" />
                <xsl:variable name="relatedpriceUnFormatted" select="$relatedprices/* [name() = $currentCurrency/@ISOCode]/@price" />
                <xsl:variable name="relatedProductPrice" select="teacommerce:FormatPriceWithSpecificCulture($relatedpriceUnFormatted, $currentCurrency/@cultureName)" />
                <li>
                  <href="{$productLink}" title="">
                    <img src="{concat('/ImageGen.ashx?Width=100&amp;Image=',./productImage/DAMP/mediaItem/Image/umbracoFile)}" alt="" />
                  </a>
                  <div class="productName"><xsl:value-of select="productName"/></div>
                  
                  <span class="{concat('currency', ./@id, $currentCurrency)} productPrice" itemprop="offers" itemscope="" itemtype="http://schema.org/Offer">
                    <xsl:choose>
                      <xsl:when test="$stock = '' or $stock &gt; 0">
                        <meta itemprop="availability" content="http://schema.org/InStock" />
                      </xsl:when>
                      <xsl:otherwise>
                        <meta itemprop="availability" content="http://schema.org/OutOfStock" />
                      </xsl:otherwise>
                    </xsl:choose>
                    
                    <span itemprop="price"><xsl:value-of select="$relatedProductPrice"/></span>
                  </span>
                  <br class="clearBoth" />
                  <href="{$productLink}" title="">Se mere</a>
                </li>
              </xsl:for-each>
            </ul>
        </div>
        </xsl:if>

    If I'm right I should wrap that code in a template <xsl:template match="RelatedProducts"> .... </xsl:template> in the relatedProducts.xslt .. and the somehow include that template where I want it in the product_product.xslt..
    How do I do this?

    Bjarne

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Nov 25, 2011 @ 18:53
    Chriztian Steinmeier
    0

    Hi Bjarne,

    I wrote an article on that here: http://pimpmyxslt.com/articles/include-or-import/

    /Chriztian

  • Bjarne Fyrstenborg 1284 posts 4038 karma points MVP 8x c-trib
    Nov 30, 2011 @ 00:19
    Bjarne Fyrstenborg
    0

    Hi Chriztian..

    I looked at your article and looked around on the forum and came up with this:

    relatedProducts.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:teacommerce="urn:teacommerce" 
      exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets teacommerce ">


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

    <xsl:variable name="currentCurrency" select="teacommerce:GetCurrentCurrency()"/>
    <xsl:param name="currentPage"/>
    <xsl:template name="RelatedProducts">

    <xsl:variable name="relatedProducts" select="$currentPage/parent::ProductCategory/Product [@id != $currentPage/@id]"/>
        <xsl:if test="count($relatedProducts) > 0">
        <div id="relatedCategoryProducts">
        <h3>Produkter i samme kategori</h3>
            <ul id="relatedProducts">
              <xsl:for-each select="$relatedProducts">
                <xsl:variable name="relvariant" select="./descendant-or-self::Product [not(child::Product)][1]"/>
                <xsl:variable name="stock" select="teacommerce:GetStock($relvariant)" />
                <xsl:variable name="productLink" select="umbraco.library:NiceUrl(@id)" />
                <xsl:variable name="relatedprices" select="teacommerce:GetPrices($relvariant)" />
                <xsl:variable name="relatedpriceUnFormatted" select="$relatedprices/* [name() = $currentCurrency/@ISOCode]/@price" />
                <xsl:variable name="relatedProductPrice" select="teacommerce:FormatPriceWithSpecificCulture($relatedpriceUnFormatted, $currentCurrency/@cultureName)" />
                <li>
                  <href="{$productLink}" title="">
                    <img src="{concat('/ImageGen.ashx?Width=100&amp;Image=',./productImage/DAMP/mediaItem/Image/umbracoFile)}" alt="" />
                  </a>
                  <div class="productName"><xsl:value-of select="productName"/></div>
                  
                  <span class="{concat('currency', ./@id, $currentCurrency)} productPrice" itemprop="offers" itemscope="" itemtype="http://schema.org/Offer">
                    <xsl:choose>
                      <xsl:when test="$stock = '' or $stock &gt; 0">
                        <meta itemprop="availability" content="http://schema.org/InStock" />
                      </xsl:when>
                      <xsl:otherwise>
                        <meta itemprop="availability" content="http://schema.org/OutOfStock" />
                      </xsl:otherwise>
                    </xsl:choose>
                    
                    <span itemprop="price"><xsl:value-of select="$relatedProductPrice"/></span>
                  </span>
                  <br class="clearBoth" />
                  <href="{$productLink}" title="">Se mere</a>
                </li>
              </xsl:for-each>
            </ul>
        </div>
        </xsl:if>

    </xsl:template>

    </xsl:stylesheet>

    and then in my product_product.xslt where I have included the relatedProducts.xslt and the called the template below the product.

    <xsl:param name="currentPage"/>
      <xsl:variable name="currentCurrency" select="teacommerce:GetCurrentCurrency()"/>
      <xsl:include href="relatedProducts.xslt" />

      <xsl:template match="/">

        <!-- I make the name() = 'Product' check here to force umbraco to save the page. Otherwise it cheats and throws errors all over the place. -->
        <xsl:apply-templates select="$currentPage [name() = 'Product']" />
        
      </xsl:template>

      <xsl:template match="Product" >
    <!-- Code for the product here: title, description, price, images, variants etc. -->
    <div class="clearBoth"><xsl:text</xsl:text></div>
        
        <xsl:call-template name="RelatedProducts" />
      </xsl:template> 

    It seem to work and save the files without errors and also seem to be displayed just as before: http://sub.ak-security.dk/da/shop/category-b/product-b2.aspx

    Is it the right way I have done it?

    Bjarne

  • Bjarne Fyrstenborg 1284 posts 4038 karma points MVP 8x c-trib
    Nov 30, 2011 @ 00:30
    Bjarne Fyrstenborg
    0

    I get a small error with:

    <xsl:variable name="currentCurrency" select="teacommerce:GetCurrentCurrency()"/>
    <xsl:param name="currentPage"/>

    I need them in both files, but I'll then have an error when saving the files..

    So right now I have removed them from relatedProducts.xslt..

    is there any way I can include them in the relatedProducts.xslt without getting an error saying it's duplicated..

    Bjarne

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Nov 30, 2011 @ 00:57
    Chriztian Steinmeier
    1

    Hi Bjarne,

    Yes, it's an error to include/import a global variable/parameter with the same name as an existing - but it's fixable... 

    The best and most flexible way of doing this, is to add them as parameters to the template:

    <xsl:template name="RelatedProducts">
        <xsl:param name="currentCurrency" />
        <xsl:param name="currentPage" />
    ...
    </xsl:template>

    - and then send in the values when calling it:

    <xsl:call-template name="RelatedProducts">
        <xsl:with-param name="currentCurrency" select="teacommerce:GetCurrentCurrency()" />
        <xsl:with-param name="currentPage" select="$currentPage" />
    </xsl:call-template>
    

    This way, you've actually decoupled the template from its surroundings and made it easier to test - win! :-)

    /Chriztian

  • Bjarne Fyrstenborg 1284 posts 4038 karma points MVP 8x c-trib
    Nov 30, 2011 @ 01:07
    Bjarne Fyrstenborg
    0

    Thanks.. that fixed it..

    I also noticed that when changing something in relatedProducts.xslt I have to save product_product.xslt before I can see the changes..

    But I think I read something about XSLTouch fix this problem..?

    Bjarne

  • Bjarne Fyrstenborg 1284 posts 4038 karma points MVP 8x c-trib
    Nov 30, 2011 @ 23:29
    Bjarne Fyrstenborg
    0

    Do you know why the images is moves a bit down and to left here: http://sub.ak-security.dk/da/shop/category-a/product-a3.aspx ... it should stay on the right as it is here: http://sub.ak-security.dk/da/shop/category-b/product-b3.aspx

    The div with class thumb_wrapper is moved inside the productContent div.. but it should be outside that div..

    When I remove the include link and the called template from the product_product.xslt it's shown fine with the images on the right..

    Bjarne

  • Bjarne Fyrstenborg 1284 posts 4038 karma points MVP 8x c-trib
    Dec 06, 2011 @ 01:29
    Bjarne Fyrstenborg
    0

    Okay.. the product description div was self-closing, when the product description was empty.. but I fixed it with adding a forced space with  xsl:text ... it could be xsl:comment, &nbsp; xsl:value-of select=" " as well: http://our.umbraco.org/wiki/how-tos/xslt-useful-tips-and-snippets/stop-html-tags-from-self-closing

    <div id="productDescription" itemprop="description"><xsl:text</xsl:text>
        <xsl:value-of select="$productDescription" disable-output-escaping="yes" />
    </div>

    Bjarne

Please Sign in or register to post replies

Write your reply to:

Draft