使用 XSLT 1.0 & 创建表连接多个值 [英] Create A Table w/ XSLT 1.0 & Concatenate Multiple Values

查看:31
本文介绍了使用 XSLT 1.0 & 创建表连接多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我需要使用 xslt 1.0 创建一个动态表.我看过几篇文章/帖子,但我似乎无法把它们放在一起.在许多情况下,会有几个表格单元格的数据为空.在其他情况下,将有多个值需要连接到一个单元格中.这是示例 XML:

So, I need to create a dynamic table using xslt 1.0. I've looked at several articles/posts but I can't seem to put it all together. In many instances, there will be several table cells with empty data. In other instances, there will be multiple values that need to concatenated into one cell. Here is sample XML:

<Document>
<Records>
  <User>
    <Name>User1</Name>
    <list xid="data.set">
      <Data>
        <Column>2</Column>
        <Type>Carbs</Type>
        <RowValue>Oatmeal</RowValue>
      </Data>
      <Data>
        <Column>1</Column>
        <Type>Protein</Type>
        <RowValue>sausage</RowValue>
        <RowValue>eggs</RowValue>
        <RowValue>turkey</RowValue>
      </Data>
    </list>
  </User>
  <User>
    <Name>User2</Name>
    <list xid="data.set">
      <Data>
        <Type>Vegetables</Type>
        <Column>8</Column>
        <RowValue>Squash</RowValue>
      </Data>
      <Data>
        <Column>3</Column>
        <Type>Sweets</Type>
        <RowValue>cake</RowValue>
        <RowValue>cookies</RowValue>
      </Data>
      <Data>
        <Column>5</Column>
        <Type>Other</Type>
      </Data>
      <Data>
        <Column>6</Column>
        <Type>Beverage</Type>
        <RowValue>grape juice</RowValue>
      </Data>
    </list>
    </User>
  <User>
    <Name>User4</Name>
    <list xid="data.set">
        <Data>
          <Column>7</Column>
          <Type>Fats</Type>
          <RowValue>cashews</RowValue>
        </Data>
        <Data>
          <Column>8</Column>
          <Type>Vegetables</Type>
          <RowValue>Green Beans</RowValue>
        </Data>
        <Data>
          <Column>2</Column>
          <Type>Carbs</Type>
          <RowValue>Brown Rice</RowValue>
        </Data>
      </list>
    </User>
    <User>
      <Name>User5</Name>
      <list xid="data.set">
        <Data>
          <Column>3</Column>
          <Type>Sweets</Type>
          <RowValue>gummy worms</RowValue>
        </Data>
        <Data>
          <Column>4</Column>
          <Type>Fruit</Type>
          <RowValue>apples</RowValue>
        </Data>
      </list>
    </User>
    <User>
      <Name>User5</Name>
      <list xid="data.set">
        <Data>
          <Column>3</Column>
          <Type>Sweets</Type>
          <RowValue>gummy worms</RowValue>
        </Data>
        <Data>
          <Column>4</Column>
          <Type>Fruit</Type>
          <RowValue>grapes</RowValue>
        </Data>
      </list>
    </User>
    <User>
      <Name>User5</Name>
      <list xid="data.set">
        <Data>
          <Column>3</Column>
          <Type>Sweets</Type>
          <RowValue>gummy worms</RowValue>
        </Data>
        <Data>
          <Column>4</Column>
          <Type>Fruit</Type>
          <RowValue>grapes</RowValue>
        </Data>
      </list>
    </User>
</Records>
</Document>

在 HTML 输出中,当用户名存在多次时,表格单元格中有重复的值(见下文).我想删除每个单元格中存在的重复值,例如gummy worms, gummy worms, gummy worms".例如,列出了三个users5".我想保留包含user5"的每一行,但删除这些行的每个单元格中的重复值.

EDIT : In HTML output there are duplicate values in table cells when the username exists more than once (see below). I would like to remove the duplicate values present in each cell such as "gummy worms, gummy worms, gummy worms." For example, there are three "users5" listed. I would like to keep each row containing "user5" but remove the duplicate values in each of the cells for those row.

<table border="1">
<thead>
<tr>
<th>
</th>
<th>Protein</th>
<th>Carbs</th>
<th>Sweets</th>
<th>Fruit</th>
<th>Other</th>
<th>Beverage</th>
<th>Fats</th>
<th>Vegetables</th>
</tr>
<tr>
<th></th>
<th>1</th>
<th>2</th>
<th>3</th>
<th>4</th>
<th>5</th>
<th>6</th>
<th>7</th>
<th>8</th>
</tr></thead><tbody><tr><th>User1</th>
<td>sausage, eggs, turkey</td>
<td>Oatmeal</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr><th>User2</th>
<td></td>
<td></td>
<td>cake, cookies</td>
<td></td>
<td></td>
<td>grape juice</td>
<td></td>
<td>Squash</td>
</tr>
<tr><th>User4</th>
<td></td>
<td>Brown Rice</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td>cashews</td>
<td>Green Beans</td>
</tr>
<tr><th>User5</th>
<td></td>
<td></td>
<td>gummy worms, gummy worms, gummy worms</td>
<td>apples, grapes, grapes</td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr><th>User5</th>
<td></td>
<td></td>
<td>gummy worms, gummy worms, gummy worms</td>
<td>apples, grapes, grapes</td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr><th>User5</th>
<td></td>
<td></td>
<td>gummy worms, gummy worms, gummy worms</td>
<td>apples, grapes, grapes</td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>

