【问题标题】:xsl grouping by xml element for repetitive nodes in xslt1 into html table outputxslt1中重复节点的xml元素xsl分组到html表输出
【发布时间】:2012-11-14 10:46:14
【问题描述】:

我有一个复杂的 xml 结构,看起来像:

  <Items>
      <Item>
          <ItemTexts>
            <ItemText>
              <ItemTextsType>type1</ItemTextsType>
              <ItemTextsTypeDesc>description11</ItemTextsTypeDesc>
              <ItemTextsLine>1</ItemTextsLine>
            </ItemText>
            <ItemText>
              <ItemTextsType>type1</ItemTextsType>
              <ItemTextsTypeDesc>description12</ItemTextsTypeDesc>
              <ItemTextsLine>2</ItemTextsLine>
            </ItemText>
            <ItemText>
              <ItemTextsType>type2</ItemTextsType>
              <ItemTextsTypeDesc>description21</ItemTextsTypeDesc>
              <ItemTextsLine>3</ItemTextsLine>
            </ItemText>
            <ItemText>
              <ItemTextsType>type2</ItemTextsType>
              <ItemTextsTypeDesc>description22</ItemTextsTypeDesc>
              <ItemTextsLine>4</ItemTextsLine>
            </ItemText>
          </ItemTexts>
         </Item>
    <Item>
          <ItemTexts>
            <ItemText>
              <ItemTextsType>type1</ItemTextsType>
              <ItemTextsTypeDesc>description11</ItemTextsTypeDesc>
              <ItemTextsLine>1</ItemTextsLine>
            </ItemText>
            <ItemText>
              <ItemTextsType>type1</ItemTextsType>
              <ItemTextsTypeDesc>description12</ItemTextsTypeDesc>
              <ItemTextsLine>2</ItemTextsLine>
            </ItemText>
            <ItemText>
              <ItemTextsType>type2</ItemTextsType>
              <ItemTextsTypeDesc>description21</ItemTextsTypeDesc>
              <ItemTextsLine>3</ItemTextsLine>
            </ItemText>
            <ItemText>
              <ItemTextsType>type2</ItemTextsType>
              <ItemTextsTypeDesc>description22</ItemTextsTypeDesc>
              <ItemTextsLine>4</ItemTextsLine>
            </ItemText>
          </ItemTexts>
         </Item>
<Item>
          <ItemTexts>
            <ItemText>
              <ItemTextsType>type1</ItemTextsType>
              <ItemTextsTypeDesc>description11</ItemTextsTypeDesc>
              <ItemTextsLine>1</ItemTextsLine>
            </ItemText>
            <ItemText>
              <ItemTextsType>type1</ItemTextsType>
              <ItemTextsTypeDesc>description12</ItemTextsTypeDesc>
              <ItemTextsLine>2</ItemTextsLine>
            </ItemText>
            <ItemText>
              <ItemTextsType>type2</ItemTextsType>
              <ItemTextsTypeDesc>description21</ItemTextsTypeDesc>
              <ItemTextsLine>3</ItemTextsLine>
            </ItemText>
            <ItemText>
              <ItemTextsType>type2</ItemTextsType>
              <ItemTextsTypeDesc>description22</ItemTextsTypeDesc>
              <ItemTextsLine>4</ItemTextsLine>
            </ItemText>
          </ItemTexts>
         </Item>
    <Item>
          <ItemTexts>
            <ItemText>
              <ItemTextsType>type3</ItemTextsType>
              <ItemTextsTypeDesc>description31</ItemTextsTypeDesc>
              <ItemTextsLine>1</ItemTextsLine>
            </ItemText>
            <ItemText>
              <ItemTextsType>type3</ItemTextsType>
              <ItemTextsTypeDesc>description32</ItemTextsTypeDesc>
              <ItemTextsLine>2</ItemTextsLine>
            </ItemText>
            <ItemText>
              <ItemTextsType>type2</ItemTextsType>
              <ItemTextsTypeDesc>description21</ItemTextsTypeDesc>
              <ItemTextsLine>3</ItemTextsLine>
            </ItemText>
            <ItemText>
              <ItemTextsType>type2</ItemTextsType>
              <ItemTextsTypeDesc>description22</ItemTextsTypeDesc>
              <ItemTextsLine>4</ItemTextsLine>
            </ItemText>
          </ItemTexts>
         </Item>
    </Items>

每个&lt;Item&gt; 都有更多的 xml 元素,但我现在不会指定它们。

我在每个项目上都使用 xsl 运行,例如:

<xsl:for-each select="Items/Item">

我为每个&lt;Item&gt; 和每行中的许多&lt;td&gt; 生成一个表格。

我需要一个示例,说明如何将 &lt;ItemText&gt;&lt;ItemTextsType&gt; 分别为每个 &lt;Item&gt; 分组。

我会将结果安排在如下表格中:

 <table>
    <%--tr for each Item in Items--%>
    <tr>
        <td>
            some value from xml element according <b> Item1 </b>
        </td>
        <td>
            some more value from xml element according <b> Item1 </b>
        </td>
        <%--
        .
        .
        .
