使用xslt获取内容查询web部件中的最后一项(SharePoint 2010)

使用xslt获取内容查询web部件中的最后一项(SharePoint 2010),xslt,sharepoint-2010,Xslt,Sharepoint 2010,这是我第一次使用内容查询Web部件和xslt。我必须以以下方式显示博客中的最后5篇文章(请看下图): 该表包含两列。在左栏我想显示最后一篇文章,在右栏我想显示一个特定博客的其余文章 编辑1 源代码只是一个OOTB博客站点,我在其中添加了一些帖子。我想在包含两列的html表中显示这些帖子。在左栏中,我想显示博客中输入的最后一篇文章,在右栏中,我想循环浏览其他文章。用户必须指定他/她希望在CQWP中看到的帖子数量 编辑2 这是我迄今为止创建的xslt。唯一的想法是这个表被重复了很多次,我不希望这样。

这是我第一次使用内容查询Web部件和xslt。我必须以以下方式显示博客中的
最后5篇文章
(请看下图):

该表包含两列。在左栏我想显示最后一篇文章,在右栏我想显示一个特定博客的其余文章

编辑1

源代码只是一个OOTB博客站点,我在其中添加了一些帖子。我想在包含两列的html表中显示这些帖子。在左栏中,我想显示博客中输入的最后一篇文章,在右栏中,我想循环浏览其他文章。用户必须指定他/她希望在CQWP中看到的帖子数量

编辑2

这是我迄今为止创建的xslt。唯一的想法是这个表被重复了很多次,我不希望这样。应该只有一张桌子。如果您查看xslt代码,我已经手动输入了一些文本,比如Test1、test2。。。在他们的位置,我想显示其余的博客文章

<xsl:template name="Post" match="Row[@Style='Post']" mode="itemstyle">
    <xsl:variable name="SafeLinkUrl">
        <xsl:call-template name="OuterTemplate.GetSafeLink">
            <xsl:with-param name="UrlColumnName" select="'LinkUrl'"/>
        </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="DisplayTitle">
        <xsl:call-template name="OuterTemplate.GetTitle">
            <xsl:with-param name="Title" select="@Title"/>
            <xsl:with-param name="UrlColumnName" select="'LinkUrl'"/>
        </xsl:call-template>
    </xsl:variable>
    <div>        
        <table width="100%" cellpadding="2" cellspacing="2" border="1">
            <tr>
                <td colspan="2" valign="top">
                   <xsl:value-of select="@Author"/></td>
                <td rowspan="2" valign="top" width="30%">
                    <div>
                        <b>Previous blog posts:</b>
                    </div>
                    <div>
                        <ul style="margin-left:-2px;">
                        <li>Test 1</li>
                        <li>Test 2</li>
                        <li>Test 3</li>
                        <li>Test 4</li>
                        </ul>
                    </div>
                    </td>
            </tr>
            <tr>
                <td width="15%" valign="top">
                    image</td>
                <td  valign="top">
                     <div>
                        <xsl:value-of select="@Title"/>
                     </div>  
                     <div class="custom_description"> 
                        <xsl:value-of select="@Body" disable-output-escaping="yes" /> 
                    </div>                  
                    <p>
                    <xsl:call-template name="OuterTemplate.CallPresenceStatusIconTemplate"/>
                    <a href="{$SafeLinkUrl}">
                      <xsl:if test="$ItemsHaveStreams = 'True'">
                        <xsl:attribute name="onclick">
                          <xsl:value-of select="@OnClickForWebRendering"/>
                        </xsl:attribute>
                      </xsl:if>
                      <xsl:if test="$ItemsHaveStreams != 'True' and @OpenInNewWindow = 'True'">
                        <xsl:attribute name="onclick">
                          <xsl:value-of disable-output-escaping="yes" select="$OnClickTargetAttribute"/>
                        </xsl:attribute>
                      </xsl:if>
                      <br />
                      <b>Read More &gt;</b>
                    </a>
                    </p>
                </td>
            </tr>
        </table>                   
    </div>
</xsl:template>

以前的博客帖子:
  • 测试1
  • 测试2
  • 测试3
  • 测试4
形象

编辑3

<root>
   <Posts>
        <Post ID="1">
         <Title>this post 1</Title>
         <Body>The comment comes here</Body>
    </Post>
<Post ID="2">
         <Title>this post 2</Title>
         <Body>The comment comes here</Body>
    </Post>
<Post ID="3">
         <Title>this post 3</Title>
         <Body>The comment comes here</Body>
    </Post>
