tags:

views:

45

answers:

2

Hi,

One of xslt stylesheets i have created has many usages of for example

<xsl:template match="ProductType">
 <xsl:value-of select="../../Title"/>
</xsl:template>

so my question is would it be more efficient to create a new nodeset manualy including just the data required or perhaps passing in using params?

What would constitute best practice for this case?

the xml would be in the form as below

<product>
<title>the title</title>
<tags>
<producttype>the product type</producttype>
<tags>
</product>

the actuall template match is as follows

<xsl:template match="ProductType" xml:space="default">
      <xsl:param name="by" select="by"/>            
      <xsl:choose>
            <xsl:when test="current()='Karaoke MP3+G'">
                  <div class="product-subhead">
                        <h3>Karaoke Song</h3>
                        <h3 class="middle">MP3+G</h3>
                        <h3>100% Legal Download</h3>
                  </div>
                  The track <xsl:value-of select="../../Title"/> is 100% legally covered by MCPS and PPL ensuring the artist <xsl:value-of select="../../Attribution"/> is paid royalties for every sale! Each karaoke track download from Mastermix Digital contains audio and graphics prompts. The song is provided in MP3+G format. Purchase is available through Paypal with all major credit cards.
            </xsl:when> 
            <xsl:when test="current()='Track'">
                  <div class="product-subhead">
                              <h3>Original Version</h3>
                              <h3 class="middle">Available in 320kbps MP3 and WAV</h3>
                              <h3>100% Legal Download</h3>
                        </div>
                        The track <xsl:value-of select="../../Title"/> is 100% legally covered by MCPS and PPL ensuring the artist <xsl:value-of select="../../Attribution"/> is paid royalties for every sale! Each track download from Mastermix Digital is the original artist version and a guaranteed dancefloor filler! The audio is provided in high quality 320kbps mp3 format. Purchase is available through Paypal with all major credit cards.                        
            </xsl:when>
            <xsl:when test="current()='Extended Floorfillers'">
                  <div class="product-subhead">
                              <h3>Extended Version</h3>
                              <h3 class="middle">Available in 320kbps MP3 and WAV</h3>
                              <h3 class="phead3">100% Legal Download</h3>
                        </div>
                        Each extended floorfiller download from Mastermix Digital contains the original artist version mixed to be just that little bit longer! <xsl:value-of select="../../Title"/> is 100% legally covered by MCPS and PPL ensuring the artist <xsl:value-of select="../../Attribution"/> is paid royalties for every sale. The audio is provided in high quality 320kbps mp3 format. Purchase is available through Paypal with all major credit cards.
            </xsl:when>
            <xsl:otherwise>                        
                  <xsl:value-of select="."/>
            </xsl:otherwise> 
      </xsl:choose>
</xsl:template>

and a copy of xml is as below

  <Tracks>
    <Track AlbumArtURL="http://api.3.esh-partner.net/GetThumbnail.ashx?ID=07167B09-6365-4908-BFE6-811039D62D5E" IsBundle="true">
      <ReportingCode>MG00089531</ReportingCode>
      <Title Language="en-gb">Complete Number Ones Collection</Title>
      <ExtendedTitle>Every UK No 1 in one download</ExtendedTitle>
      <ExplicitContent>false</ExplicitContent>
      <Attribution>Various Artists</Attribution>
      <Duration Seconds="0">00:00:00</Duration>
      <GRid>A1-0327D-MG00089531-J</GRid>
      <CatalogNr>MFEG090</CatalogNr>
      <OriginalReleaseFormat>CD</OriginalReleaseFormat>
      <OriginalReleaseDate>2009-08-04</OriginalReleaseDate>
      <ReleaseDate>2009-08-04</ReleaseDate>
      <Description Language="en-gb">
        <Long>One amazing collection! This download contains every single number 1 from the Official UK charts, since the charts began back in 1952 right up to the present day (not including the last 2 months)! That's more than 1,000 tracks, all in 320 KBPS (the highest quality, indistinguishable from quality you would get from a CD). 
Save over £600 compared by buying every download individually!</Long>
      </Description>      
      <Tags>
            <ProductType>Track</ProductType>
      </Tags>
      <Attributes>
        <Attribute Type="Text" Description="PriceCode">AA</Attribute>
        <Attribute Type="LookupValue" Description="Category">MasterMix</Attribute>
      </Attributes>
      <Contributors />
      <PriceBand>Mid/Front Premium</PriceBand>
      <RecompilationRights>Yes</RecompilationRights>
    </Track>
  </Tracks>
