Formatting/Displaying a SOAP Reply Message (XML)

Posted on 2007-10-13
Last Modified: 2013-11-18
Hi Experts

I am writing a script (in PHP and Javascript) to communicate with a web service using SOAP requests. This works perfectly and has no problems.

What i was wondering is, when a message comes back from the soap request, how do I format it properly so that it can be displayed say for example in a table format or something alike? Applying some stylesheet to the soap message?

The SOAP message reply looks something like this:

<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="" xmlns:xsd="" xmlns:xsi="">
      <response from="gs2mgppdemo/TextQuery" type="process">
          <metadata name="numDocsMatched">45</metadata>
          <metadata name="numDocsReturned">10</metadata>
          <metadata name="query">apple</metadata>
          <documentNode docType="hierarchy" nodeID="HASH2e2c0786db13d242a0640e.5" nodeType="leaf" rank="61.874542" />
          <documentNode docType="hierarchy" nodeID="HASH9a32d65fe2d5e0559e9a88.6.3" nodeType="leaf" rank="30.937271" />
          <documentNode docType="hierarchy" nodeID="HASH01e114881a5545b979515a68.10.3" nodeType="leaf" rank="30.937271" />
          <documentNode docType="hierarchy" nodeID="HASH9a32d65fe2d5e0559e9a88.10.3" nodeType="leaf" rank="15.468636" />
          <documentNode docType="hierarchy" nodeID="HASH9a32d65fe2d5e0559e9a88.7.1" nodeType="leaf" rank="15.468636" />
          <documentNode docType="hierarchy" nodeID="HASH626d047760b154f868d2e5.7.1" nodeType="leaf" rank="15.468636" />
          <documentNode docType="hierarchy" nodeID="HASH01a99b6dc399591d12f46803.7.4" nodeType="leaf" rank="0.510514" />
          <documentNode docType="hierarchy" nodeID="HASH2e2c0786db13d242a0640e.6.5" nodeType="leaf" rank="0.20321295" />
          <documentNode docType="hierarchy" nodeID="HASH351102fcb484dbf644cd09.10.3" nodeType="leaf" rank="0.20321295" />
          <documentNode docType="hierarchy" nodeID="HASH626d047760b154f868d2e5.10.4" nodeType="leaf" rank="0.17838767" />
          <term field="" freq="54" name="apple" numDocsMatch="25" stem="3">
              <term freq="" name="apples" numDocsMatch="" />
              <term freq="" name="applicant" numDocsMatch="" />
              <term freq="" name="Applied" numDocsMatch="" />
              <term freq="" name="applied" numDocsMatch="" />
              <term freq="" name="applies" numDocsMatch="" />
              <term freq="" name="Applying" numDocsMatch="" />
              <term freq="" name="applying" numDocsMatch="" />

I simply want to format the message so that it is displayable (doesn't even have to be too pretty with colours and sophisticated tables!).

Many thanks for your help!
Question by:nizsmo
    LVL 60

    Accepted Solution

    You should use XSLT for that

    try this

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="" version="1.0">
        <xsl:template match="/">
                    <xsl:apply-templates select="//response"></xsl:apply-templates>
        <xsl:template match="response">
            <xsl:apply-templates select=".//termList"></xsl:apply-templates>
        <xsl:template match="termList">
            <table border="1">
                    <th>Matching Docs</th>
                    <th>Equivalent Terms</th>
                <xsl:apply-templates select=".//equivTermList/term"/>
        <xsl:template match="equivTermList/term">
            <xsl:param name="rspan" select="count(../term)" />
                    <xsl:when test="position() = 1">
                        <td rowspan="{$rspan}"><xsl:value-of select="ancestor::term/@name"/></td>
                        <td rowspan="{$rspan}"><xsl:value-of select="ancestor::term/@freq"/></td>
                        <td rowspan="{$rspan}"><xsl:value-of select="ancestor::term/@numDocsMatch"/></td>
                        <td rowspan="{$rspan}"><xsl:value-of select="ancestor::term/@stem"/></td>
                        <td><xsl:value-of select="@name"/></td>
                        <td><xsl:value-of select="@name"/></td>


    LVL 21

    Author Comment

    sorry it took so long, i was quite busy, but thank you for your solution, it did get me started and i did learn a few things with this, thanks again.
    LVL 60

    Expert Comment

    by:Geert Bormans

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
    JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to count occurrences of each item in an array.

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now