xslt 1.0 - 如何使用相同级别的元素进行嵌套分组

时间:2013-11-07 08:23:45

标签: xml xslt xslt-1.0 xslt-grouping

我有以下形式的xml

<operation>
    <update>
        <wogroup>id1</wogroup>
        <woid>SL0001</woid>
        <status>NEW</status>
    </update>
    <update>
        <wogroup>id1</wogroup>
        <woid>SL0001</woid>
        <status>OPEN</status>
    </update>
    <update>
        <wogroup>id1</wogroup>
        <woid>SL0001</woid>
        <status>CLOSED</status>
    </update>
    <update>
        <wogroup>id1</wogroup>
        <woid>SL0002</woid>
        <status>NEW</status>
    </update>
    <update>
        <wogroup>id2</wogroup>
        <woid>SL00011</woid>
        <status>OVERRIDE</status>
    </update>
    <update>
        <wogroup>id2</wogroup>
        <woid>SL00011</woid>
        <status>CLOSED</status>
    </update>
    <update>
        <wogroup>id2</wogroup>
        <woid>SL00021</woid>
        <status>NEW</status>
    </update>
</operation>

我对xml的外观没有任何说法,但我需要用html进行报告。我想看一个像

这样的HTML
Group : id1
    WO : SL001
        NEW
        OPEN
        CLOSED
    WO : SL002
        NEW
Group : id2
    WO : SL0011
       OVERRIDE
       CLOSED
    WO : SL0021
       NEW

我成功地分组了wogroup或woid,但不是以嵌套的方式......任何人都有建议吗?

1 个答案:

答案 0 :(得分:2)

我不知道这是否是最好的方法,但它可以根据需要对元素进行分组:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:output method="xml" encoding="UTF-8" />

   <xsl:key name="updates-by-wogroup" match="/operation/update" use="wogroup" />
   <xsl:key name="updates-by-wogroup-and-woid" match="/operation/update" use="concat(wogroup,woid)" />

   <xsl:template match="/">
      <xsl:for-each select="operation/update[not(wogroup = preceding-sibling::update/wogroup)]">
        Group: <xsl:value-of select="wogroup" />
        <xsl:for-each select="key('updates-by-wogroup',current()/wogroup)[not(woid = preceding-sibling::update/woid)]">
          WO: <xsl:value-of select="woid" />
          <xsl:for-each select="key('updates-by-wogroup-and-woid',concat(current()/wogroup,current()/woid))">
            <xsl:text>
              <xsl:value-of select="status" /></xsl:text>
            </xsl:for-each>
          </xsl:for-each>
      </xsl:for-each>
   </xsl:template>
</xsl:stylesheet>

如果您成功通过wogroup进行分组或无效,您将理解代码,无论如何,如果您需要,请提出任何问题。