这是我尝试完成这项工作的非常微弱的尝试.我是新手,请温柔...

<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:key name="food-by-Category" match="Data" use="Column" />
 <xsl:key name="food-by-Value" match="Columns" use="RowValue" />

  <xsl:template match="/Document/Records/User/list[@xid = 'data.set']">

 <table><tr>
    <xsl:for-each select="Data[count(. | key('food-by-Category', Column)[1]) = 1]">
      <!-- Sort by the Category -->
      <xsl:sort select="Column" />
 <td>
      <xsl:value-of select="Type" />
</td>
</xsl:for-each>
</tr>

  <xsl:for-each select="Data[count(. | key('food-by-Category', Column)[1]) = 1]">
 <tr>
      <xsl:for-each select="key('food-by-Category', Column)">
         <!-- Sort by the item Value -->
        <xsl:sort select="RowValue" />
<td>
        <xsl:value-of select="RowValue" />
</td>      
    </xsl:for-each>
 </tr>
</xsl:for-each>
</table>
  </xsl:template>
</xsl:stylesheet>

如您所见,当有多个 < 时,它不会将表格标题/单元格组合在一起或连接值.行值 >.

As you can see it's not grouping the table headers/cells together or concatenating the values when there are multiple < RowValue >.

推荐答案

哇.我认为你至少有三个不同的问题值得在那里:

Wow. I think you have at least three different questions' worth in there:

  1. 如何生成唯一的列标题;
  2. 如何将稀疏数组中的数据放入表中;
  3. 如何将多个匹配的数据项连接到一个单元格中.

如果这还不够复杂,您可以通过修改最后一个用户的数据(使 成为 而不是兄弟姐妹).

If that wasn't complicated enough, you made sure it would be by mangling the last user's data (making <Name> a child of <list> instead of a sibling).

无论如何,看看下面的样式表:

Anyway, have a look at the following stylesheet:

<?xml version="1.0" encoding="utf-8"?>
<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="data_by_column" match="Data" use="Column" />
<xsl:key name="value_by_cell" match="RowValue" use="concat(ancestor::User/Name, '|', preceding-sibling::Column)" />

<xsl:template match="/">
<table border="1">
<thead>
    <tr>
        <th/>
        <!--unique column headers -->
        <xsl:for-each select="Document/Records/User/list/Data[count(. | key('data_by_column', Column)[1]) = 1]">
        <xsl:sort select="Column" data-type="number" order="ascending"/>
        <th><xsl:value-of select="Type"/></th>
        </xsl:for-each>
    </tr>
    <tr>
        <th/>
        <!--unique column headers -->
        <xsl:for-each select="Document/Records/User/list/Data[count(. | key('data_by_column', Column)[1]) = 1]">
        <xsl:sort select="Column" data-type="number" order="ascending"/>
        <th><xsl:value-of select="Column"/></th>
        </xsl:for-each>
    </tr>
</thead>
<tbody>
    <xsl:for-each select="Document/Records/User">
    <xsl:variable name="row" select="Name" />
    <tr>
        <th><xsl:value-of select="$row"/></th>
        <!-- for each unique column header -->
        <xsl:for-each select="/Document/Records/User/list/Data[count(. | key('data_by_column', Column)[1]) = 1]">
        <xsl:sort select="Column" data-type="number" order="ascending"/>
        <xsl:variable name="col" select="Column" />
        <!-- new cell -->
        <td>
            <!-- get matching data -->
            <xsl:for-each select="key('value_by_cell', concat($row, '|', $col))">
                <xsl:value-of select="."/>
                <xsl:if test="position()!=last()">
                    <xsl:text>, </xsl:text>
                </xsl:if>
            </xsl:for-each>     
        </td>
        </xsl:for-each>
    </tr>
    </xsl:for-each>
</tbody>
</table>
</xsl:template>
</xsl:stylesheet>

注意:我本可以先将相同的代码转储到变量中,从而避免为唯一的列标题重复三遍相同的代码,但我没时间了.

Note: I could have probably eliminated repeating the same code for unique column headers three times by dumping it into a variable first, but I ran out of time.

这篇关于使用 XSLT 1.0 &amp; 创建表连接多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