Kumar_y
Kumar_y

Reputation: 239

Converting non-xml to xml or SOAP using XSLT 1.0

I have to convert response like this to

Data received successfully to XML

to XML or SOAP

This XML output

<?xml version="1.0" encoding="UTF-8"?>
<response>Data received successfully</response>

Or SOAP

Both of them will work

<SOAP:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP:Header/>
    <SOAP:Body>
    <response>Data received successfully</response>
</SOAP:Envelope>

I am not sure whether this can be done with via XSLT?

Any suggestion would be helpful.

Upvotes: 1

Views: 533

Answers (1)

Mads Hansen
Mads Hansen

Reputation: 66723

You could send the text in as a parameter to an XSLT stylesheet and produce the desired XML output with the contents of the value specified in the parameter:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <xsl:param name="message" select="'Data received successfully to XML'"/>

  <xsl:template match="/">
    <response><xsl:value-of select="$message"/></response>
  </xsl:template>

</xsl:stylesheet>

SOAP output:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <xsl:param name="message"/>

  <xsl:template match="/">
    <SOAP:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/">
      <SOAP:Header/>
      <SOAP:Body>
        <response>
          <xsl:value-of select="$message"/>
        </response>
      </SOAP:Body>
    </SOAP:Envelope>
  </xsl:template>

</xsl:stylesheet>

If you need a well-formed XML document, use the XSLT itself as the input XML to invoke the transform.

Upvotes: 1

Related Questions