2010-09-15 50 views
0

我有创建XSLT样式表中有许多用途,例如XSLT高效选择使用

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

所以我的问题是,这将是更有效地创建一个新的节点集manualy包括刚才所需要的数据或者是通过在使用参数?

什么构成这种情况下的最佳做法?

的XML将在形式如下

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

的实际工作模板匹配如下

<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> 

和XML的副本如下

<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> 
+0

这很难说什么是你的情况比较好... XSLT转换一个wellknow模式输入到wellknow模式输出。但是你没有提供任何一个。没有像一般解决方案那样的东西。除此之外,将元素转换为祖父母的子字符串值,似乎并不是一个好习惯...... – 2010-09-15 17:43:35

+0

@Alejandro我根据产品类型在产品显示器上工作我生成一些用于搜索引擎优化的文本,其中包括标题。虽然我有一个想法可能可以解决这个问题,但我会在测试时更新这个问题:) – Treemonkey 2010-09-16 09:56:26

+0

@Alejandro生病只是添加我的真实代码的副本!它不是很漂亮:P – Treemonkey 2010-09-16 09:59:38

回答

0

这个样式表:

<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> 

输出:

<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. 
0

如果你指的是XPath中../..的用法,在我看来这没什么不对。

但是如果你使用的product/title在许多子模板的product模板,一个办法来解决这个问题是使用隧道参数:

<?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> 

通过使用隧道参数,您将能够选择它可以在任何子节点的任何模板中生成。但漂亮的部分是,你不必通过所有的模板手动发送它不需要在标题

这里的多一些对掘进参数:http://www.xml.com/lpt/a/1386

然而,这种技术需要XSLT 2.0 ,但是您可以在XSLT 1.0中执行相同的操作,但需要将参数传递到您正在处理的每个模板中。

+0

感谢这一点,即时通讯使用xslt 1.0,因此,如果它可以做到这一点,将保持现有状态;) – Treemonkey 2010-09-15 15:27:52

+0

@ Per-T:OP没有提供一个很好的例子,所以有没有理由假设他将受益于隧道参数。这是一个很少需要的高级功能,最好不要推荐给新手和中级XSLT编程人员,因为它的使用可能会阻止他们使用更好的简化设计。 – 2010-09-15 17:20:11

+1

@Dimitre:我可以同意这个例子不需要隧道参数来解决。但是因为我们通常在这里用过于简单的例子来工作,所以我没有看到没有提出一个可能的解决方案的理由。 – 2010-09-15 19:58:19