2010-10-14 96 views
2

我正在使用SharePoint內容查詢Web部件。我有需要的HTML輸出爲修改XSLT中的屬性值並再次調用模板

<ul> 
<li> 
    <img src="{RowAttribute-Url}" /> 
</li> 
<li> 
    <img src="{RowAttribute-Url}" /> 
</li> 
</ul> 
<table> 
<tr> 
<td>{RowAttribute-Title}</td> 
<td>{RowAttribute-Title}</td> 
</tr> 
</table> 

,爲CQWP輸入XML是

<dsQueryResponse> 
<Rows> 
<Row ID="1" Title="Jane Doe" Modified="2010-10-14 14:05:14" Author="" Editor="" Created="2010-10-14 11:50:35" ArticleStartDate="2010-10-01 00:00:00" Style="OutputTemplateName" GroupStyle="DefaultHeader" __begincolumn="True" __begingroup="False"></Row> 
<Row ID="2" Title="John Doe" Modified="2010-10-14 14:05:29" Author="" Editor="" Created="2010-10-14 13:17:10" ArticleStartDate="2010-10-01 00:00:00" Style="OutputTemplateName" GroupStyle="DefaultHeader" __begincolumn="False" __begingroup="False"></Row> 
</Rows> 
</dsQueryResponse> 

所以你可以看到,Web部件將「告訴」 XSLT,它應該呈現一個特定的樣式或輸出模板。我想在行上重新運行第二個模板,我認爲最簡單的方法是在第一次運行後替換style屬性。

第一次運行我想爲每一行渲染一系列li標籤,第二遍經過我想要做trs。

是否有可能在再次調用ItemStyle模板之前使用xsl-copy替換「OutputTemplateName」?

這裏是外和內樣式表的XSLT(內作爲itemstyles)

<xsl:template name="OuterTemplate"> 
    <xsl:variable name="Rows" select="/dsQueryResponse/Rows/Row" /> 
    <xsl:variable name="RowCount" select="count($Rows)" /> 
    <xsl:variable name="IsEmpty" select="$RowCount = 0" /> 
    <div id="container"> 
    <div id="inner-container"> 
       <xsl:choose> 
        <xsl:when test="$IsEmpty"> 
         <xsl:call-template name="OuterTemplate.Empty" > 
          <xsl:with-param name="EditMode" select="$cbq_iseditmode" /> 
         </xsl:call-template> 
        </xsl:when> 
        <xsl:otherwise> 
         <xsl:call-template name="OuterTemplate.Body"> 
          <xsl:with-param name="Rows" select="$Rows" /> 
          <xsl:with-param name="FirstRow" select="1" /> 
          <xsl:with-param name="LastRow" select="$RowCount" /> 
         </xsl:call-template>       
        </xsl:otherwise> 
       </xsl:choose>   
    </div> 
    </div> 
</xsl:template> 


<xsl:template name="OuterTemplate.Body"> 
    <xsl:param name="Rows" /> 
    <xsl:param name="FirstRow" /> 
    <xsl:param name="LastRow" /> 
    <div id="container-rotator"> 
    <ul> 
     <xsl:for-each select="$Rows"> 
      <xsl:variable name="CurPosition" select="position()" /> 
      <xsl:if test="($CurPosition &gt;= $FirstRow and $CurPosition &lt;= $LastRow)">    
       <xsl:call-template name="OuterTemplate.CallItemTemplate"> 
        <xsl:with-param name="CurPosition" select="$CurPosition" /> 
       </xsl:call-template>    
      </xsl:if> 
     </xsl:for-each>   
    </ul> 
    </div> 
    <h5>  
    <xsl:value-of select="ddwrt:FormatDateTime(string(/dsQueryResponse/Rows/Row[1]/@ArticleStartDate), 1033, 'MMMM')"/></h5> 
    <table> 
    <!-- before calling this foreach.. would i do the copy? --> 
    <xsl:for-each select="$Rows"> 
     <xsl:variable name="CurPosition" select="position()" /> 
     <xsl:if test="($CurPosition &gt;= $FirstRow and $CurPosition &lt;= $LastRow)"> 
     <xsl:call-template name="OuterTemplate.CallItemTemplate"> 
      <xsl:with-param name="CurPosition" select="$CurPosition" /> 
     </xsl:call-template>    
     </xsl:if> 
    </xsl:for-each> 
    </table> 
    <div> 
    <a title="" href="/sites/sitename/Pages/page.aspx" target="">A Page Link</a> 
    </div> 
    <div> 
    <a href="#">Another Page</a> 
    </div> 
</xsl:template> 

而那麼項目模板低於這是我想幾乎複製,而是使用TRS,並且還提供了新的「風格」

<xsl:template name="OutputTemplateName" match="Row[@Style='OutputTemplateName']" mode="itemstyle"> 
    <xsl:param name="CurPos" /> 
    <xsl:choose>  
    <xsl:when test="$CurPos = 1"> 
     <li class="show"> 
     <img src="{$SafeImageUrl}" /> 
     </li> 
    </xsl:when> 
    <xsl:otherwise> 
     <li> 
     <img src="{$SafeImageUrl}" /> 
     </li>  
    </xsl:otherwise> 
    </xsl:choose> 
    </xsl:template> 

