从XML SharePoint列表到Html表



我在SharePoint列表视图中工作,使用Xsl,列表的格式是这样的。

<Rows>
   <Row text='text1' group='group1'/>
   <Row text='text2' group='group1'/>
   <Row text='text3' group='group1'/>
   <Row text='text4' group='group1'/>
   <Row text='text5' group='group1'/>
   <Row text='text6' group='group1'/>
   <Row text='text7' group='group1'/>
   <Row text='text8' group='group1'/>
   <Row text='text9' group='group1'/>
   <Row text='text10' group='group2'/>
   <Row text='text11' group='group2'/>
   <Row text='text12' group='group2'/>
   <Row text='text13' group='group2'/>
   <Row text='text14' group='group2'/>
   <Row text='text15' group='group2'/>
   <Row text='text16' group='group2'/>
   <Row text='text17' group='group2'/>
   <Row text='text18' group='group2'/>
   <Row text='text19' group='group2'/>
   <Row text='text20' group='group2'/>
   <Row text='text21' group='group2'/>
   <Row text='text22' group='group2'/>
   <Row text='text23' group='group2'/>
   ...
</Rows>

我需要被格式化成。

<Table>
  <td>
    <ul class='group1'>
      <li>text1</li>
      <li>text2</li>
      <li>text3</li>
    </ul>
  </td>
  <td>
    <ul class='group1'>
      <li>text4</li>
      <li>text5</li>
      <li>text6</li>
    </ul>
  </td>
  <td>
    <ul class='group1'>
     <li>text7</li>
     <li>text8</li>
     <li>text9</li>
    </ul>
  </td>
</table>
<Table>
  <td>
    <ul class='group2'>
      <li>text10</li>
      <li>text11</li>
      <li>text12</li>
      <li>text13</li>
      <li>text14</li>
    </ul>
  </td>
    <ul class='group2'>
      <li>text15</li>
      <li>text16</li>
      <li>text17</li>
      <li>text18</li>
      <li>text19</li>
    </ul>
  </td>
    <ul class='group2'>
      <li>text20</li>
      <li>text21</li>
      <li>text22</li>
      <li>text23</li>
    </ul>
  </td>
</table>

请注意,我需要根据一个字段在组中分开,然后在3个子组。我知道怎么做这两件事,但是分开做。我使用键按@group对行进行分组。这是我的实际代码。

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
  <xsl:output method="xml" indent="yes"/>
  <xsl:key name="Grouping" match="Row" use="@group" />
  <xsl:template match="/">
    <xsl:for-each select="/dsQueryResponse/Rows/Row[count(. | key('Grouping', @group)[1]) = 1]">
      <table>
            <xsl:apply-templates select="key('Grouping', @group)[position() mod ceiling(count(key('Grouping', @group)) div 3) = 1]" mode="cell"/>
      </table>
    </xsl:for-each>
  </xsl:template>
  <xsl:template match="/dsQueryResponse/Rows/Row" mode="row">
    <td>
      <ul class="{@group}">
        <xsl:apply-templates select=". | following-sibling::node()[position() &lt; ceiling(count(key('Grouping', @group)) div 3)]"
                   mode="cell" />
      </ul>
    </td>
  </xsl:template>
  <xsl:template match="/dsQueryResponse/Rows/Row" mode="cell">
        <li>
            <xsl:value-of select="@text" disable-output-escaping="no"/>
        </li>
      </xsl:template>
</xsl:stylesheet>

如果我能使用下面的-sibling::key("Grouping", @group)[bla]而不是node()[bla],我想我的问题就解决了。

如果有任何混淆或不清楚的地方,请告诉我。

编辑:Xsl版本1.0,SharePoint唯一支持的版本

这样怎么样?

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:key name="row-by-group" match="Row" use="@group" />
<xsl:template match="/">
<html>
<xsl:for-each select="Rows/Row[count(. | key('row-by-group', @group)[1]) = 1]">
    <table>
        <xsl:variable name="rows-in-group" select="key('row-by-group', @group)" />
        <xsl:variable name="size" select="ceiling(count($rows-in-group) div 3)" />
        <xsl:for-each select="$rows-in-group[position() mod $size = 1]">
            <xsl:variable name="start" select="count(preceding-sibling::Row)" />
            <td>
                <ul class="{@group}">
                    <xsl:for-each select="$rows-in-group[$start &lt;= count(preceding-sibling::Row) and count(preceding-sibling::Row) &lt; $start + $size]">
                        <li><xsl:value-of select="@text"/></li>
                    </xsl:for-each> 
                </ul>   
            </td>   
        </xsl:for-each>
    </table>
</xsl:for-each>
</html>
</xsl:template>
</xsl:stylesheet>

应用于以下输入时:

<Rows>
   <Row text='text1' group='group1'/>
   <Row text='text2' group='group1'/>
   <Row text='text3' group='group1'/>
   <Row text='text4' group='group1'/>
   <Row text='text5' group='group1'/>
   <Row text='text6' group='group1'/>
   <Row text='text7' group='group1'/>
   <Row text='text8' group='group1'/>
   <Row text='text9' group='group1'/>
   <Row text='text10' group='group2'/>
   <Row text='text11' group='group2'/>
   <Row text='text12' group='group2'/>
   <Row text='text13' group='group2'/>
   <Row text='text14' group='group2'/>
   <Row text='text15' group='group2'/>
   <Row text='text16' group='group2'/>
   <Row text='text17' group='group2'/>
   <Row text='text18' group='group2'/>
   <Row text='text19' group='group2'/>
   <Row text='text20' group='group2'/>
   <Row text='text21' group='group2'/>
   <Row text='text22' group='group2'/>
   <Row text='text23' group='group2'/>
</Rows>

结果是:

<?xml version="1.0" encoding="UTF-8"?>
<html>
   <table>
      <td>
         <ul class="group1">
            <li>text1</li>
            <li>text2</li>
            <li>text3</li>
         </ul>
      </td>
      <td>
         <ul class="group1">
            <li>text4</li>
            <li>text5</li>
            <li>text6</li>
         </ul>
      </td>
      <td>
         <ul class="group1">
            <li>text7</li>
            <li>text8</li>
            <li>text9</li>
         </ul>
      </td>
   </table>
   <table>
      <td>
         <ul class="group2">
            <li>text10</li>
            <li>text11</li>
            <li>text12</li>
            <li>text13</li>
            <li>text14</li>
         </ul>
      </td>
      <td>
         <ul class="group2">
            <li>text15</li>
            <li>text16</li>
            <li>text17</li>
            <li>text18</li>
            <li>text19</li>
         </ul>
      </td>
      <td>
         <ul class="group2">
            <li>text20</li>
            <li>text21</li>
            <li>text22</li>
            <li>text23</li>
         </ul>
      </td>
   </table>
</html>

注意输出中添加的根元素

相关内容

  • 没有找到相关文章

最新更新