Monday, January 18, 2016

Generate unique id for multiple order lines


More than often we come across a requirement that a unique order line should only be processed.

That essentially mean you can have multiple parameters in your order line and you want to pick record with a unique combination of some attribute in your order line data. this will be more clear with an example and then subsequent solution for the issue.

This we will achieve using two XSLT function

XSLT key function which returns a node-set from the document, using the index specified by an element.

XSLT generate-id() Function which returns a string value that uniquely identifies a specified node.

If the node-set specified is empty, an empty string is returned. If you omit the node-set parameter, it defaults to the current node.

You can get more idea on the same from w3 schools

http://www.w3schools.com/xsl/func_generateid.asp
http://www.w3schools.com/xsl/func_key.asp

Now we will see in this example how we can use these two function to process unique order line.

First you should define a key, i.e. let suppose you want to process an order with a unique combination of account number and po number. In that case you will first define a key as below



Here xpath for parameter can be the path in your xml where account number and po number can be traversed

e.g if you have following xpath valid

/PO/ListofPO/AccontNumber
/PO/ListofPO/PoNumber

so your match parameter will be /PO/ListofPO

Obviously the namespace has to be there.

Now you got an idea on how to generate a key for a combination of parameter.

Lets generate a unique if for this key

/PO/ListofPO/PODetails[generate-id()=generate-id(key('Order',concat(ns0:AccountNumber,",",ns0:PoNumber))[1])]

Now what this mean is the processing will happend for the PODetails which has a unique combination of AccountNumber and PoNumber


No comments: