2010-10-14 97 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