Topic
  • 4 replies
  • Latest Post - ‏2013-05-17T17:05:27Z by alanholc
alanholc
alanholc
45 Posts

Pinned topic Converting string element to XML

‏2013-05-17T16:26:24Z |

Hi all,

I know this should be fairly simple, but I'm obviously not getting it...

I'm calling a service which responds with a soap message.

In the body there is a string element for the result data - like so:

<?xml version="1.0" encoding="UTF-8"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <soap:Body> <CodeResponse> <CodeResult> <?xml version="1.0" encoding="utf-8"?><Code><CodeElement1>Test A</CodeElement1><CodeElement2>Test B</CodeElement2></Code> </CodeResult> </CodeResponse> </soap:Body> </soap:Envelope>

What I need to do is retrieve the values of <CodeElement1> and <CodeElement2> so I can pass these back to the client as a CSV text value.

The problem I'm having is when I retrieve the <CodeResult> value and try to pass it as an XML message (right now I'm just attempting to transform it like so:

<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="no"/>
   <xsl:template match="/">
     <xsl:variable name="msg"
          select="//soap:Body"/>
     <xsl:message dp:priority="debug">
          Message Payload = <xsl:value-of select="$msg"/>
     </xsl:message>
     <xsl:value-of select="$msg"/>
  </xsl:template>

In the above, the $msg variable is getting the contents of <CodeResult> and I can see that in the debug log.

The problem now is how to run an xpath on the result... everything I've tried returns null values...

I know this must have been done before but I haven't really found any good examples...

Updated on 2013-05-17T16:34:24Z at 2013-05-17T16:34:24Z by alanholc
  • kenhygh
    kenhygh
    1606 Posts
    ACCEPTED ANSWER

    Re: Converting string element to XML

    ‏2013-05-17T16:59:33Z  
    • alanholc
    • ‏2013-05-17T16:54:20Z

    I can't 'fix' the result as I have no control over the service.

    The embedded xml declaration is actually part of the result string (<CodeResult> is a string value)

    I'm assigning that entire string value to $msg... If I try an xpath like your example it returns null.

    $msg is still a string and I need to convert it to an xml doc... what is the best way to do that? Is it as simple as a serialize function?

    ah. dp:parse()

  • kenhygh
    kenhygh
    1606 Posts

    Re: Converting string element to XML

    ‏2013-05-17T16:38:15Z  

    First comment: your response is not well-formed XML: there are two XML declarations.

    But, assuming you get that fixed :-), you should be able to do

    <xsl:variable name="CodeElement1" select="$msg/CodeElement1"/>

  • alanholc
    alanholc
    45 Posts

    Re: Converting string element to XML

    ‏2013-05-17T16:54:20Z  
    • kenhygh
    • ‏2013-05-17T16:38:15Z

    First comment: your response is not well-formed XML: there are two XML declarations.

    But, assuming you get that fixed :-), you should be able to do

    <xsl:variable name="CodeElement1" select="$msg/CodeElement1"/>

    I can't 'fix' the result as I have no control over the service.

    The embedded xml declaration is actually part of the result string (<CodeResult> is a string value)

    I'm assigning that entire string value to $msg... If I try an xpath like your example it returns null.

    $msg is still a string and I need to convert it to an xml doc... what is the best way to do that? Is it as simple as a serialize function?

  • kenhygh
    kenhygh
    1606 Posts

    Re: Converting string element to XML

    ‏2013-05-17T16:59:33Z  
    • alanholc
    • ‏2013-05-17T16:54:20Z

    I can't 'fix' the result as I have no control over the service.

    The embedded xml declaration is actually part of the result string (<CodeResult> is a string value)

    I'm assigning that entire string value to $msg... If I try an xpath like your example it returns null.

    $msg is still a string and I need to convert it to an xml doc... what is the best way to do that? Is it as simple as a serialize function?

    ah. dp:parse()

  • alanholc
    alanholc
    45 Posts

    Re: Converting string element to XML

    ‏2013-05-17T17:05:27Z  
    • kenhygh
    • ‏2013-05-17T16:59:33Z

    ah. dp:parse()

    Good night... Thanks. That was it and I have no idea what I was thinking...

    Ok - I probably wasn't thinking...