Xml/Xslt - To arrange elements using xslt - Asked By abinav shankar on 21-May-12 05:10 AM

hi

I want to display the items in an order such that the div which contains the maximum items should be displayed first and div which is empty should be placed in the end how to do it
Chintan Vaghela replied to abinav shankar on 21-May-12 05:15 AM
Give me your div structure?

Where to store this item?
Somesh Yadav replied to abinav shankar on 21-May-12 05:27 AM

I just built a site that transforms XML into HTML for display in MVC3. I used the second technique, where the controller determines the XML and XSLT files to use, and passes them in the model. An HTML helper in the view actually performs the transform.

In this case I'm rendering a conference program, so that's what Program refers to below. Parameters can be supplied to the stylesheet; below, I'm supplying a base URL as a parameter that will be turned into links in the generated HTML.

The model:

public class ProgramModel
{
    public string ProgramFilename { get; set; }
    public string StylesheetFilename { get; set; }

    public Dictionary
<string, string> Parameters { get; protected set; }

    public ProgramModel()
    {
        Parameters = new Dictionary
<string, string>();
    }
}

The controller:

    [OutputCache(Duration=1000)]
    public ActionResult Index()
    {
        string xmlFile = Server.MapPath("~/Program.xml");
        string xsltFile = Server.MapPath("~/Program-index.xslt");
        Response.AddCacheDependency(new CacheDependency(xmlFile), new CacheDependency(xsltFile));

        ProgramModel model = new ProgramModel();
        model.ProgramFilename = xmlFile;
        model.StylesheetFilename = xsltFile;
        model.Parameters["baseDayUrl"] = Url.Action("Day");

        return View(model);
    }

The helper:

public static class HtmlHelperXmlExtensions
{
    ///
<summary>
    /// Applies an XSL transformation to an XML document.
    ///
</summary>
    public static HtmlString RenderXml(this HtmlHelper helper, string xmlPath, string xsltPath, IDictionary
<string,string> parameters)
    {
        XsltArgumentList args = new XsltArgumentList();
        if (parameters != null)
            foreach (string key in parameters.Keys)
                args.AddParam(key, "", parameters[key]);

        XslCompiledTransform t = new XslCompiledTransform();
        t.Load(xsltPath);

        XmlReaderSettings settings = new XmlReaderSettings();
        settings.DtdProcessing = DtdProcessing.Parse;
        settings.ValidationType = ValidationType.DTD;

        using (XmlReader reader = XmlReader.Create(xmlPath, settings))
        {
            StringWriter writer = new StringWriter();
            t.Transform(reader, args, writer);
            return new HtmlString(writer.ToString());
        }

    }

}

The view:

<div data-role="content">
@Html.RenderXml(Model.ProgramFilename, Model.StylesheetFilename, Model.Parameters)
</div>
abinav shankar replied to Chintan Vaghela on 21-May-12 05:30 AM
hi just consider this div
<div>
<ul>
<li>item1</li>
<li>item2</li>
<li>item3</li>
</ul>
</div>
and this is another div which contains
<div>
<ul>
<li>item1</li>
</ul>
</div>

i want the div with three items to get displayed first and the div with only one element to get displayed in the end
Chintan Vaghela replied to abinav shankar on 21-May-12 05:46 AM
do you want XML?
abinav shankar replied to Chintan Vaghela on 21-May-12 05:54 AM
I am selecting some subcategory element if the subcategory count is 0 i want to display the category with no subcategory at last and the category with subcategoryitems in the begining
Chintan Vaghela replied to abinav shankar on 21-May-12 06:08 AM
My question is where is display this Category with subcategory ?

in asp.net page?