修改XSLT中的属性值并再次调用模板
我正在使用 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 >= $FirstRow and $CurPosition <= $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 >= $FirstRow and $CurPosition <= $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>
I'm working with the SharePoint content query web part. I have an HTML output which is desired to be
<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>
and the input xml for the CQWP is
<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>
So you can see that the web part will "tell" the xslt that it should render a particular style or output template. I'd like to re-run a second template on the rows and I thought that the easiest way to do this would be to replace the style attribute after the first run-through.
The first run through I'd like to render a series of li tags for each row and the second go through i'd like to do trs.
Is it possible to use xsl-copy to replace the "OutputTemplateName" prior to calling the ItemStyle template again?
Here is the xslt for the outer and inner stylesheets (inner being the 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 >= $FirstRow and $CurPosition <= $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 >= $FirstRow and $CurPosition <= $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>
And then the item template is below this is the template that i'd like to nearly duplicate but instead use trs and also provide a new "Style"
<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>
So, in summary, I have a series of rows that i'd like to put into firstly, an unordered list and then later into a table... all of this is outputted as HTML but i can only send it into one outer transform.
Even any conceptual suggestions would be helpful. I'll continue to update this post as I discover more.
BELOW I USED THE ACCEPTED ANSWER TO DO THE FOLLOWING **
Using the answer below I'll illustrate the changes needed to the above.
In the main wrapper xslt I did the following.
<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>
And then i have two item templates instead of one.
<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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自 http://www.w3.org/TR/xslt#modes
此样式表:
使用此输入:
输出:
From http://www.w3.org/TR/xslt#modes
This stylesheet:
With this input:
Output: