DataPower

 View Only
  • 1.  Parsing xml declaration failed

    Posted Sat May 28, 2022 01:43 AM

    Dear Team,

    Can you suggest how to parse below xml declaration, there are no spaces between xml and version but it is coming from source, can you suggest how to parse using xslt and output xml should be without <?xmlversion="1.0"encoding="utf-8"?> :

    <?xmlversion="1.0"encoding="utf-8"?>

    Kindly suggest.



    #DataPower
    #Support
    #SupportMigration


  • 2.  RE: Parsing xml declaration failed

    Posted Thu June 02, 2022 09:24 AM

    Hi,

    first, while what you receive looks like XML, it is Non-XML:

    https://www.w3.org/TR/xml/#NT-XMLDecl

    After "<?xml" comes VersionInfo [24], and that requires a leading space ("S").

    So easiest is let each and every false request just fail and force sender to fix his system!

    Only in case you cannot force sender to send non-junk input, as said the input is Non-XML. So you can fix with a GatewayScript action reading input with "session.input.readAsBuffer()" and then create new Buffer from that with space inserted between "<?xml" and "version...". Then just do "session.output.write(newBuf)" and the following rule action will get valid XML as input.



    #DataPower
    #Support
    #SupportMigration


  • 3.  RE: Parsing xml declaration failed

    Posted Thu June 02, 2022 09:56 AM

    Just realized that a space is missing before "encoding=..." as well.

    This GatewayScript does add the missing two spaces:

    $ cat fix_junk.js session.input.readAsBuffer(function (err, buf) { throwIf(err); var newBuf = new Buffer(2 + buf.length); buf.copy(newBuf, 0, 0, 5); newBuf[5] = 0x20; buf.copy(newBuf, 6, 5, 19); newBuf[19] = 0x20; buf.copy(newBuf, 20, 18); session.output.write (newBuf); }); function throwIf(error) { if (error) throw error; } $

    Sample processing:

    $ coproc2 fix_junk.js <(echo -n '<?xmlversion="1.0"encoding="utf-8"?><x/>') dp3-l3.boeblingen.de.ibm.com:2227; echo <?xml version="1.0" encoding="utf-8"?><x/> $

    #DataPower
    #Support
    #SupportMigration


  • 4.  RE: Parsing xml declaration failed

    Posted Thu June 02, 2022 12:15 PM

    Just adding XSLT - based option, modified from Hermann's old repair.xsl:

    Fix declaration:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:dp="http://www.datapower.com/extensions" xmlns:regexp="http://exslt.org/regular-expressions" extension-element-prefixes="dp" > <xsl:output method="xml" /> <dp:input-mapping href="hexBinary.ffd" type="ffd"/> <dp:output-mapping href="hexBinary.ffd" type="ffd"/> <xsl:template match="Conversion/hexstr"> <Conversion> <hexstr> <xsl:value-of select="regexp:replace(string(.), '3c3f786d6c76657273696f6e3d22312e3022656e636f64696e673d227574662d38223f3e200a', 'g', '3c3f786d6c2076657273696f6e3d22312e302220656e636f64696e673d227574662d38223f3e200a')" /> </hexstr> </Conversion> </xsl:template> </xsl:stylesheet>

    Strip declaration:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:dp="http://www.datapower.com/extensions" xmlns:regexp="http://exslt.org/regular-expressions" extension-element-prefixes="dp" > <xsl:output method="xml" /> <dp:input-mapping href="hexBinary.ffd" type="ffd"/> <dp:output-mapping href="hexBinary.ffd" type="ffd"/> <xsl:template match="Conversion/hexstr"> <Conversion> <hexstr> <xsl:value-of select="substring-after(string(.), '3c3f786d6c76657273696f6e3d22312e3022656e636f64696e673d227574662d38223f3e200a')" /> </hexstr> </Conversion> </xsl:template> </xsl:stylesheet>

    --HP



    #DataPower
    #Support
    #SupportMigration


  • 5.  RE: Parsing xml declaration failed

    Posted Thu June 02, 2022 12:19 PM


  • 6.  RE: Parsing xml declaration failed

    Posted Thu June 02, 2022 02:56 PM

    That was code from before 2015, when GatewayScript became available for DataPower.

    My rule of thumb is, use XSLT for XML processing, and GatewayScript for JSON and binary data processing.



    #DataPower
    #Support
    #SupportMigration


  • 7.  RE: Parsing xml declaration failed

    Posted Fri June 03, 2022 05:20 AM

    Yes, I agree. That is a good practice and should be followed whenever possible.


    --HP



    #DataPower
    #Support
    #SupportMigration