【问题标题】:Modify attribute value in XSLT and call template again for second time修改XSLT中的属性值并再次调用模板第二次
【发布时间】:2010-10-14 22:39:36
【问题描述】:

我正在使用 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>

【问题讨论】:

  • 不清楚你想要什么。请提供要应用转换的源 XML 文档。此外,对于这个确切的 XML 文档,提供了所需的转换结果。最后,描述转换所需的属性。
  • 只是未成年人。我已经看到您使用模式进行的编辑。我认为这并没有帮助您减少样式表逻辑。那一定是因为那些命名模板...我认为您的样式表需要使用更多模式匹配进行重构...此外,混合数据和样式信息不是一个好习惯:您可以将该样式信息放入布局文档中,然后填充包含数据的文档。

标签: sharepoint xslt cqwp


【解决方案1】:

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

【讨论】:

  • 非常酷的...模式!我在 SharePoint 原始 XSLT 中注意到了这些模式,但我不知道它们的用途。
  • 我所做的是“复制”我的项目模板。给它一个新名称,但模式不同。并确保当我第二次调用 apply-templates 时,我提供了“table”模式
  • @Famous Nerd:我很高兴它有帮助。
猜你喜欢
  • 2023-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-31
  • 1970-01-01
  • 1970-01-01
  • 2018-01-14
  • 1970-01-01
相关资源
最近更新 更多