3

我有以下形式的 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 进行报告。我想看到一个像

Group : id1
    WO : SL001
        NEW
        OPEN
        CLOSED
    WO : SL002
        NEW
Group : id2
    WO : SL0011
       OVERRIDE
       CLOSED
    WO : SL0021
       NEW

我成功地在 wogroup 或 woid 上进行分组,但不是以嵌套方式......有人建议吗?

4

1 回答 1

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 或 woid 进行分组,您将理解代码,如果您需要,请提出任何问题。

于 2013-11-08T08:36:19.193 回答