Output news as XML

Topics: XSLT
May 9, 2011 at 11:24 AM

Hi,

I am using the Composite.News package to create news in Composite and display the news on a website. I use default XSLT functions (Composite.News.Latest, Composite.News.Details and so on) , that are created when installing the package, to display the news on my XHTML pages.

But now I wish to output the news as an XML feed, so that I can load the news in an app I will be building for iOS.

How is this done?

Do I create a new XSLT function that outputs the XML elements I need, and then insert this function in the editor on a page?
I have tried this, but the editor creates html, head and body elements that I don't want to be outputted in the XML feed.

I would like the news to be outputted in a document that looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <news>
        <item date="2011-05-09">
            <title>My News Title</title>
            <excerpt>This is a news story created in Composite C1.</excerpt>
        </item>
    </news>
</root> 

Bottom line is: I don't know how to make Composite C1 output pages that are not XHTML, but that are XML.

Can anyone help?

Coordinator
May 9, 2011 at 2:40 PM
Edited May 9, 2011 at 2:46 PM

Hi

With some code, you can map C1's functions to url-s, here're the steps:

1. In /App_Code folder, create a cs file with the following content

 

 

using System;
using System.Globalization;
using System.IO;
using System.Web;
using System.Xml.Linq;

using Composite.Data;
using Composite.Functions;

/// <summary>
/// Executes a C1 Function. The name of the function is copied from the name of this .ashx file,
/// i.e. if this file is named "Composite.Pages.QuickSitemap.ashx" the function "Composite.Pages.QuickSitemap" will be called
/// Parameters specified in the URL will be passed on to the function.
/// 
/// You control the data culture through the URL parameter "cultureScope", like "cultureScope=en-US".
/// 
/// To use this class - create a .ashx file which inherit from this class (ExecuteC1FunctionHttpHandler) and name it
/// (full function name).ashx
/// </summary>
public abstract class ExecuteC1FunctionHttpHandler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        // Get the name of the function to execute by copying the current file name (without the .ashx extension)
        string functionName = Path.GetFileNameWithoutExtension(context.Request.Path);
        // Locate the data culture to use - like en-US or nl-NL
        CultureInfo dataCulture = GetCurrentDataCulture(context);

        using (new DataScope(DataScopeIdentifier.Public, dataCulture))
        {
            // Grab a function object to execute
            IFunction function = FunctionFacade.GetFunction(functionName);

            // Execute the function, passing all query string parameters as input parameters
            object functionResult = FunctionFacade.Execute<object>(function, context.Request.QueryString);

            // output result
            if (functionResult != null)
            {
                context.Response.Write(functionResult.ToString());

                if (functionResult is XNode && function.ReturnType != typeof(Composite.Core.Xml.XhtmlDocument)) 
			context.Response.ContentType = "text/xml";
            }
        }
    }

    
    public bool IsReusable
    {
        get
        {
            return true;
        }
    }


    /// <summary>
    /// Locates the data scope culture that the function should be scoped to.
    /// I.e. the 'language' of the data to work on. 
    /// The Query String variable 'cultureScope' will be used, or the systems 
    /// default culure is this parameter is not specified.
    /// </summary>
    private CultureInfo GetCurrentDataCulture(HttpContext context)
    {
        string cultureScope = context.Request.QueryString["cultureScope"];
        if (string.IsNullOrEmpty(cultureScope)==false)
        {
            return new CultureInfo(cultureScope);
        }
        
        return DataLocalizationFacade.DefaultLocalizationCulture;
    }
}

 

2. Create a file with name "{Name of your xslt function}.ashx" with the following code 

 

<%@ WebHandler Language="C#" Class="ExecuteC1Function" %>


/// <summary>
/// Executes a C1 Function. The name of the function is copied from the name of this .ashx file,
/// i.e. if this file is named "Composite.Pages.QuickSitemap.ashx" the function "Composite.Pages.QuickSitemap" will be called
/// Parameters specified in the URL will be passed on to the function.
/// 
/// Implementation code is localed in ExecuteC1FunctionHttpHandler.cs in the /App_Code folder.
/// </summary>
public class ExecuteC1Function : ExecuteC1FunctionHttpHandler {
}

 

 

Now, when you access to this ashx file by http, it will execute your function. Function also may have some string parameters, you can specify their values in the query string, f.e. it could be a url like:

http://omnicorp.universe/News/Composite.News.Details.ashx?newsid=c7e68e6d-1790-413c-b1ce-e7fd8ad1b506