Friday, January 1, 2010

BIP: Grouping and Parent Access

 

I found an interesting question on stackoverflow in the BI-Publisher tag. At first I thought BIP could not handle this requirement, but a little bit of trial and error taught me something. cleartext.blogspot.com

With this xml:

<root>
    <OrderEntry-Orders>
        <CwaChannel>Customer Portal</CwaChannel>
        <CwaOrderType>UT Sales</CwaOrderType>
        <ListOfOrderEntry-LineItems>
            <OrderEntry-LineItems>
                <CwaLineTotalAmount2>1000</CwaLineTotalAmount2>
                <CwaProductCode>001</CwaProductCode>
            </OrderEntry-LineItems>
            <OrderEntry-LineItems>
                <CwaLineTotalAmount2>1000</CwaLineTotalAmount2>
                <CwaProductCode>005</CwaProductCode>
            </OrderEntry-LineItems>
        </ListOfOrderEntry-LineItems>
    </OrderEntry-Orders>
    <OrderEntry-Orders>
        <CwaChannel>Customer Portal</CwaChannel>
        <CwaOrderType>UT Sales</CwaOrderType>
        <ListOfOrderEntry-LineItems>
            <OrderEntry-LineItems>
                <CwaLineTotalAmount2>1000</CwaLineTotalAmount2>
                <CwaProductCode>005</CwaProductCode>
            </OrderEntry-LineItems>
        </ListOfOrderEntry-LineItems>
    </OrderEntry-Orders>
    <OrderEntry-Orders>
        <CwaChannel>Customer Portal</CwaChannel>
        <CwaOrderType>UT Redemption</CwaOrderType>
        <ListOfOrderEntry-LineItems>
            <OrderEntry-LineItems>
                <CwaLineTotalAmount2>1000</CwaLineTotalAmount2>
                <CwaProductCode>005</CwaProductCode>
            </OrderEntry-LineItems>
        </ListOfOrderEntry-LineItems> cleartext.blogspot.com
    </OrderEntry-Orders>
</root>

 

This BIP code :

<?for-each-group: OrderEntry-LineItems; CwaProductCode?>

<?CwaProductCode?>

<?if:../../CwaOrderType='UT Sales' and ../../CwaChannel='Customer Portal'?>

<?'True'?>

<?end if?>

<?../../CwaOrderType?>

<?count(current-group()/.)?>

<?end for-each-group?>

Produces this output:

001

True

UT Sales

1

005

True

UT Sales

3

The count function returns a value of 3, but it prints only one value of OrderType. So it looks like the if condition is not filtering at all and always returns True.

The count returning 3 gives us the clue that there are two more elements there which need to be processed.

This code:

<?for-each-group: OrderEntry-LineItems; CwaProductCode?>

<?CwaProductCode?>

<?for-each:current-group()?>

<?if:../../CwaOrderType='UT Sales' and ../../CwaChannel='Customer Portal'?>

<?'True'?>

<?end if?>

<?../../CwaOrderType?>

<?end for-each?>

<?count(current-group()/.)?>

<?end for-each-group?>

Returns the answer: cleartext.blogspot.com

001

True

UT Sales

1

005

True

UT Sales

True

UT Sales

UT Redemption

3

Here the second True is print only once, as it iterates through the parents of the grouped node. cleartext.blogspot.com

So when there is a grouping done, and you need to check if its parent satisfies a condition, one single if statement will not suffce. Instead one more “ for-each:current-group() “ is required to iterate through all the grouped elements , and then the if statement should be added after that.

 

So the original BIP code in the question: cleartext.blogspot.com

 

<?for-each-group: OrderEntry-LineItems; CwaProductCode?>
<?if:../../CwaOrderType='UT Sales' and ../../CwaChannel='Customer Portal'?>
<?for-each: current-group()?>
<?CwaProductCode?>
<?xdoxslt:set_variable($_XDOCTX, 'countFund', xdoxslt:get_variable($_XDOCTX, 'countFund')+1)?>
<?xdoxslt:set_variable($_XDOCTX, 'TotalCount', xdoxslt:get_variable($_XDOCTX, 'TotalCount')+1)?>
<?xdoxslt:get_variable($_XDOCTX, 'countFund')?>
<?xdoxslt:set_variable($_XDOCTX, 'countFund', 0)?>
<?end if?>
<?end for-each-group?>
<?xdoxslt:get_variable($_XDOCTX, 'TotalCount')?>

Returns:

001

1

1

 

005

1

005

1

005

1

4

Can be re-written as

<?for-each-group: OrderEntry-LineItems; CwaProductCode?>

<?CwaProductCode?>

<?for-each: current-group()?>

<?if:../../CwaOrderType='UT Sales' and ../../CwaChannel='Customer Portal'?>

<?xdoxslt:set_variable($_XDOCTX, 'countFund', xdoxslt:get_variable($_XDOCTX, 'countFund')+1)?>

<?xdoxslt:set_variable($_XDOCTX, 'TotalCount', xdoxslt:get_variable($_XDOCTX, 'TotalCount')+1)?>

<?xdoxslt:get_variable($_XDOCTX, 'countFund')?>

<?xdoxslt:set_variable($_XDOCTX, 'countFund', 0)?>

<?end if?>

<?end for-each-group?>

<?end for-each-group?>

<?xdoxslt:get_variable($_XDOCTX, 'TotalCount')?>

 

Which returns: cleartext.blogspot.com

cleartext.blogspot.comcleartext.blogspot.com

001

1

005

1

1

3