Forum Discussion

duannguyen's avatar
13 years ago

How to consume multiple XML from multiple Rest API calls and transform data with XSLT

Hi, I need to pull in data from the Lithium blog to display on a webpage the following info:

1 - Blog posts (teaser headline, date, user, etc)

2 - PermaLink

3 - User's Avatar

 

In order to get all 3 above, I have to make multiple REst API calls.  This yields 3 or more XML results. What is the best way to display all 3 of the above in a single XSLT sheet with 3 different XML?  Should I merge into 1 XML file and parse it in the XSLT file or is there a better way of doing this?  I'm currently using C# as the language for the project.

 

Thank you

  • Not sure if you have the feature enabled in your community, but we use endpoints for this.  Within the endpoint, we make all of the REST calls then produce a consolidated XML output with only the data we care about.  For example, here is the code we use in a search-before-post endpoint to give us a consolidated list of matches along with their kudos and a custom metadata count of cases linked.

     

    <#assign subject = http.request.parameters.name.get("subject", "") />
    <#assign size = http.request.parameters.name.get("size", "") />
    <#assign messages = restadmin("/search/messages?one_or_more=" + subject?replace(" ", "+") + "&page_size=" + size + "&restapi.response_style=view").messages />
    <response status="success">
    <messages>
    <#list messages.message as message>
      <message type="message" href="${message.@href}" view_href="${message.@view_href}">
      <subject type="string">${message.subject}</subject>
      <kudos>
      <count type="int">${message.kudos.count}</count>
      </kudos>
      <#assign cases_linked = 0 />
      <#assign tags = restadmin("/messages/id/" + message.id + "/tagging/tags/all").tags />
      <#list tags.tag as tag>
        <#if tag.text?starts_with("tz:km:")>
          <#assign cases_linked = cases_linked + 1 />
        </#if>
      </#list>
      <cases_linked>
      <count type="int">${cases_linked}</count>
      </cases_linked>
      </message>
    </#list>
    </messages>
    </response>

     

    The result looks like:

     

    <response status="success">
      <messages>
        <message type="message" href="/messages/id/59" view_href="https://foo.example.com/t5/boards/id/bar/This-is-a-test">   
          <subject type="string">CASE: 12345: This is a test</subject>    
          <kudos>
            <count type="int">10</count>
          </kudos>
          <cases_linked>
            <count type="int">3</count>
          </cases_linked>
        </message>
      </messages>
    </response>

     

     

  • Not sure if you have the feature enabled in your community, but we use endpoints for this.  Within the endpoint, we make all of the REST calls then produce a consolidated XML output with only the data we care about.  For example, here is the code we use in a search-before-post endpoint to give us a consolidated list of matches along with their kudos and a custom metadata count of cases linked.

     

    <#assign subject = http.request.parameters.name.get("subject", "") />
    <#assign size = http.request.parameters.name.get("size", "") />
    <#assign messages = restadmin("/search/messages?one_or_more=" + subject?replace(" ", "+") + "&page_size=" + size + "&restapi.response_style=view").messages />
    <response status="success">
    <messages>
    <#list messages.message as message>
      <message type="message" href="${message.@href}" view_href="${message.@view_href}">
      <subject type="string">${message.subject}</subject>
      <kudos>
      <count type="int">${message.kudos.count}</count>
      </kudos>
      <#assign cases_linked = 0 />
      <#assign tags = restadmin("/messages/id/" + message.id + "/tagging/tags/all").tags />
      <#list tags.tag as tag>
        <#if tag.text?starts_with("tz:km:")>
          <#assign cases_linked = cases_linked + 1 />
        </#if>
      </#list>
      <cases_linked>
      <count type="int">${cases_linked}</count>
      </cases_linked>
      </message>
    </#list>
    </messages>
    </response>

     

    The result looks like:

     

    <response status="success">
      <messages>
        <message type="message" href="/messages/id/59" view_href="https://foo.example.com/t5/boards/id/bar/This-is-a-test">   
          <subject type="string">CASE: 12345: This is a test</subject>    
          <kudos>
            <count type="int">10</count>
          </kudos>
          <cases_linked>
            <count type="int">3</count>
          </cases_linked>
        </message>
      </messages>
    </response>

     

     

    • duannguyen's avatar
      duannguyen
      Guide

      Thank you, that's pretty awesome.  I will try this out and check with my admin to see if we have such feature enabled.  In the meantime, I made 3 separate calls, got the resulting XML and did a merge on all 3.  I then send that combined XML to my XSLT to format the output.  Thank you for your time and sample codes.