xsl由xslt1中的xml元素对重复节点进行分组

时间:2012-11-14 07:06:08

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

我有一个复杂的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>

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

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

我需要一个示例,说明如何针对每个<ItemText><ItemTextsType>分别<Item> 结果如下:

对于此示例中的第一个<Item>

  

type1

description11

description12
     

TYPE2

     

description21

     

description22

对于此示例中的第二个Item

  

type3

description31

description32
     

TYPE2

     

description21

     

description22

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

<table width="100%" border="1" style="display: block;">
        <tbody>
            <tr>
                <td id="SelectedRowLinkageContents">
                    <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>
                                    desription11
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    desription12
                                </td>
                            </tr>
                            <tr>
                                <td style="height: 35px; font: bold; color: #507CD1;">
                                    type2
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    desription21
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    desription22
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    desription23
                                </td>
                            </tr>
                        </tbody>
                    </table>

                    <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>
                                    desription11
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    desription12
                                </td>
                            </tr>
                            <tr>
                                <td style="height: 35px; font: bold; color: #507CD1;">
                                    type2
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    desription21
                                </td>
                            </tr>

                            <tr>
                                <td>
                                    desription23
                                </td>
                            </tr>
                        </tbody>
                    </table>

                    <table width="100%" dir="ltr">
                        <tbody>
                            <tr style="background-color: #507CD1; text-align: center">
                                <td colspan="3" style="font: bold; color: white">
                                    Item3
                                </td>
                            </tr>
                            <tr>
                                <td style="height: 35px; font: bold; color: #507CD1;">
                                    type1
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    desription11
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    desription12
                                </td>
                            </tr>
                            <tr>
                                <td style="height: 35px; font: bold; color: #507CD1;">
                                    type2
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    desription21
                                </td>
                            </tr>

                            <tr>
                                <td>
                                    desription23
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </td>
            </tr>
        </tbody>
    </table>

关键是每个<Item>都有单独的分组到自己的<table>内,<table>内部有<ItemTextsType>

我尝试了类似

的内容
<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>

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

请帮助我,我需要尽快。

谢谢!!!!

1 个答案:

答案 0 :(得分:3)

由于存在一些差异,很难说出所需的解决方案是什么 - 例如,您的输入XML有4个<Item>元素,但您想要的输出只考虑其中的3个元素。此外,所需输出中的多个描述与输入XML文档中的预期位置不匹配。

那就是说,如果我们专注于您所说的理想解决方案:

  

“重点是每个<Item>都有单独的分组   它是自己的<table>,而在<table>里面则是噼啪声   <ItemTextsType>

......那么这应该让你朝着正确的方向前进。

当这个XSLT:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
  <xsl:output omit-xml-declaration="yes" indent="yes" />
  <xsl:strip-space elements="*" />

  <xsl:key
    name="kItemTextByType"
    match="ItemText"
    use="concat(generate-id(ancestor::Item[1]), '+', ItemTextsType)" />

  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*" />
    </xsl:copy>
  </xsl:template>

  <xsl:template match="/*">
    <table width="100%" border="1" style="display: block;">
      <tbody>
        <tr>
          <td id="SelectedRowLinkageContents">
            <xsl:apply-templates />
          </td>
        </tr>
      </tbody>
    </table>
  </xsl:template>

  <xsl:template match="Item">
    <table width="100%" dir="ltr">
      <tbody>
        <tr style="background-color: #507CD1; text-align: center">
          <td colspan="3" style="font: bold; color: white">
            <xsl:value-of select="concat('Item', position())" />
          </td>
        </tr>
        <xsl:apply-templates 
          select="ItemTexts/ItemText[
            generate-id() = 
            generate-id(
              key(
                'kItemTextByType', 
                 concat(generate-id(current()), '+', ItemTextsType)
              )[1]
            )
          ]">
          <xsl:sort select="ItemTextsType" />
        </xsl:apply-templates>
      </tbody>
    </table>
  </xsl:template>

  <xsl:template match="ItemText">
    <tr>
      <td style="height: 35px; font: bold; color: #507CD1;">
        <xsl:value-of select="ItemTextsType" />
      </td>
    </tr>
    <xsl:for-each
      select="key(
        'kItemTextByType', 
        concat(
          generate-id(ancestor::Item[1]),
          '+',
          ItemTextsType
        )
      )"
    >
      <xsl:sort select="ItemTextsTypeDesc" />
      <tr>
        <td>
          <xsl:value-of select="ItemTextsTypeDesc" />
        </td>
      </tr>
    </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>

...适用于提供的输入XML:

<?xml version="1.0" encoding="UTF-8"?>
<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>

...想要的结果(我认为)产生了:

<table width="100%" border="1" style="display: block;">
  <tbody>
    <tr>
      <td id="SelectedRowLinkageContents">
        <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>
        <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>
        <table width="100%" dir="ltr">
          <tbody>
            <tr style="background-color: #507CD1; text-align: center">
              <td colspan="3" style="font: bold; color: white">Item3</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>
        <table width="100%" dir="ltr">
          <tbody>
            <tr style="background-color: #507CD1; text-align: center">
              <td colspan="3" style="font: bold; color: white">Item4</td>
            </tr>
            <tr>
              <td style="height: 35px; font: bold; color: #507CD1;">type2</td>
            </tr>
            <tr>
              <td>description21</td>
            </tr>
            <tr>
              <td>description22</td>
            </tr>
            <tr>
              <td style="height: 35px; font: bold; color: #507CD1;">
              type3</td>
            </tr>
            <tr>
              <td>description31</td>
            </tr>
            <tr>
              <td>description32</td>
            </tr>
          </tbody>
        </table>
      </td>
    </tr>
  </tbody>
</table>

...在HTML网页中呈现时,如下所示:

HTML table