所以模板,總的來說,我有一系列的行,我我想先放入一個無序列表,然後再放入一個表格......所有這些都以HTML格式輸出,但我只能將它發送到一個外部變換中。

即使任何概念性建議都會有所幫助。我會繼續更新這篇文章,因爲我發現更多。

下面我用的接受的答案做以下**

使用回答以下我將闡述需要上述的變化。

在主包裝xslt我做了以下。

<xsl:template name="OuterTemplate.CallItemTemplate"> 
<xsl:param name="CurPosition" /> 
<xsl:param name="Mode" /> 
<xsl:choose> 
    <xsl:when test="$Mode = 'table'"> 
    <xsl:apply-templates select="." mode="table"> 
     <xsl:with-param name="CurPos" select="$CurPosition" /> 
    </xsl:apply-templates> 
    </xsl:when> 
    <xsl:when test="$Mode = 'listitem'"> 
    <xsl:apply-templates select="." mode="listitem"> 
     <xsl:with-param name="CurPos" select="$CurPosition" /> 
    </xsl:apply-templates> 
    </xsl:when> 
    <xsl:otherwise> 
    <xsl:apply-templates select="." mode="itemstyle"> 
    </xsl:apply-templates> 
    </xsl:otherwise> 
</xsl:choose> 

然後我有兩個項目模板,而不是一個。

<xsl:template name="TemplateNameList" match="Row[@Style='TemplateName']" mode="listitem"> 
<xsl:param name="CurPos" /> 
<xsl:variable name="SafeImageUrl"> 
    <xsl:call-template name="OuterTemplate.GetSafeStaticUrl"> 
    <xsl:with-param name="UrlColumnName" select="'ImageUrl'"/> 
    </xsl:call-template> 
</xsl:variable> 
<xsl:choose> 
    <xsl:when test="$CurPos = 1"> 
    <li class="show"> 
     <img src="{$SafeImageUrl}" /> 
    </li> 
    </xsl:when> 
    <xsl:otherwise> 
    <li> 
     <img src="{$SafeImageUrl}" /> 
    </li> 
    </xsl:otherwise> 
</xsl:choose> 

+0

尚不清楚你想要什麼。請提供要應用轉換的源XML文檔。另外,對於這個完全XML文檔提供了轉換所需的結果。最後,描述轉換的所需屬性。 – 2010-10-14 22:44:50

+0

當然,我會看看我能做些什麼 – 2010-10-14 22:50:36

+0

只是未成年人。我已經看到你的模式編輯。我認爲這並沒有幫助你減少樣式表邏輯。這一定是因爲那些名爲模板...我認爲你的樣式表需要重構更多的模式匹配...另外,混合數據和樣式信息並不是一個好的做法:你可以將該樣式信息放入佈局文檔中,然後填充該文件與數據。 – 2010-10-15 22:29:52

回答

3

http://www.w3.org/TR/xslt#modes

模式允許的元件要被處理 多次,每次產生一個 不同的結果。

這個樣式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="Rows"> 
     <ul> 
      <xsl:apply-templates/> 
     </ul> 
     <table> 
      <xsl:apply-templates mode="table"/> 
     </table> 
    </xsl:template> 
    <xsl:template match="Row"> 
     <li> 
      <xsl:value-of select="@Data1"/> 
     </li> 
    </xsl:template> 
    <xsl:template match="Row" mode="table"> 
     <tr> 
      <xsl:apply-templates select="@*" mode="table"/> 
     </tr> 
    </xsl:template> 
    <xsl:template match="Row/@*" mode="table"> 
     <td> 
      <xsl:value-of select="."/> 
     </td> 
    </xsl:template> 
</xsl:stylesheet> 

有了這個輸入:

<Rows> 
    <Row Style="OutputTemplateName" Data1="Data1Value" Data2="Data2Value" /> 
    <Row Style="OutputTemplateName" Data1="Data1Value" Data2="Data2Value" /> 
</Rows> 

輸出:

<ul> 
    <li>Data1Value</li> 
    <li>Data1Value</li> 
</ul> 
<table> 
    <tr> 
     <td>OutputTemplateName</td> 
     <td>Data1Value</td> 
     <td>Data2Value</td> 
    </tr> 
    <tr> 
     <td>OutputTemplateName</td> 
     <td>Data1Value</td> 
     <td>Data2Value</td> 
    </tr> 
</table> 
+0

很酷......模式!我注意到SharePoint原始XSLT中的這些模式,但我不知道它們的用途。 – 2010-10-14 23:05:37

+0

我所做的是「重複」我的項目模板。給它一個新的名字,但不同的模式。並確保當我第二次調用應用模板時,我提供了「表格」 – 2010-10-14 23:31:27

+0

@Famous Nerd模式:我很高興它是有用的。 – 2010-10-14 23:33:10