Written by

Senior Cloud Architect at InterSystems
Question Eduard Lebedyuk · Jun 13, 2017

Force %SOAP.WebRequest to output XML without new lines or whitespaces

I'm using %SOAP.WebRequest to send SOAP requests:

  1. Populate %SOAP.WebRequest object (by setting Request and HeadersOut properties)
  2. Call SendSOAPBody method to send request

Currently the XML I send looks like this:

<?xml version="1.0" encoding="UTF-8" ?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/'>
  <SOAP-ENV:Header>
<SomeCustomHeader/>  </SOAP-ENV:Header>
  <SOAP-ENV:Body><SomeCustomBody/></SOAP-ENV:Body>
</SOAP-ENV:Envelope>

However, I want XML to be generated differently:

  • No new lines
  • No whitespaces between <SomeCustomHeader/> and </SOAP-ENV:Header>

How can I tweak XML generation to achieve that?

Comments

Fabian Haupt · Jun 13, 2017

Why do you need to change that? Semantically identical xml should not make a difference anywhere?

0
Eduard Lebedyuk  Jun 13, 2017 to Fabian Haupt

Target system is very fastidious about XML. I aim to eliminate all possible variables that may affect XML processing.

0
Eduard Lebedyuk  Jun 13, 2017 to Warlin Garcia

Traced stuff to %SOAP.WebBase and for example in WriteSOAPHeaders there are these lines:

$$$XMLSetBuffer("  </"_..#SOAPPREFIX_":Header>")
$$$XMLWriteLine

I guess there's really no way to remove new lines without a very heavy customization.

0
Warlin Garcia · Jun 13, 2017

There's no easy way for this (as far as I know) as most of this information is "hardcoded" to be written that way in the methods. 

You can always extend the WebRequest class (and its parents) and modify the methods to write the XML in a single line or however you want. However, this is very tedious and carries a lot of problems with each upgrade. 

0
Warlin Garcia  Jun 14, 2017 to Eduard Lebedyuk

Yeah, that's what I was referring to :-(

0
Herman Slagman · Jun 14, 2017

You can use XSLT to do this:

ClassMethod Run()
{
  Set XML=##class(%Dictionary.CompiledXData).%OpenId(..%ClassName(1)_"||XML").Data
  Set XSLT=##class(%Dictionary.CompiledXData).%OpenId(..%ClassName(1)_"||XSLT").Data
  Set sc = ##class(%XML.XSLT.Transformer).TransformStream(XML, XSLT, .Result,, .Params,)
  Do Result.OutputToDevice()
}
XData XML{
<?xml version="1.0" encoding="UTF-8" ?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/'>
  <SOAP-ENV:Header>
<SomeCustomHeader/> </SOAP-ENV:Header>
  <SOAP-ENV:Body><SomeCustomBody/></SOAP-ENV:Body>
</SOAP-ENV:Envelope>
}
XData XSLT{
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes"/>
  <xsl:strip-space elements="*"/>
  <xsl:template match="@*|node()">
   <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
   </xsl:copy>
  </xsl:template>
</xsl:stylesheet>
}

 

0