Introduction
In Oracle Integration Cloud (OIC), there are situations where an API returns a list of files along with their complete directory paths.
For example:
/archive/inbound/file1.csv,
/archive/inbound/file2.csv,
/archive/outbound/file3.csv
If we only need the file names and want to remove the directory path, we can use the OIC replace() function with a regular expression.
The expression used in this use case is:
replace( oraext:create-delimited-string($Var_FinalFileList/nsmpr2:executeResponse/ns29:response-wrapper/ns29:objects/ns29:name, "," ),"[^,]*/","")
Use Case
Suppose an OIC REST/API response returns multiple file names with their complete paths.
Input
/archive/inbound/File_001.csv,
/archive/inbound/File_002.csv,
/archive/outbound/File_003.csv
We want to remove the directory path and get:
File_001.csv,File_002.csv,File_003.csv
This can be achieved using replace().
Solution
The main expression is:
replace(
oraext:create-delimited-string(
$Var_FinalFileList/nsmpr2:executeResponse/ns29:response-wrapper/ns29:objects/ns29:name,
","
),
"[^,]*/",
""
)
Let's break it into two parts.
Step 1: Create a Comma-Separated String
First, we use:
oraext:create-delimited-string($Var_FinalFileList/nsmpr2:executeResponse/ns29:response-wrapper/ns29:objects/ns29:name, ",")
The XPath:
$Var_FinalFileList/nsmpr2:executeResponse/ns29:response-wrapper/ns29:objects/ns29:name
retrieves the file names/paths from the response.
oraext:create-delimited-string() combines multiple values into a single string using the specified delimiter.
Here the delimiter is: ,
Example
If the response contains:
/archive/inbound/File_001.csv
/archive/inbound/File_002.csv
/archive/outbound/File_003.csv
the function creates:
/archive/inbound/File_001.csv,/archive/inbound/File_002.csv,/archive/outbound/File_003.csv
Step 2: Use replace() to Remove the Path
Now we apply:
replace(
<comma-separated-string>,
"[^,]*/",
""
)
The important part is the regular expression:
[^,]*/
What does it mean?
[^,] :Any character except comma
*: Zero or more occurrences
/: Forward slash
Therefore:
[^,]*/
matches the directory/path portion before the file name, while stopping at the comma separating the next value.
Example
Input:
/archive/inbound/File_001.csv
The matched portion is:
/archive/inbound/
After replacing it with an empty string:
File_001.csv
Complete Example
Input
/archive/inbound/File_001.csv,
/archive/inbound/File_002.csv,
/archive/outbound/File_003.csv
Output
File_001.csv,File_002.csv,File_003.csv
In simple terms
First convert all file-path values into one comma-separated string, then use replace() with a regular expression to remove everything up to the last / for each comma-separated value, leaving only the file names.
This is a simple and useful OIC XPath/XSLT expression technique when working with API responses containing multiple file paths.

No comments:
Post a Comment