Create Delimited String from XML Nodes and Vice Versa in SOA 12c

A delimited string is a string representation of data separated by a delimiter(e.g. ","). A simple representation of the delimited string will look like.

Delimited String Examples
Sample-1-- Delimiter as "," 
    Value1,Value2,Value3 
Sample-2-- Delimiter as "|" 
    Value1|Value2|Value3

If we want to represent the above set of delimited values in the form of an XML document under some root and parent tag. The representation will look like this.

XML Example
<root xmlns="http://test.com/sample/xml">
  <Values>
    <value>Value1</value>
    <value>Value2</value>
    <value>Value3</value>
  </Values>
</root>

Sometimes we may need to convert values coming in delimited string to an xml document or vice versa. Since delimited string is similar to csv format. We can create a nxsd schema and then perform translate i.e. native to xml or xml to native within our SOA application. But in the scenarios wherein we just want to convert one delimited string to xml or recurring xml nodes to one delimited string, there we can make use of oracle xpath extension functions:

  1. oraext:create-nodeset-from-delimited-string()
  2. oraext:create-delimited-string()

xmlns:oraext="http://www.oracle.com/XSL/Transform/java/oracle.tip.pc.services.functions.ExtFunc"

Note: Both functions can be used in XSLT or BPEL assign activity. The above two mentioned examples will be used for explaining these 2 functions.

oraext:create-nodeset-from-delimited-string() 

This function is used to create XML node set from delimited string. Basic syntax of the function is as below.

oraext:create-nodeset-from-delimited-string ($qName, $delimitedString, $delimiter)

$qName: Qualified name of the node for each value to be created along with namespace. For e.g. '{http://test.com/sample/xml}value'
$delimitedString: String containing values delimited by a delimiter. Examples as above.
$delimiter: String representation of delimiter. For e.g. ','
Sample XSLT code:
<xsl:template match="/">
    <ns1:root>
      <ns1:Values>
        <xsl:copy-of select="oraext:create-nodeset-from-delimited-string('{http://test.com/sample/xml}value','Value1,Value2,Value3' , ',' )"/>
      </ns1:Values>
    </ns1:root>
</xsl:template>

Note: Excluding, the XSL header for a better view. Please make sure all namespaces are properly imported in the XSL header.
The above code snippet will give output as above example.

oraext:create-delimited-string()

This function is used to convert nodes to delimited string. Below is the syntax of the function.
oraext:create-delimited-string ($nodeSetValue, $delimiter)
$nodeSetValue: repetitive nodes or combination of nodes for which delimited string needs to be created. For e.g.
<value>Value1</value>
<value>Value2</value>
<value>Value3</value>
$delimiter: String representation of delimiter. For e.g. ','
Sample code using the above sample
<!--',' as delimiter-->
<xsl:value-of select="oraext:create-delimited-string ($nodeSet/ns1:Values/ns1:value, ',')"/>
<!--'|' as delimiter-->
<xsl:value-of select="oraext:create-delimited-string ($nodeSet/ns1:Values/ns1:value, '|')"/>

xmlns:ns1="http://test.com/sample/xml"

Here $nodeSet contains the XML data as represented in above example. Above XSL snippets will yield results as shown in delimited string samples



Comments

  1. please sample code for XSLT

    ReplyDelete
    Replies
    1. Hi Abhishek, above code snippets are from XSLT. Can you please elaborate your problem statement?

      Delete
  2. very helpful.. article helped in solving a critical requirement

    ReplyDelete
  3. Thanks to the blogger for sharing such explanatory codes.
    Tableau Soap Connection

    ReplyDelete

Post a Comment

Popular posts from this blog

DateTime formatting using xp20:format-dateTime ()

Import and Export MDS artifacts in SOA 12c