A: 

If you refer to the usage of ../.. in your XPath it's nothing wrong with that in my opinion.

But if you use the product/title in many sub templates to the product template, one way to solve it would be to use a tunneled parameter:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

  <xsl:template match="product">
    <!-- Do some other stuff here maybe... -->
    <xsl:apply-templates>
      <xsl:with-param name="title" select="title" tunnel="yes"/>
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="tags">
    <!-- ... and some more stuff ... -->
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="ProductType">
    <xsl:param name="title" tunnel="yes"/>
    <xsl:value-of select="$title"/>
  </xsl:template>

</xsl:stylesheet>

By using a tunneled parameter you will be able to pick it up in any of the templates for any child node to product. But the nice part is that you don't have to manually send it through all the templates don't need the title in.

Here's some more on tunneling parameters: http://www.xml.com/lpt/a/1386

However, this technique requires XSLT 2.0, but you could do the same in XSLT 1.0 but with the caveat that you need to pass the parameter through every template you're processing.

Per T
thanks for this, im currently using xslt 1.0 so will keep as is for the moment if its ok to do it ;)
Treemonkey
@Per-T: The OP hasn't provided a good example, so there is no ground to assume he will benefit from tunnel parameters. This is an advanced feature which is rarely needed and is best not to be recommended to novice and intermediate XSLT programmers, because its usage may prevent them come with a better, simplified design.
Dimitre Novatchev
@Dimitre: I can agree that the example isn't in need of tunneled parameters to be solved. But since we usually work with oversimplified examples here at SO I don't see a reason to not present one possible solution.
Per T
A: 

This stylesheet:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:h="header"
 exclude-result-prefixes="h">
    <h:header for="Karaoke MP3+G">
        <h1>Karaoke Song</h1>
        <h2>MP3+G</h2>
        <h4>The track</h4>
        <h5>Each karaoke track download from Mastermix Digital contains audio and graphics prompts. The song is provided in MP3+G format.</h5>
    </h:header>
    <h:header for="Track">
        <h1>Original Version</h1>
        <h2>Available in 320kbps MP3 and WAV</h2>
        <h4>The track</h4>
        <h5>Each track download from Mastermix Digital is the original artist version and a guaranteed dancefloor filler! The audio is provided in high quality 320kbps mp3 format.</h5>
    </h:header>
    <h:header for="Extended Floorfillers">
        <h1>Extended Version</h1>
        <h2>Available in 320kbps MP3 and WAV</h2>
        <h3 class="phead3"/>
        <h4>Each extended floorfiller download from Mastermix Digital contains the original artist version mixed to be just that little bit longer!</h4>
        <h5>The audio is provided in high quality 320kbps mp3 format.</h5>
    </h:header>
    <xsl:template match="Track">
        <xsl:variable name="vText" select="document('')/*/h:*[@for=current()/Tags/ProductType]"/>
        <xsl:choose>
            <xsl:when test="$vText">
                <div class="product-subhead">
                    <h3>
                        <xsl:value-of select="$vText/h1"/>
                    </h3>
                    <h3 class="middle">
                        <xsl:value-of select="$vText/h2"/>
                    </h3>
                    <h3>
                        <xsl:copy-of select="$vText/h3/@*"/>
                        <xsl:text>100% Legal Download</xsl:text>
                    </h3>
                </div>
                <xsl:value-of select="$vText/h4"/>
                <xsl:value-of select="Title"/> is 100% legally covered by MCPS and PPL ensuring the artist 
                <xsl:value-of select="Attribution"/> is paid royalties for every sale! 
                <xsl:value-of select="$vText/h5"/> Purchase is available through Paypal with all major credit cards.
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="Tags/ProductType"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

Output:

<div class="product-subhead">
    <h3>Original Version</h3>
    <h3 class="middle">Available in 320kbps MP3 and WAV</h3>
    <h3>100% Legal Download</h3>
</div>
The trackComplete Number Ones Collection is 100% legally covered by MCPS and PPL ensuring the artist 
Various Artists is paid royalties for every sale!
Each track download from Mastermix Digital is the original artist version and a guaranteed dancefloor filler! The audio is provided in high quality 320kbps mp3 format. Purchase is available through Paypal with all major credit cards.
Alejandro