Oliver Friedrich
Oliver Friedrich

Reputation: 9240

XSLT Select with grouping data over two differend lists

Here's another mindblower, maybe just easy for you.

I have two lists, one is a map that connects item-ids to group-ids, the second one is the item list with simple values. I need to accumulate the number of item-values to group-totals. Originally the two lists are based in different XML-Files.

<!-- List 1 mapping-->
<mapping>
    <sub id="1" item="a" group="a">
    <sub id="2" item="b" group="a">
    <sub id="3" item="d" group="b">
    <sub id="4" item="e" group="b">
    <sub id="5" item="f" group="c">
</mapping>

<!-- List 2 items -->
<items>
    <item id="a" val="OK"/> 
    <item id="b" val="ERROR"/>
    <item id="c" val="OK"/>
    <item id="d" val="OK"/>
    <item id="e" val="OK"/>
    <item id="f" val="OK"/>
</items>

My current approach:

<xsl:variable name="failed-total">

<xsl:variable name="groups-total">
    <xsl:value-of select="count(mapping//sub[not(@group=preceding-sibling::sub/@group)])"/>
</xsl:variable>

<!--Selecting the unique groups of the first list:-->
<xsl:for-each select="mapping//sub[not(@group=preceding-sibling::sub/@group)]">
    <xsl:variable name="failed">
        <xsl:value-of select="items/item[@id=current()/@item and val='ERROR']"/>
    </xsl:variable>

<!-- TODO: add value of failed to failed-total value -->
</xsl:for-each>

Needed Output:

Number of Groups: 3
Groups failed: 1

Changing list 2 to the following:

<!-- List 2 items -->
<items>
    <item id="a" val="ERROR"/> 
    <item id="b" val="ERROR"/>
    <item id="c" val="OK"/>
    <item id="d" val="OK"/>
    <item id="e" val="OK"/>
    <item id="f" val="OK"/>
</items>

Should then output the same, cause item a and be are in the same group:

Number of Groups: 3
Groups failed: 1

Any hints welcome.

Upvotes: 2

Views: 469

Answers (2)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243469

Here is a short and simple solution (just one template), that reads the items.xml document from a local file:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>
 <xsl:key name="kGroupByVal" match="@group" use="."/>

 <xsl:variable name="vItems" select=
  "document('file:///c:/temp/delete/items.xml')"/>

 <xsl:template match="/">
  <xsl:variable name="vGroups" select=
  "/*/*/@group
          [generate-id()=generate-id(key('kGroupByVal',.)[1])]"/>

  <xsl:variable name="vErrorIds" select=
    "$vItems/*/*[@val='ERROR']/@id"/>

  Number of groups: <xsl:value-of select="count($vGroups)"/>
  Groups failed: <xsl:value-of select=
  "count($vGroups[key('kGroupByVal',.)/../@item = $vErrorIds])"/>
 </xsl:template>
</xsl:stylesheet>

When this transformation is applied on the provided XML document (mappings):

<mapping>
    <sub id="1" item="a" group="a"/>
    <sub id="2" item="b" group="a"/>
    <sub id="3" item="d" group="b"/>
    <sub id="4" item="e" group="b"/>
    <sub id="5" item="f" group="c"/>
</mapping>

and the second provided XML document (items) is in the file: C:\temp\delete\items.xml:

<items>
    <item id="a" val="OK"/>
    <item id="b" val="ERROR"/>
    <item id="c" val="OK"/>
    <item id="d" val="OK"/>
    <item id="e" val="OK"/>
    <item id="f" val="OK"/>
</items>

the wanted, correct result is produced:

  Number of groups: 3
  Groups failed: 1

If we modify the items.xml document to:

<items>
    <item id="a" val="ERROR"/>
    <item id="b" val="ERROR"/>
    <item id="c" val="OK"/>
    <item id="d" val="OK"/>
    <item id="e" val="OK"/>
    <item id="f" val="OK"/>
</items>

then again the correct result is produced:

  Number of groups: 3
  Groups failed: 1

If we modify items.xml to this:

