Wednesday, January 16, 2008

Removing a node from node-list

With the bpelx: extensions in BPEL you are able to insert and update nodes in a node list. A node-list is an XML message that contains a set of records. In this article I use the following example:
<BookList xmlns="http://message.vijfhuizen.com">
<Book>
<title>The Lord Of The Rings</title>
<author>J.R.R. Tolkien</author>
</Book>
<Book>
<title>Harry Potter</title>
<author>J.R.R. Tolkien</author>
</Book>
<Book>
<title>The Hobbit</title>
<author>J.R.R. Tolkien</author>
</Book>
<Book>
<title>Storm; Chronicals of Pandarve</title>
<author>Don Lawrence</author>
</Book>
</BookList>

For creating and updating node lists the bpelx: functions are enough to handle this. But when you want to remove a particular node, you can use the bpelx:remove function. But this function can only remove a node from a particular position. For example removing the second node you code:
<bpel:assign>
<bpelx:remove>
<bpelx:target variable="VarBookList" query="/Booklist/Book[2]" />
</bpelx:append>
</bpel:assign>

It is hard to code the bpelx:remove to create a xpath to dynamicly remove node. You would like to remove the second node based on the xpath:
/Booklist/Book[title="Harry Potter" and author="J.R.R. Tolkien"]
You can add the above xpath in the bpelx:remove, but you are not able to make this dynamically.

There is a solution. The trick is to create a stylesheet that copies the data into a new message, but removing that particular records. Create a stylesheet that does the normal copy of the XML message. Then add a <choose> element in the stylsheet to filter that particular record.
  <xsl:template match="/">
<BookList>
<xsl:for-each select="/BookList/Book">
<xsl:choose>
<xsl:when test="title='Harry Potter' and author='J.R.R. Tolkien'"/>
<xsl:otherwise>
<Book>
<title>
<xsl:value-of select="title"/>
</title>
<author>
<xsl:value-of select="author"/>
</author>
</Book>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</BookList>

Now we have a XSL stylesheet that removes a particular record, but this is not variable. This can be done via XSLT parameters.
  <xsl:param name="pTitle"/>
<xsl:param name="pAuthor"/>
<xsl:template match="/">
<BookList>
<xsl:for-each select="/BookList/Book">
<xsl:choose>
<xsl:when test="title=$pTitle and author=$pAuthor"/>
<xsl:otherwise>
<Book>
<title>
<xsl:value-of select="title"/>
</title>
<author>
<xsl:value-of select="author"/>
</author>
</Book>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</BookList>
</xsl:template>
Now we are able to use this stylesheet in BPEL. In general BPEL create the following code:
  <assign name="Transform">
<bpelx:annotation>
<bpelx:pattern>transformation</bpelx:pattern>
</bpelx:annotation>
<copy>
<from expression="ora:processXSLT('RemoveNode.xsl'
, bpws:getVariableData('Variable_BookList','payload')" />
<to variable="Variable_BookListTemp" part="payload"/>
</copy>
</assign>
But this code does not pass parameters to the stylesheet. The ora:processXSLT() can do this it has an additional parameter in this function:
  <assign name="Transform">
<bpelx:annotation>
<bpelx:pattern>transformation</bpelx:pattern>
</bpelx:annotation>
<copy>
<from expression="ora:processXSLT('RemoveNode.xsl'
, bpws:getVariableData('Variable_BookList','payload')" />
, bpws:getVariableData('BPELxslparameters'))"/>
<to variable="Variable_BookListTemp" part="payload"/>
</copy>
</assign>
Now only you have to create the BPELxslparameters variable and assign it with the correct name/value pairs. The structure of the this variable is as follows:
  <?xml version="1.0" encoding="windows-1252" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://schemas.oracle.com/service/bpel/common"
targetNamespace="http://schemas.oracle.com/service/bpel/common"
elementFormDefault="qualified">
<xsd:element name="parameters">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="item" minOccurs="1" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="name" type="xsd:string"/>
<xsd:element name="value" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Create in BPEL the variable BPELxslparameters and let it point to this strucure:
    <process ....
xmlns:common="http://schemas.oracle.com/service/bpel/common"
.../>
<variable name="BPELparameters" element="common:parameters"/>
Now we can in BPEL create an empty XML message, based on this strcuture and assign the values to these parameters and then call the processXSLT function.
  <bpelx:assign name="Assign_GenerateEmptyParameterSet">
<copy>
<from>
<parameters xmlns="http://schemas.oracle.com/service/bpel/common">
<item>
<name>pTitle</name>
<value/>
</item>
<item>
<name>pAutor</name>
<value/>
</item>
</parameters>
</from>
<to variable="BPELparameters" query="/common:parameters"/>
</copy>
</bpelx:assign>
<assign name="Assign_setXSLTParameters">
<copy>
<from expression="'Harry Potter'"/>
<to variable="BPELparameters" query="/common:parameters/common:item[1]/common:value"/>
</copy>
<copy>
<from expression="'J.R.R. Tolkien'"/>
<to variable="BPELparameters" query="/common:parameters/common:item[1]/common:value"/>
</copy>
</assign>

Post a Comment