<Post ID="4">
         <Title>this post 4</Title>
         <Body>The comment comes here</Body>
    </Post>
<Post ID="5">
         <Title>this post 5</Title>
         <Body>The comment comes here</Body>
    </Post>

   </Posts>

</root>

这个帖子是1
评论就在这里
这个帖子2
评论就在这里
这是第3条
评论就在这里
这个帖子是4
评论就在这里
这个帖子是5
评论就在这里
多谢各位


首先让我警告一下emptor:我对SharePoint一无所知,而且我没有安装SharePoint。有一个StackExchange网站专门处理SharePoint问题,该网站可能会为您提供更好的服务。看

你也应该看一看


更新

此XSLT 1.0样式表适用于MS XSLT处理器…

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:msxsl="urn:schemas-microsoft-com:xslt"
  exclude-result-prefixes="xsl msxsl">
<xsl:output method="html" doctype-system="" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*" />

<xsl:variable name="empty-main">
  <empty-main>No content yet.</empty-main>
</xsl:variable>

<xsl:variable name="empty-sub">
  <empty-main>No older content yet.</empty-main>
</xsl:variable>

<xsl:template match="/*">
  <html>
  <head>
    <title>Blog</title>
    <style type="text/css">
      table {
        width: 100%;
        border: 1px solid #000;
      }
      td.main-col,  td.subsiduary-col {
        vertical-align:text-top;
      }
      td.main-col {
        width: 75%
      }
      td.subsiduary-col {
        width: 25%
      }
    </style>
  </head>

  <body>
    <table border="1">
     <tr>
     <xsl:variable name="count-posts" select="count(msxsl:node-set(Posts/Post))" />
       <td class="main-col">
         <xsl:apply-templates select="
       (Posts/Post[last()] | msxsl:node-set($empty-main))[1]" />
       </td>
       <td class="subsiduary-col">
         <xsl:apply-templates select="
       Posts/Post[position() &gt; last() - 5 and position() &lt; last()] |
       msxsl:node-set($empty-sub)[$count-posts &lt; 2]" />
       </td>
     </tr>
    </table>
  </body>
  </html>
</xsl:template>


<xsl:template match="Post">
  <xsl:apply-templates select="Title" />
  <p><xsl:copy-of select="Body/node()" /></p>
</xsl:template>

<xsl:template match="Post[last()]/Title" priority="2">
  <h1><xsl:value-of select="." /></h1>
</xsl:template>

<xsl:template match="Post/Title">
  <h2><xsl:value-of select="." /></h2>
</xsl:template>


<xsl:template match="empty-main|empty-sub" priority="2">
  <p><xsl:value-of select="." /></p>
</xsl:template>

</xsl:stylesheet>
<root>
    <Posts>
        <Post ID="1">
            <Title>this post 1</Title>
            <Body>The comment comes here</Body>
        </Post>
        <Post ID="2">
            <Title>this post 2</Title>
            <Body>The comment comes here</Body>
        </Post>
        <Post ID="3">
            <Title>this post 3</Title>
            <Body>The comment comes here</Body>
        </Post>
        <Post ID="4">
            <Title>this post 4</Title>
            <Body>The comment comes here</Body>
        </Post>
        <Post ID="5">
            <Title>this post 5</Title>
            <Body>The comment comes here</Body>
        </Post>
    </Posts>
</root>
<!DOCTYPE html SYSTEM "">
<html>
  <head>
    <META http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Blog</title>
    <style type="text/css">
      table {
        width: 100%;
        border: 1px solid #000;
      }
      td.main-col,  td.subsiduary-col {
        vertical-align:text-top;
      }
      td.main-col {
        width: 75%
      }
      td.subsiduary-col {
        width: 25%
      }
    </style>
  </head>
  <body>
    <table border="1">
      <tr>
        <td class="main-col">
          <h1>this post 5</h1>
          <p>The comment comes here</p>
        </td>
        <td class="subsiduary-col">
          <h2>this post 1</h2>
          <p>The comment comes here</p>
          <h2>this post 2</h2>
          <p>The comment comes here</p>
          <h2>this post 3</h2>
          <p>The comment comes here</p>
          <h2>this post 4</h2>
          <p>The comment comes here</p>
        </td>
      </tr>
    </table>
  </body>
</html>

还没有内容。
还没有旧的内容。
博客
桌子{
宽度:100%;
边框:1px实心#000;
}
td.main-col,td.subsiduary-col{
垂直对齐:文本顶部;
}
td.main-col{
宽度:75%
}
td.SUBSDUARY-col{
宽度:25%
}

