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:
- oraext:create-nodeset-from-delimited-string()
- 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.
Nice post...!
ReplyDeleteThank you :)
Deleteplease sample code for XSLT
ReplyDeleteHi Abhishek, above code snippets are from XSLT. Can you please elaborate your problem statement?
Deletevery helpful.. article helped in solving a critical requirement
ReplyDeleteThanks to the blogger for sharing such explanatory codes.
ReplyDeleteTableau Soap Connection