Copied to clipboard

Flag this post as spam?

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


  • Dan Sunesen 45 posts 145 karma points
    Feb 11, 2013 @ 17:51
    Dan Sunesen
    0

    XSLT table

    So I have the following XML:

    <ImportProduct>
        <Title />
        <ShortDescription />
        <LongDescription />
        <SKU>48265</SKU>
        <LanguageKey>ENGLISH</LanguageKey>
        <ShowOnTheeseContries />
        <Header>Crocodile</Header>
        <SubHeader />
        <Description />
        <ItemFamilies>
          <ItemFamily>
            <CompId>48301</CompId>
            <ProductCompId>48265</ProductCompId>
            <LanguageKey>ENGLISH</LanguageKey>
            <Header>Size 1</Header>
            <SubHeader>cm (inch)</SubHeader>
            <Description />
            <ItemFamCat>Size and measurements</ItemFamCat>
            <Measure />
            <Size />
            <CharsetTEXT>
              <root>
                <row no="0" language="ENGLISH">
                  <key>widthbetweenhandles</key>
                  <label>Width between handles (J)</label>
                  <text>27-55 (10½ - 21½”)</text>
                </row>
                <row no="13" language="ENGLISH">
                  <key>heightfromfloortohandles</key>
                  <label>Height from floor to handles (K)</label>
                  <text>39-70 (15¼ - 27¼”)</text>
                </row>
                <row no="26" language="ENGLISH">
                  <key>widthg</key>
                  <label>Width, max. (G)</label>
                  <text>62 (24”)</text>
                </row>
                <row no="39" language="ENGLISH">
                  <key>LengthH</key>
                  <label>Length (H)</label>
                  <text>72 (28”)</text>
                </row>
                <row no="52" language="ENGLISH">
                  <key>frameheight</key>
                  <label>Frame height (I)</label>
                  <text>49-66 (19-25¾”)</text>
                </row>
                <row no="65" language="ENGLISH">
                  <key>lengthfolded</key>
                  <label>Length folded</label>
                  <text>72 (28”)</text>
                </row>
                <row no="78" language="ENGLISH">
                  <key>heightfolded</key>
                  <label>Height folded</label>
                  <text>31 (12”)</text>
                </row>
                <row no="91" language="ENGLISH">
                  <key>weightstandard</key>
                  <label>Weight, standard</label>
                  <text>5,5 kg (12 lb)</text>
                </row>
                <row no="104" language="ENGLISH">
                  <key>userheightmaximum</key>
                  <label>User height, maximum</label>
                  <text>115 (44¾”)</text>
                </row>
                <row no="117" language="ENGLISH">
                  <key>userweightmaximum</key>
                  <label>User weight, maximum</label>
                  <text>30 kg (66 lb)</text>
                </row>
                <row no="130" language="ENGLISH">
                  <key>Turningradius</key>
                  <label>Turning radius</label>
                  <text>75 (29½”)</text>
                </row>
              </root>
            </CharsetTEXT>
            <Images />
          </ItemFamily>
          <ItemFamily>
            <CompId>48302</CompId>
            <ProductCompId>48265</ProductCompId>
            <LanguageKey>ENGLISH</LanguageKey>
            <Header>Size 2</Header>
            <SubHeader>cm (inch)</SubHeader>
            <Description />
            <ItemFamCat>Size and measurements</ItemFamCat>
            <Measure />
            <Size />
            <CharsetTEXT>
              <root>
                <row no="0" language="ENGLISH">
                  <key>widthbetweenhandles</key>
                  <label>Width between handles (J)</label>
                  <text>32-60 (12½ - 23½”)</text>
                </row>
                <row no="13" language="ENGLISH">
                  <key>heightfromfloortohandles</key>
                  <label>Height from floor to handles (K)</label>
                  <text>55-88 (21½ - 34¼”)</text>
                </row>
                <row no="26" language="ENGLISH">
                  <key>widthg</key>
                  <label>Width, max. (G)</label>
                  <text>69 (27¼”)</text>
                </row>
                <row no="39" language="ENGLISH">
                  <key>LengthH</key>
                  <label>Length (H)</label>
                  <text>84 (32¾”)</text>
                </row>
                <row no="52" language="ENGLISH">
                  <key>frameheight</key>
                  <label>Frame height (I)</label>
                  <text>65-82 (25¼ - 32”)</text>
                </row>
                <row no="65" language="ENGLISH">
                  <key>lengthfolded</key>
                  <label>Length folded</label>
                  <text>86 (33½”)</text>
                </row>
                <row no="78" language="ENGLISH">
                  <key>heightfolded</key>
                  <label>Height folded</label>
                  <text>27 (10½”)</text>
                </row>
                <row no="91" language="ENGLISH">
                  <key>weightstandard</key>
                  <label>Weight, standard</label>
                  <text>6,5 kg (14 lb)</text>
                </row>
                <row no="104" language="ENGLISH">
                  <key>userheightmaximum</key>
                  <label>User height, maximum</label>
                  <text>150 (58½”)</text>
                </row>
                <row no="117" language="ENGLISH">
                  <key>userweightmaximum</key>
                  <label>User weight, maximum</label>
                  <text>45 kg (99 lb)</text>
                </row>
                <row no="130" language="ENGLISH">
                  <key>Turningradius</key>
                  <label>Turning radius</label>
                  <text>77 (30¼”)</text>
                </row>
              </root>
            </CharsetTEXT>
            <Images />
          </ItemFamily>
          <ItemFamily>
            <CompId>48303</CompId>
            <ProductCompId>48265</ProductCompId>
            <LanguageKey>ENGLISH</LanguageKey>
            <Header>Size 3</Header>
            <SubHeader>cm (inch)</SubHeader>
            <Description />
            <ItemFamCat>Size and measurements</ItemFamCat>
            <Measure />
            <Size />
            <CharsetTEXT>
              <root>
                <row no="0" language="ENGLISH">
                  <key>widthbetweenhandles</key>
                  <label>Width between handles (J)</label>
                  <text>35-69 (13½ - 27¼”)</text>
                </row>
                <row no="13" language="ENGLISH">
                  <key>heightfromfloortohandles</key>
                  <label>Height from floor to handles (K)</label>
                  <text>70-100 (27½ -39¼”)</text>
                </row>
                <row no="26" language="ENGLISH">
                  <key>widthg</key>
                  <label>Width, max. (G)</label>
                  <text>69 (27¼”)</text>
                </row>
                <row no="39" language="ENGLISH">
                  <key>LengthH</key>
                  <label>Length (H)</label>
                  <text>87 (34¼”)</text>
                </row>
                <row no="52" language="ENGLISH">
                  <key>frameheight</key>
                  <label>Frame height (I)</label>
                  <text>77 (30¼”)</text>
                </row>
                <row no="65" language="ENGLISH">
                  <key>lengthfolded</key>
                  <label>Length folded</label>
                  <text>100 (39½”)</text>
                </row>
                <row no="78" language="ENGLISH">
                  <key>heightfolded</key>
                  <label>Height folded</label>
                  <text>29 (11½”)</text>
                </row>
                <row no="91" language="ENGLISH">
                  <key>weightstandard</key>
                  <label>Weight, standard</label>
                  <text>12 kg (26½ lb)</text>
                </row>
                <row no="104" language="ENGLISH">
                  <key>userheightmaximum</key>
                  <label>User height, maximum</label>
                  <text>180 (71”)</text>
                </row>
                <row no="117" language="ENGLISH">
                  <key>userweightmaximum</key>
                  <label>User weight, maximum</label>
                  <text>80 kg (176¼ lb)</text>
                </row>
                <row no="130" language="ENGLISH">
                  <key>Turningradius</key>
                  <label>Turning radius</label>
                  <text>90 (35½”)</text>
                </row>
              </root>
            </CharsetTEXT>
            <Images />
          </ItemFamily>
          <ItemFamily>
        <!-- some other content -->
          </ItemFamily>
          <ItemFamily>
            <!-- some other content -->
          </ItemFamily>
          <ItemFamily>
        <!-- some other content -->
          </ItemFamily>           
       </ItemFamilies>
    </ImportProduct>
    

     

    I need to output it like this:

    <table>
        <thead>
            <tr>
                <th></th>
                <th>Size 1</th>
                <th>Size 2</th>
                <th>Size 3</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>Label</td>
                <td>Text for size 1</td>
                <td>Text for size 2</td>
                <td>Text for size 3</td>
            </tr>
            <tr>
                <td>Label</td>
                <td>Text for size 1</td>
                <td>Text for size 2</td>
                <td>Text for size 3</td>
            </tr>
            <tr>
                <td>Label</td>
                <td>Text for size 1</td>
                <td>Text for size 2</td>
                <td>Text for size 3</td>
            </tr>
        </tbody>
    </table>

     

    I have the following XSLT working:

    <?xml version="1.0" encoding="utf-8" ?>
    <xsl:stylesheet
        version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:umb="urn:umbraco.library"
        xmlns:kv="urn:R82Import"
        xmlns:CommerceLibrary="urn:CommerceLibrary"
        exclude-result-prefixes="umb kv CommerceLibrary"
    >
    
        <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
    
        <xsl:param name="currentPage" />
        <xsl:variable name="siteRoot" select="$currentPage/ancestor-or-self::Website" />
    
        <xsl:variable name="sku" select="umb:RequestQueryString('sku')" />
        <xsl:variable name="langSelectDic" select="umb:GetDictionaryItem('LanguageList')"/>
          <xsl:variable name="selectLanguage">
            <xsl:choose>
              <xsl:when test="$langSelectDic = 'de'">GERMAN</xsl:when>
              <xsl:when test="$langSelectDic = 'es'">SPANISH</xsl:when>
              <xsl:when test="$langSelectDic = 'nl'">DUTCH</xsl:when>
              <xsl:when test="$langSelectDic = 'uk'">ENLLISHUK</xsl:when>
              <xsl:when test="$langSelectDic = 'cz'">CZECH</xsl:when>
              <xsl:otherwise>ENGLISH</xsl:otherwise>
            </xsl:choose>
          </xsl:variable>
    
        <xsl:variable name="productExtra" select="kv:GetProductInformation($sku, $selectLanguage)" />
    
        <xsl:template match="/">                      
          <div class="table-wrapper">
            <div class="scrollable">
              <table class="measurement">
            <thead>
              <tr>
                <th></th>
                <xsl:apply-templates select="$productExtra/ImportProduct/ItemFamilies/ItemFamily[ItemFamCat = 'Size and measurements']" />
              </tr>
            </thead>
            <tbody>
              <xsl:apply-templates select="$productExtra/ImportProduct" />
                </tbody>
              </table>
            </div>
          </div>
        </xsl:template>
    
        <xsl:template match="ItemFamily">
          <th><xsl:value-of select="Header" /></th>
        </xsl:template>
    
        <xsl:template match="ImportProduct">
            <xsl:variable name="item" select="ItemFamilies/ItemFamily[ItemFamCat = 'Size and measurements']" />
    
            <xsl:for-each select="$item/CharsetTEXT/root/row">        
              <xsl:variable name="colour">
                <xsl:choose>
                  <xsl:when test="position() mod 2">
                <xsl:text>odd</xsl:text>
                  </xsl:when>
                  <xsl:otherwise>
                <xsl:text>even</xsl:text>
                  </xsl:otherwise>
                </xsl:choose>
              </xsl:variable>
              <tr class="{$colour}">
                <xsl:apply-templates select="label" />
                <xsl:apply-templates select="text" />
              </tr>
            </xsl:for-each>
        </xsl:template>
    
        <xsl:template match="label">
          <td><xsl:value-of select="." /></td>
        </xsl:template>
    
        <xsl:template match="text">
          <td><xsl:value-of select="." /></td>
        </xsl:template>
    
    </xsl:stylesheet>

     

    But this gives me the following:

    <table>
        <thead>
            <tr>
                <th></th>
                <th>Size 1</th>
                <th>Size 2</th>
                <th>Size 3</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>Label</td>
                <td>Text for size 1</td>
            </tr>
            <tr>
                <td>Label</td>
                <td>Text for size 1</td>
            </tr>
            <tr>
                <td>Label</td>
                <td>Text for size 1</td>
            </tr>
            <tr>
                <td>Label</td>
                <td>Text for size 2</td>
            </tr>
            <tr>
                <td>Label</td>
                <td>Text for size 2</td>
            </tr>
            <tr>
                <td>Label</td>
                <td>Text for size 2</td>
            </tr>
            <tr>
                <td>Label</td>
                <td>Text for size 3</td>
            </tr>
            <tr>
                <td>Label</td>
                <td>Text for size 3</td>
            </tr>
            <tr>
                <td>Label</td>
                <td>Text for size 3</td>
            </tr>
        </tbody>
    </table>

     

    Any suggestions?

    Thanks in advance

     

    Br,
    Dan

  • Dan Sunesen 45 posts 145 karma points
    Feb 11, 2013 @ 18:28
    Dan Sunesen
    0

    This is how it looks atm:

  • Dan Sunesen 45 posts 145 karma points
    Feb 11, 2013 @ 19:50
    Dan Sunesen
    0

    Maybe it's hard to see, but the labels are repeated three times.. So I only need to output the labels once and then show the sizes under size 1, 2, 3.. etc.

  • kim Thomsen 59 posts 277 karma points
    Feb 11, 2013 @ 20:06
    kim Thomsen
    0

    Hi Dan this is just an idear if this dont work you will have to look into grouping in xslt

     

          <?xml version="1.0" encoding="utf-8" ?>
    <xsl:stylesheet
            version="1.0"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:umb="urn:umbraco.library"
            xmlns:kv="urn:R82Import"
            xmlns:CommerceLibrary="urn:CommerceLibrary"
            exclude-result-prefixes="umb kv CommerceLibrary"
    >
    
            <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
    
            <xsl:param name="currentPage" />
            <xsl:variable name="siteRoot" select="$currentPage/ancestor-or-self::Website" />
    
            <xsl:variable name="sku" select="umb:RequestQueryString('sku')" />
            <xsl:variable name="langSelectDic" select="umb:GetDictionaryItem('LanguageList')"/>
              <xsl:variable name="selectLanguage">
                <xsl:choose>
                  <xsl:when test="$langSelectDic = 'de'">GERMAN</xsl:when>
                  <xsl:when test="$langSelectDic = 'es'">SPANISH</xsl:when>
                  <xsl:when test="$langSelectDic = 'nl'">DUTCH</xsl:when>
                  <xsl:when test="$langSelectDic = 'uk'">ENLLISHUK</xsl:when>
                  <xsl:when test="$langSelectDic = 'cz'">CZECH</xsl:when>
                  <xsl:otherwise>ENGLISH</xsl:otherwise>
                </xsl:choose>
              </xsl:variable>
    
            <xsl:variable name="productExtra" select="kv:GetProductInformation($sku, $selectLanguage)" />
    
            <xsl:template match="/">                                                
              <div class="table-wrapper">
                <div class="scrollable">
                  <table class="measurement">
                    <thead>
                      <tr>
                        <th></th>
                        <xsl:apply-templates select="$productExtra/ImportProduct/ItemFamilies/ItemFamily[ItemFamCat = 'Size and measurements']" />
                      </tr>
                    </thead>
                    <tbody>
                      <xsl:apply-templates select="$productExtra//root" >
                              <xsl:sort select="@no"/>
                    </xsl:apply-templates>
                    </tbody>
                  </table>
                </div>
              </div>
            </xsl:template>
    
            <xsl:template match="ItemFamily">
              <th><xsl:value-of select="Header" /></th>
            </xsl:template>
    
            <xsl:template match="row">
                    <xsl:if  test="position() mod 3">
                      <xsl:variable name="colour">
                        <xsl:choose>
                          <xsl:when test="position() mod 2">
                            <xsl:text>odd</xsl:text>
                          </xsl:when>
                          <xsl:otherwise>
                            <xsl:text>even</xsl:text>
                          </xsl:otherwise>
                        </xsl:choose>
                      </xsl:variable>
                      <tr class="{$colour}">
                        <xsl:apply-templates select="label" />
                        <xsl:apply-templates select="text" />
                        <xsl:apply-templates select="following-sibling::*[1]" />
                        <xsl:apply-templates select="following-sibling::*[2]" />
                      </tr>
                    </xsl:for-each>
            </xsl:template>
    
            <xsl:template match="label">
              <td><xsl:value-of select="." /></td>
            </xsl:template>
    
            <xsl:template match="text">
              <td><xsl:value-of select="." /></td>
            </xsl:template>
    
    </xsl:stylesheet>
  • Dan Sunesen 45 posts 145 karma points
    Feb 11, 2013 @ 20:18
    Dan Sunesen
    0

    Sry, Kim - Didn't do the trick :-/

    I'll try and have a look at grouping

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Feb 12, 2013 @ 00:09
    Chriztian Steinmeier
    100

    Hi Dan,

    Here's a modified version of the ImportProduct template to do the trick, with the important parts highlighted:

    <xsl:template match="ImportProduct">
        <xsl:variable name="item" select="ItemFamilies/ItemFamily[ItemFamCat = 'Size and measurements'][1]" />
        <xsl:for-each select="$item/CharsetTEXT/root/row">
            <xsl:variable name="rowNumber" select="@no" />
            <tr class="odd" style="background:#eee;">
                <xsl:if test="position() mod 2 = 0">
                    <xsl:attribute name="class">even</xsl:attribute>
                    <xsl:attribute name="style">background:#cf0;</xsl:attribute>
                </xsl:if>
                <xsl:apply-templates select="label" />
                <xsl:apply-templates select="text" />
                <xsl:apply-templates select="$item/following-sibling::ItemFamily[ItemFamCat = 'Size and measurements']/CharsetTEXT/root/row[@no = $rowNumber]/text" />
            </tr>
        </xsl:for-each>
    </xsl:template>

    Feel free to ask about how it works, if you want :-)

    /Chriztian

  • Dan Sunesen 45 posts 145 karma points
    Feb 12, 2013 @ 08:36
    Dan Sunesen
    0

    Hey Chriz!

    Thanks a lot, and it makes perfect sense!

Please Sign in or register to post replies

Write your reply to:

Draft