在兄弟之后获取xpath以使用具有其值的模板

时间:2012-12-27 15:59:13

标签: html css xml xslt xpath

我有以下XSLT:

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

============&GT;指向此模板

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

这可以调用====&gt;

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

再次调用另一个模板,依次为=====&gt;

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

以下是实际XML:https://gist.github.com/4380967

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

单个行的XSL:https://gist.github.com/4389997

我无法将$ SafeLinkUrl用于以下兄弟:: * [1],因为它适用于当前项目,而不适用于直接兄弟。如何使变量也适用于兄弟姐妹?

1 个答案:

答案 0 :(得分: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]