Question
· 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?

Discussion (6)0
Log in or sign up to continue

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. 

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>
}