XML to String and String to XML conversion
Use Cases
- Many a times, we may need to convert xml data and pass it as a string inside an XML tag.
- Sometimes from source, we get data which is in string format but actually is xml encoded in string format.
To do so in Oracle BPEL 12c or OSB 12c we can use built in functions.
XML data to String
Sample input
<inRoot>
<t1>Hello</t1>
<t2>World</t2>
<t3>One</t3>
</inRoot>
Sample output
<outRoot> <t1><inRoot><t1>Hello</t1><t2>World</t2><t3>One</t3></inRoot></t1>
</outRoot>
XSLT
This can be accomplished by using the function oraext:get-content-as-string(element as node-set). This function is really helpful when we are working on BPEL as we can use this function in ASSIGN activity or in TRANSFORM activity using XSLT.
xmlns:oraext="http://www.oracle.com/XSL/Transform/java/oracle.tip.pc.services.functions.ExtFunc"
element as node-set: xml data(can be variable, xpath, or a function that returns xml data)
Example:
<outRoot>
<t1><xsl:value-of select="oraext:get-content-as-string(/ns0:inRoot)"/></t1>
</outRoot>
XQuery
Xquery too have a function to convert xml to string i.e. fn-bea:serialize($arg as item()*) as xs:string. This function is really helpful when we are dealing with OSB.
xmlns:fn-bea="http://www.bea.com/xquery/xquery-functions"
$arg: Variable containing XML data.
Example:
$input contains xml data that needs to be converted and assigned to one tag in output payload.
<outRoot>
<t1>{fn-bea:serialize($input)}</t1>
</outRoot>
String to XML
Sample input
String Data:
'<inRoot><t1>Hello</t1><t2>World</t2><t3>One</t3></inRoot>'
Sample output
XML Data
<inRoot>
<t1>Hello</t1>
<t2>World</t2>
<t3>One</t3>
</inRoot>
As we can see in above example that < and > are replaced with < and >. In xml < and > are invalid characters and escaper characters are used in place of these.
XSLT
In xslt, we can use built in function oraext:parseXML(stringData).This function is really helpful when we are working on BPEL as we can use this function in ASSIGN activity or in TRANSFORM activity using XSLT.
xmlns:oraext="http://www.oracle.com/XSL/Transform/java/oracle.tip.pc.services.functions.ExtFunc"
stringData: string value of xml data.
Example:
<xsl:copy-of select="oraext:parseXML('<inRoot><t1>Hello</t1><t2>World</t2><t3>One</t3></inRoot>')"/>
Note: we have used copy-of instead of value-of as we need all the xml nodes to be copied to the target file. If we want to extract the data from xml without xml tags then we can use value-of.
XQuery
In xquery, we can use fn-bea:inlinedXML($arg as xs:string) as node()*. This function is really helpful when we are dealing with OSB.
xmlns:fn-bea="http://www.bea.com/xquery/xquery-functions"
$arg: variable containing xml data in string format.
Example:
fn-bea:inlinedXML('<inRoot><t1>Hello</t1><t2>World</t2><t3>One</t3></inRoot>')
Excellent article and this helps to enhance your knowledge regarding new things. Waiting for more updates.
ReplyDeleteAngular 12 New Features
Angular Latest Version Features