Force %SOAP.WebRequest to output XML without new lines or whitespaces
I'm using %SOAP.WebRequest to send SOAP requests:
- Populate %SOAP.WebRequest object (by setting Request and HeadersOut properties)
- 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
Why do you need to change that? Semantically identical xml should not make a difference anywhere?
Target system is very fastidious about XML. I aim to eliminate all possible variables that may affect XML processing.
Traced stuff to %SOAP.WebBase and for example in WriteSOAPHeaders there are these lines:
$$$XMLSetBuffer(" </"_..#SOAPPREFIX_":Header>")
$$$XMLWriteLineI guess there's really no way to remove new lines without a very heavy customization.
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.
Yeah, that's what I was referring to :-(
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>
}