…将转换此输入…

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:msxsl="urn:schemas-microsoft-com:xslt"
  exclude-result-prefixes="xsl msxsl">
<xsl:output method="html" doctype-system="" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*" />

<xsl:variable name="empty-main">
  <empty-main>No content yet.</empty-main>
</xsl:variable>

<xsl:variable name="empty-sub">
  <empty-main>No older content yet.</empty-main>
</xsl:variable>

<xsl:template match="/*">
  <html>
  <head>
    <title>Blog</title>
    <style type="text/css">
      table {
        width: 100%;
        border: 1px solid #000;
      }
      td.main-col,  td.subsiduary-col {
        vertical-align:text-top;
      }
      td.main-col {
        width: 75%
      }
      td.subsiduary-col {
        width: 25%
      }
    </style>
  </head>

  <body>
    <table border="1">
     <tr>
     <xsl:variable name="count-posts" select="count(msxsl:node-set(Posts/Post))" />
       <td class="main-col">
         <xsl:apply-templates select="
       (Posts/Post[last()] | msxsl:node-set($empty-main))[1]" />
       </td>
       <td class="subsiduary-col">
         <xsl:apply-templates select="
       Posts/Post[position() &gt; last() - 5 and position() &lt; last()] |
       msxsl:node-set($empty-sub)[$count-posts &lt; 2]" />
       </td>
     </tr>
    </table>
  </body>
  </html>
</xsl:template>


<xsl:template match="Post">
  <xsl:apply-templates select="Title" />
  <p><xsl:copy-of select="Body/node()" /></p>
</xsl:template>

<xsl:template match="Post[last()]/Title" priority="2">
  <h1><xsl:value-of select="." /></h1>
</xsl:template>

<xsl:template match="Post/Title">
  <h2><xsl:value-of select="." /></h2>
</xsl:template>


<xsl:template match="empty-main|empty-sub" priority="2">
  <p><xsl:value-of select="." /></p>
</xsl:template>

</xsl:stylesheet>
<root>
    <Posts>
        <Post ID="1">
            <Title>this post 1</Title>
            <Body>The comment comes here</Body>
        </Post>
        <Post ID="2">
            <Title>this post 2</Title>
            <Body>The comment comes here</Body>
        </Post>
        <Post ID="3">
            <Title>this post 3</Title>
            <Body>The comment comes here</Body>
        </Post>
        <Post ID="4">
            <Title>this post 4</Title>
            <Body>The comment comes here</Body>
        </Post>
        <Post ID="5">
            <Title>this post 5</Title>
            <Body>The comment comes here</Body>
        </Post>
    </Posts>
</root>
<!DOCTYPE html SYSTEM "">
<html>
  <head>
    <META http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Blog</title>
    <style type="text/css">
      table {
        width: 100%;
        border: 1px solid #000;
      }
      td.main-col,  td.subsiduary-col {
        vertical-align:text-top;
      }
      td.main-col {
        width: 75%
      }
      td.subsiduary-col {
        width: 25%
      }
    </style>
  </head>
  <body>
    <table border="1">
      <tr>
        <td class="main-col">
          <h1>this post 5</h1>
          <p>The comment comes here</p>
        </td>
        <td class="subsiduary-col">
          <h2>this post 1</h2>
          <p>The comment comes here</p>
          <h2>this post 2</h2>
          <p>The comment comes here</p>
          <h2>this post 3</h2>
          <p>The comment comes here</p>
          <h2>this post 4</h2>
          <p>The comment comes here</p>
        </td>
      </tr>
    </table>
  </body>
</html>

这个帖子是1
评论就在这里
这个帖子2
评论就在这里
这是第3条
评论就在这里
这个帖子是4
评论就在这里
这个帖子是5
评论就在这里
。输入此输出…

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:msxsl="urn:schemas-microsoft-com:xslt"
  exclude-result-prefixes="xsl msxsl">
<xsl:output method="html" doctype-system="" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*" />

<xsl:variable name="empty-main">
  <empty-main>No content yet.</empty-main>
</xsl:variable>

<xsl:variable name="empty-sub">
  <empty-main>No older content yet.</empty-main>
</xsl:variable>