--%>
        <td>
            <table width="100%" dir="ltr">
                <tbody>
                    <tr style="background-color: #507CD1; text-align: center">
                        <td colspan="3" style="font: bold; color: white">
                            Item1
                        </td>
                    </tr>
                    <tr>
                        <td style="height: 35px; font: bold; color: #507CD1;">
                            type1
                        </td>
                    </tr>
                    <tr>
                        <td>
                            description11
                        </td>
                    </tr>
                    <tr>
                        <td>
                            description12
                        </td>
                    </tr>
                    <tr>
                        <td style="height: 35px; font: bold; color: #507CD1;">
                            type2
                        </td>
                    </tr>
                    <tr>
                        <td>
                            description21
                        </td>
                    </tr>
                    <tr>
                        <td>
                            description22
                        </td>
                    </tr>
                </tbody>
            </table>
        </td>
        <td>
            even more value from xml element according <b> Item1 </b>
        </td>
        <%--
        .
        .
        .
        --%>
    </tr>
   <tr>
        <td>
            some value from xml element according <b> Item2 </b>
        </td>
        <td>
            some more value from  xml element according <b> Item2 </b>
        </td>
        <%--
        .
        .
        .
--%>
        <td>
            <table width="100%" dir="ltr">
                <tbody>
                    <tr style="background-color: #507CD1; text-align: center">
                        <td colspan="3" style="font: bold; color: white">
                            Item2
                        </td>
                    </tr>
                    <tr>
                        <td style="height: 35px; font: bold; color: #507CD1;">
                            type1
                        </td>
                    </tr>
                    <tr>
                        <td>
                            description11
                        </td>
                    </tr>
                    <tr>
                        <td>
                            description12
                        </td>
                    </tr>
                    <tr>
                        <td style="height: 35px; font: bold; color: #507CD1;">
                            type2
                        </td>
                    </tr>
                    <tr>
                        <td>
                            description21
                        </td>
                    </tr>
                    <tr>
                        <td>
                            description22
                        </td>
                    </tr>
                </tbody>
            </table>
        </td>
        <td>
            even more value from according <b> Item2 </b>
        </td>
        <%--
        .
        .
        .
        --%>
    </tr>
   <%-- and so on--%>
</table>

关键是每个&lt;Item&gt; 都有单独的分组到它自己的&lt;tr&gt;&lt;td&gt;&lt;table&gt; 里面&lt;table&gt;&lt;ItemTextsType&gt; 分组

我尝试过类似的东西

<xsl:key name="item-texts-type" match="ItemText" use="ItemTextsType" />


<xsl:for-each select="ItemTexts/ItemText[count(. | key('item-texts-type', ItemTextsType)[1]) = 1]">

                        <xsl:sort select="ItemTextsType" />
                        <tr>
                          <td style ="height:35px;font: bold; color:#507CD1;">
                            <xsl:value-of select="ItemTextsType" />
                          </td>
                        </tr>

                        <xsl:for-each select="key('item-texts-type', ItemTextsType)">
                          <tr>
                            <td>
                               <xsl:value-of select="ItemTextsTypeDesc" />
                            </td>
                          </tr>
                        </xsl:for-each>


                      </xsl:for-each>

但它只适用于重复节点(如果只有一个&lt;Item&gt;它会正常工作)。 我无法更改 xml,因为它来自客户。

请帮帮我,我需要它尽快。

谢谢!!!!

【问题讨论】:

  • 这和你的previous seemingly identical question有什么区别?在我回答这个问题之前我没有注意到那个问题,而且我似乎独立地提出了与之前接受的答案相同的解决方案。
  • 最初我给出了所需输出的示例,ABach 很友好地回答了它。但后来我意识到我需要的输出与我之前写的不同,所以我改变了它。然后我决定打开另一个问题,所以也许上一个问题会对某人有所帮助。我希望在编辑之前将旧问题返回到状态,但到目前为止我做不到 - 我是 Stackoverflow 的新手......
  • 如果您转到原始问题的编辑历史记录,您应该会看到针对每个修订号的“回滚”选项,您可以使用该选项将问题回滚到该版本。
  • 谢谢,编辑了问题,使其与答案相匹配。

标签: xml xslt xml-parsing xslt-1.0 xsl-grouping


【解决方案1】:

如果您想为每个 Item 单独分组,那么实现此目的的一种方法是将包含 Item 的一些唯一标识符作为键值的一部分,例如

<xsl:key name="item-texts-type" match="ItemText"
         use="concat(generate-id(ancestor::Item), '|', ItemTextsType)" />

<!-- assuming the context node is the current Item -->
<xsl:variable name="currentItemId" select="generate-id()" />

<xsl:for-each select="ItemTexts/ItemText[count(. | key('item-texts-type',
        concat($currentItemId, '|', ItemTextsType))[1]) = 1]">
  <!-- .... -->
  <xsl:for-each select="key('item-texts-type',
        concat($currentItemId, '|', ItemTextsType))">

如果您当然可以使用 XSLT 2.0,这将更加简单......

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-08
    • 2012-10-11
    • 1970-01-01
    • 2015-08-21
    • 1970-01-01
    • 2010-11-05
    • 1970-01-01
    相关资源
    最近更新 更多