Skip to main content
Solved

ArcScript to determine file type

  • September 24, 2025
  • 7 replies
  • 64 views

ericbux
Forum|alt.badge.img

I have a trading partner who’s sending EDI with a file extension (.xml) and a Content-Type: text/plain and they’re also sending EPCIS xml files which are also of Content-Type: text/plain.  I need to determine which files are legit xml/edi and route them accordingly.

I use a script on my EDI files to add additional headers, but I don’t think this will work on the xml EPCIS files because they are not EDI.

<!-- code I use to add additional headers to edi files -->

<arc:set attr="edi.File" value="[FilePath]" />
<arc:call op="x12Scan" in="edi" out="edi">
<arc:enum item="edi">
<arc:set attr="output.header:[_attr]" value="[_value]" />
</arc:enum>
</arc:call>

<arc:set attr="output.filepath" value="[filepath]" />
<arc:push item="output" />

Since the EPCIS files are true xml, I feel like this will bomb on the op=”x12Scan”.  Anyone have a suggestion for adding a script to check the actual content-type of inbound files?

Thanks

Best answer by ericbux

James, this ended up working for me:

<arc:try>
<arc:set attr="xml.uri" value="[filepath]" />
<arc:set attr="xml.xpath" value="/" />
<arc:call op="xmlDOMSearch" item="xml" />
<arc:set attr="output.header:content-type" value="application/xml" />
<arc:catch>
<arc:set attr="output.header:content-type" value="application/edi-x12" />
</arc:catch>
</arc:try>
<arc:set attr="output.filepath" value="[filepath]" />
<arc:push item="output" />

Since I know that only EDI files are coming over along with XML, I “hacked” it and put the correct content-type header which I can then use on a branch to route accordingly.

Thanks!

This topic has been closed for replies.

7 replies

James B
Forum|alt.badge.img
  • Employee
  • September 24, 2025

If the file is sent to you over an MFT channel where the content-type can be specified via the transport, that will be the content-type of the message that is carried forward in the application regardless of whether the application could scan it as a different type, so ideally, you’d want to get this partner to declare the right content-type when they send you files. 

 

With that said, I was able to create a script like the following to attempt to parse the document as XML and if so, override the content-type header:

 

<arc:catch>
<arc:set attr="xml.uri" value="[filepath]" />
<arc:set attr="xml.xpath" value="/" />
<arc:call op="xmlDOMSearch" item="xml" />
<arc:set attr="output.header:content-type" value="application/xml" />
</arc:catch>
<arc:set attr="output.filepath" value="[filepath]" />
<arc:push item="output" />

 

I’m not sure if this is the most efficient approach to the problem - this is modeled on the approach you were trying to take with x12Scan in a way (that is specific to application/edi-x12) by attempting to loop over the root of the document. The arc:catch block simply catches the XML parsing error you would see for a non-XML entity, and if the xmlDOMSearch was a success, a new content-type header is added to the message. 


ericbux
Forum|alt.badge.img
  • Author
  • September 24, 2025

Thanks James.  I tested your script and was unable to produce a result on either file (edi/xml). I’ll reach out to the TP and ask them to specify the correct content-type for the files they send.

Thanks!


James B
Forum|alt.badge.img
  • Employee
  • September 24, 2025

To clarify, the script that I shared can be placed in a standalone Script connector after you receive your data. Regardless of whatever type the file data is, it should pass through as-is, but if the xmlDOMSearch did not fail, a second Content-Type header will be added on the message, so you might see the headers like so:

Headers:
Other Headers
Content-Type: text/plain
Processed: by IsItXML:AS2Dummy; Success; 2025-09-24T15:38:37.018-04:00
content-type: application/xml
Processed: by IsItXML:RelabelXML; Success; 2025-09-24T15:38:37.722-04:00
Processed: by IsItXML:IsXML; Success; 2025-09-24T15:38:38.420-04:00
Processed: by IsItXML:ItIsXML; Success; 2025-09-24T15:38:38.803-04:00

The value of the second header overrides the first when the value of the Content-Type header is checked. 


ericbux
Forum|alt.badge.img
  • Author
  • September 24, 2025

I added the script to a stand-alone script connector and uploaded both edi and xml files and have no info when i expand the file in the output tab:

Other Headers:

Processed: by EPCIS:xz; Success; 2025-09-24T16:37:50.941-04:00
Processed: by EPCIS:Script1; Success; 2025-09-24T16:37:51.316-04:00

 


James B
Forum|alt.badge.img
  • Employee
  • September 24, 2025

That is what you would expect to see in the case where an exception is encountered in the xmlDOMSearch, so there could be some other element in the script that is failing.

 

If you remove the arc:catch elements:

 


<arc:set attr="xml.uri" value="[filepath]" />
<arc:set attr="xml.xpath" value="/" />
<arc:call op="xmlDOMSearch" item="xml" />
<arc:set attr="output.header:content-type" value="application/xml" />

<arc:set attr="output.filepath" value="[filepath]" />
<arc:push item="output" />

You may be able to see what was being caught before the new content-type header was added for this data.

The xmlDOMSearch is a heavy-handed test, because it is assuming that the XML parsing in the operation is able to loop over the root element without error, and instead of an active check to confirm the entity is XML (which is not a supported function), this code relies on the lack of any exception to assume the data is XML.

Perhaps there is a better check to place on the file (you could parse a few characters to see if the file opens with an XML element - or simply check the file extension if the filename is arriving as XML).

 


ericbux
Forum|alt.badge.img
  • Author
  • September 24, 2025

Perhaps there is a better check to place on the file (you could parse a few characters to see if the file opens with an XML element - or simply check the file extension if the filename is arriving as XML).

 

The problem is both files (edi/xml) are being sent with the .xml file extension.  Otherwise this would be easier.

I suppose I could use the fileReadLine operation and do something like startswith(‘ISA’) to check for EDI and set a custom header value using <arc:if>, perhaps..


ericbux
Forum|alt.badge.img
  • Author
  • Answer
  • September 24, 2025

James, this ended up working for me:

<arc:try>
<arc:set attr="xml.uri" value="[filepath]" />
<arc:set attr="xml.xpath" value="/" />
<arc:call op="xmlDOMSearch" item="xml" />
<arc:set attr="output.header:content-type" value="application/xml" />
<arc:catch>
<arc:set attr="output.header:content-type" value="application/edi-x12" />
</arc:catch>
</arc:try>
<arc:set attr="output.filepath" value="[filepath]" />
<arc:push item="output" />

Since I know that only EDI files are coming over along with XML, I “hacked” it and put the correct content-type header which I can then use on a branch to route accordingly.

Thanks!