<xsl:template match="/*">
  <html>
  <head>
    <title>Blog</title>
    <style type="text/css">
      table {
        width: 100%;
        border: 1px solid #000;
      }
      td.main-col,  td.subsiduary-col {
        vertical-align:text-top;
      }
      td.main-col {
        width: 75%
      }
      td.subsiduary-col {
        width: 25%
      }
    </style>
  </head>

  <body>
    <table border="1">
     <tr>
     <xsl:variable name="count-posts" select="count(msxsl:node-set(Posts/Post))" />
       <td class="main-col">
         <xsl:apply-templates select="
       (Posts/Post[last()] | msxsl:node-set($empty-main))[1]" />
       </td>
       <td class="subsiduary-col">
         <xsl:apply-templates select="
       Posts/Post[position() &gt; last() - 5 and position() &lt; last()] |
       msxsl:node-set($empty-sub)[$count-posts &lt; 2]" />
       </td>
     </tr>
    </table>
  </body>
  </html>
</xsl:template>


<xsl:template match="Post">
  <xsl:apply-templates select="Title" />
  <p><xsl:copy-of select="Body/node()" /></p>
</xsl:template>

<xsl:template match="Post[last()]/Title" priority="2">
  <h1><xsl:value-of select="." /></h1>
</xsl:template>

<xsl:template match="Post/Title">
  <h2><xsl:value-of select="." /></h2>
</xsl:template>


<xsl:template match="empty-main|empty-sub" priority="2">
  <p><xsl:value-of select="." /></p>
</xsl:template>

</xsl:stylesheet>
<root>
    <Posts>
        <Post ID="1">
            <Title>this post 1</Title>
            <Body>The comment comes here</Body>
        </Post>
        <Post ID="2">
            <Title>this post 2</Title>
            <Body>The comment comes here</Body>
        </Post>
        <Post ID="3">
            <Title>this post 3</Title>
            <Body>The comment comes here</Body>
        </Post>
        <Post ID="4">
            <Title>this post 4</Title>
            <Body>The comment comes here</Body>
        </Post>
        <Post ID="5">
            <Title>this post 5</Title>
            <Body>The comment comes here</Body>
        </Post>
    </Posts>
</root>
<!DOCTYPE html SYSTEM "">
<html>
  <head>
    <META http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Blog</title>
    <style type="text/css">
      table {
        width: 100%;
        border: 1px solid #000;
      }
      td.main-col,  td.subsiduary-col {
        vertical-align:text-top;
      }
      td.main-col {
        width: 75%
      }
      td.subsiduary-col {
        width: 25%
      }
    </style>
  </head>
  <body>
    <table border="1">
      <tr>
        <td class="main-col">
          <h1>this post 5</h1>
          <p>The comment comes here</p>
        </td>
        <td class="subsiduary-col">
          <h2>this post 1</h2>
          <p>The comment comes here</p>
          <h2>this post 2</h2>
          <p>The comment comes here</p>
          <h2>this post 3</h2>
          <p>The comment comes here</p>
          <h2>this post 4</h2>
          <p>The comment comes here</p>
        </td>
      </tr>
    </table>
  </body>
</html>

博客
桌子{
宽度:100%;
边框:1px实心#000;
}
td.main-col,td.subsiduary-col{
垂直对齐:文本顶部;
}
td.main-col{
宽度:75%
}
td.SUBSDUARY-col{
宽度:25%
}
这个帖子是5
评论就在这里

这个帖子是1 评论就在这里

这个帖子2 评论就在这里

这是第3条 评论就在这里

这个帖子是4 评论就在这里


要直观了解如何在浏览器中呈现,请将html输出粘贴到中。

根据在web部件的面板工具中选择的排序,显示第一个或最后一个项目

 <xsl:if test="count(preceding-sibling::*)=0"> 

范例

<xsl:template name="DisplayPosts" match="Row[@Style='DisplayPosts']" mode="itemstyle">    

 <table width="100%" border="1">
 <tr>
  <td width="70%">
     <xsl:if test="count(preceding-sibling::*)=0">
        <xsl:value-of select="@Title" />
     </xsl:if>
 </td>
 <td width="30%">
    <xsl:if test="count(preceding-sibling::*)&gt;0">
        <xsl:value-of select="@Title" />
  </xsl:if>             
 </td>  
 </tr>
 </table>

</xsl:template> 


请提供示例输入XML和预期输出。也许这也有帮助:嗨,肖恩,感谢上面的链接。请看上面我的编辑1。谢谢一个好的开始,但我们仍然需要示例输入XML。你能提供吗?你使用的是什么版本的SharePoint?好的,那就忘掉SharePoint吧。你熟悉xslt吗?如果是,那么下面是我想转换为html表的xml(xml和图像见上文)Hi Sean,感谢您的帮助。我会试试看,然后告诉你结果。再次感谢。