2012-12-27 29 views
1

我有以下XSLT:得到的XPath以下同級利用模板,其價值

 <xsl:if test="$CurPos mod 2 =1"> 
     <li style="width:604px;"> 
      <div class="content-left" style="width:300px; height:290px;float:left;"> 
       <xsl:if test="string-length($SafeImageUrl) != 0"> 
        <div class="images-area-left"> 
        <a href="{$SafeLinkUrl}" target="{$LinkTarget}"> 
         <img class="image" src="{$SafeImageUrl}" alt="{@ImageUrlAltText}" /> 
        </a> 
        <div class="Heading-desc"> 
         <span class="NewsHeading"><h4><xsl:value-of select="@Title"/></h4></span> 
         <span class="Desc"> 
          <xsl:value-of select="substring(@Comments,0,200)"/> 
          <xsl:if test="string-length(@Comments) &gt; 200">…</xsl:if> 
<a href="{$SafeLinkUrl}" class="ReadMore"> Read More</a>  
         </span> 
        </div> 
       </div> 
       </xsl:if>    
      </div> 

      <div class="content-right" style="float:right; width:300px; height:290px;"> 
       <xsl:value-of select="following-sibling::*[1]/@PublishingRollupImage" disable-output-escaping="yes" /> 
       <span class="NewsHeading"><h4><xsl:value-of select="following-sibling::*[1]/@Title"/></h4></span> 
       <span class="Desc" style="display:block; width:280px;"><xsl:value-of select="substring(following-sibling::*[1]/@Comments,0,200)"/> 
       <xsl:if test="string-length(following-sibling::*[1]/@Comments) &gt; 200">…</xsl:if><a href="{$SafeLinkUrl}" class="ReadMore"> Read More</a>  
       </span> 
      </div> 
     </li> 
    </xsl:if> 

有一個變量$ SafeLinkUrl從另一個模板獲取當前行的頁面URL來了。由於我在繼續使用兄弟節點時仍處於當前節點上,因此無法獲取以下兄弟節點的URL。

<xsl:variable name="SafeLinkUrl"> 
    <xsl:call-template name="OuterTemplate.GetSafeLink"> 
     <xsl:with-param name="UrlColumnName" select="'LinkUrl'"/> 
    </xsl:call-template> 
    </xsl:variable> 

============>指向這個模板

<xsl:template name="OuterTemplate.GetSafeLink"> 
    <xsl:param name="UrlColumnName"/> 
    <xsl:if test="$UseCopyUtil = 'True'"> 
     <xsl:value-of select="concat($RootSiteRef,'/_layouts/CopyUtil.aspx?Use=id&amp;Action=dispform&amp;ItemId=',@ID,'&amp;ListId=',@ListId,'&amp;WebId=',@WebId,'&amp;SiteId=',$SiteId,'&amp;Source=',$Source)"/> 
    </xsl:if> 
    <xsl:if test="$UseCopyUtil != 'True'"> 
     <xsl:call-template name="OuterTemplate.GetSafeStaticUrl"> 
      <xsl:with-param name="UrlColumnName" select="$UrlColumnName"/> 
     </xsl:call-template> 
    </xsl:if> 
</xsl:template> 

,這有一個調用====>

<xsl:template name="OuterTemplate.GetSafeStaticUrl"> 
    <xsl:param name="UrlColumnName"/> 
    <xsl:variable name="Url"> 
     <xsl:call-template name="OuterTemplate.FormatColumnIntoUrl"> 
      <xsl:with-param name="UrlColumnName" select="$UrlColumnName"/> 
     </xsl:call-template> 
    </xsl:variable> 
    <xsl:value-of select="cmswrt:EnsureIsAllowedProtocol($Url)"/> 
</xsl:template> 

哪再打電話給另一個模板=====>

<xsl:template name="OuterTemplate.FormatColumnIntoUrl"> 
    <xsl:param name="UrlColumnName"/> 
    <xsl:variable name="Value" select="@*[name()=$UrlColumnName]"/> 
    <xsl:if test="contains($DataColumnTypes,concat(';',$UrlColumnName,',URL;'))"> 
     <xsl:call-template name="OuterTemplate.FormatValueIntoUrl"> 
      <xsl:with-param name="Value" select="$Value"/> 
     </xsl:call-template> 
    </xsl:if> 
    <xsl:if test="not(contains($DataColumnTypes,concat(';',$UrlColumnName,',URL;')))"> 
     <xsl:value-of select="$Value"/> 
    </xsl:if> 
</xsl:template> 

這裏是實際的X ML:https://gist.github.com/4380967

XSL的容器:https://gist.github.com/4389989

XSL對個人行:https://gist.github.com/4389997

我無法使用$ SafeLinkUrl以下同胞:: * [1],因爲它是適用目前的項目,而不是直接的兄弟姐妹。我該如何製作適用於兄弟姐妹的變量?

+0

您能向我們展示一個您使用XSLT處理的輸入XML示例嗎?你發佈的代碼的上下文節點是什麼? –

+0

https://gist.github.com/4380967 – Athapali

+1

那麼,什麼是問題?我沒看到一個。什麼是源XML文檔?什麼是想要的結果?什麼是實際結果?它有什麼問題?您能否提供一個完整但很簡單的示例,以便我們可以複製+粘貼到我們的XSLT IDE中並運行轉換並重新生成結果? *然後*這將是一個很好的問題。 –

回答

0

使用

<xsl:variable name="SafeLinkUrl"> 
    <xsl:for-each select="following-sibling::*[1]"> 
    <xsl:call-template name="OuterTemplate.GetSafeLink"> 
     <xsl:with-param name="UrlColumnName" select="'LinkUrl'"/> 
    </xsl:call-template> 
    </xsl:for-each> 
    </xsl:variable> 

甚至更​​好,第二個參數添加到您的模板:

<xsl:template name="OuterTemplate.GetSafeLink"> 
    <xsl:param name="UrlColumnName"/> 
    <xsl:param name="pContext" select="."/> 

    <xsl:if test="$UseCopyUtil = 'True'"> 
     <xsl:value-of select="concat($RootSiteRef,'/_layouts/CopyUtil.aspx?Use=id&amp;Action=dispform&amp;ItemId=', $pContext/@ID,'&amp;ListId=',$pContext/@ListId,'&amp;WebId=',$pContext/@WebId,'&amp;SiteId=',$pContext/$SiteId,'&amp;Source=',$Source)"/> 
    </xsl:if> 
    <xsl:if test="$UseCopyUtil != 'True'"> 
     <xsl:call-template name="OuterTemplate.GetSafeStaticUrl"> 
      <xsl:with-param name="UrlColumnName" select="$UrlColumnName"/> 
     </xsl:call-template> 
    </xsl:if> 
</xsl:template> 

這樣做對其他兩個模板。在調用兩個內部模板中的任何一個時指定$pContext參數。

然後調用指定$pContext參數爲following-sibling::*[1]的最外層模板。

+0

非常感謝。工作完美! – Athapali

+0

@SarikaThapaliya,不客氣。 –