2

我正在使用 SharePoint 内容查询 Web 部件。我有一个需要的 HTML 输出

<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 它应该呈现特定样式或输出模板。我想在行上重新运行第二个模板,我认为最简单的方法是在第一次运行后替换样式属性。

第一次运行我想为每一行渲染一系列 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>

4

1 回答 1

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>
于 2010-10-14T22:55:38.940 回答