<items>
    <item id="a" val="ERROR"/>
    <item id="b" val="OK"/>
    <item id="c" val="OK"/>
    <item id="d" val="OK"/>
    <item id="e" val="ERROR"/>
    <item id="f" val="OK"/>
</items>

again the correct result is produced:

  Number of groups: 3
  Groups failed: 2

Upvotes: 2

JLRishe
JLRishe

Reputation: 101662

Assuming all of the XML you provided is in a single input XML document (which isn't entirely clear from your question) this should work:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" indent="yes"/>
  <xsl:key name="kGroup" match="mapping/sub" use="@group" />
  <xsl:key name="kFailedItem" match="items/item[@val = 'ERROR']" use="@id" />

  <xsl:template match="/*">
    <xsl:variable name="distinctGroups"
                  select="mapping/sub[generate-id() = 
                                      generate-id(key('kGroup', @group)[1])]" />
    <xsl:variable name="failedGroups"
                  select="$distinctGroups[key('kGroup', @group)
                                            [key('kFailedItem', @item)]
                                         ]" />

    <xsl:value-of select="concat('Number of Groups: ', count($distinctGroups))"/>
    <xsl:text>&#xA;</xsl:text>
    <xsl:value-of select="concat('Groups Failed: ', count($failedGroups))"/>
  </xsl:template>

</xsl:stylesheet>

When run on this input:

<root>
  <!-- List 1 mapping-->
  <mapping>
    <sub id="1" item="a" group="a"/>
    <sub id="2" item="b" group="a"/>
    <sub id="3" item="d" group="b"/>
    <sub id="4" item="e" group="b"/>
    <sub id="5" item="f" group="c"/>
  </mapping>

  <!-- List 2 items -->
  <items>
    <item id="a" val="OK"/>
    <item id="b" val="ERROR"/>
    <item id="c" val="OK"/>
    <item id="d" val="OK"/>
    <item id="e" val="OK"/>
    <item id="f" val="OK"/>
  </items>
</root>

The result is:

Number of Groups: 3
Groups Failed: 1

Here is an approach you can use when the mapping and items elements are stored in separate variables from different XML documents:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:exslt="http://exslt.org/common">
  <xsl:output method="text" indent="yes"/>

  <xsl:key name="kGroup" match="mapping/sub" use="@group" />

  <xsl:variable name="failedItems" select="$items/items/item[@val = 'ERROR']" />

  <xsl:template match="/">
    <!-- Jump into the $mappings variable context -->
    <xsl:apply-templates select="$mappings/mapping" />
  </xsl:template>

  <xsl:template match="mapping">
    <xsl:variable name="distinctGroups"
                  select="sub[generate-id() = 
                              generate-id(key('kGroup', @group)[1])]" />
    <xsl:variable name="failedGroups"
                  select="$distinctGroups
                             [key('kGroup', @group)/@item = $failedItems/@id]" />

    <xsl:value-of select="concat('Number of Groups: ', count($distinctGroups))"/>
    <xsl:text>&#xA;</xsl:text>
    <xsl:value-of select="concat('Groups Failed: ', count($failedGroups))"/>
  </xsl:template>


  <!-- NOTE: The definitions for the four variables below are just for
             demonstration purposes. In reality, I believe that your 
             $mappings and $items variables would be populated some
             other way. -->
  <xsl:variable name="mappingsNF">
    <mapping>
      <sub id="1" item="a" group="a"/>
      <sub id="2" item="b" group="a"/>
      <sub id="3" item="d" group="b"/>
      <sub id="4" item="e" group="b"/>
      <sub id="5" item="f" group="c"/>
    </mapping>
  </xsl:variable>
  <xsl:variable name="mappings" select="exslt:node-set($mappingsNF)" />

  <xsl:variable name="itemsNF">
    <items>
      <item id="a" val="OK"/>
      <item id="b" val="ERROR"/>
      <item id="c" val="OK"/>
      <item id="d" val="OK"/>
      <item id="e" val="OK"/>
      <item id="f" val="OK"/>
    </items>
  </xsl:variable>
  <xsl:variable name="items" select="exslt:node-set($itemsNF)" />
</xsl:stylesheet>

Upvotes: 3

Related Questions