Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Thomas Kahn 602 posts 506 karma points
    May 04, 2009 @ 16:56
    Thomas Kahn
    0

    XSLT extension (C#) returning an XmlDocument?

    Hi!

    I'm experimenting with an XSLT extension coded as a class library in C#. I have written a method that returns an XmlDocument:

    [code]
    public static XmlDocument GetTestXML()
    {
    XmlDocument xmlDoc = new XmlDocument();

    XmlElement mother = xmlDoc.CreateElement("questions");

    XmlElement question1 = xmlDoc.CreateElement("question");
    question1.InnerText = "How long is a string?";
    mother.AppendChild(question1);

    XmlElement question2 = xmlDoc.CreateElement("question");
    question2.InnerText = "Do you feel lucky?";
    mother.AppendChild(question2);

    return xmlDoc;
    }
    [/code]

    When access this method in my XSLT script I don't get any errors, but I don't get anything out either. What I'm trying to do is getting my XSLT extension to return a set of nodes that I can iterate.

    Does this work?
    Is there a different way to to it?

    Thanks in advance!

    /Thomas

  • Thomas Kahn 602 posts 506 karma points
    May 04, 2009 @ 17:00
    Thomas Kahn
    0

    Hmm - perhaps my method should return an XPathNodeIterator instead?
    Would that work better?

  • Casey Neehouse 1339 posts 483 karma points MVP 2x admin
    May 04, 2009 @ 20:26
    Casey Neehouse
    0

    .NET XSLT is expecting an XPathNodeIterator.

    Same if you are passing xml to an extension method.

  • Richard Soeteman 4047 posts 12900 karma points MVP 2x
    May 04, 2009 @ 20:26
    Richard Soeteman
    0

    Hi Thomas,

    You should indeed return a NodePathIterator. The first three minutes of this video are free and shows you how to do that.

    Regards,

    Richard

  • Thomas Kahn 602 posts 506 karma points
    May 04, 2009 @ 21:12
    Thomas Kahn
    0

    Thanks guys! Will do some more experimenting tomorrow!

    Since Umbraco is such a versatile tool, it's sometimes hard to know the quickest and easiest way to access content outside the ordinary Umbraco content. Previously I've coded custom user controls, but I love the formatting and iterating abilities of XSLT and XSLT extensions written in .NET seems to be the perfect tool.

    Are there more ways to work with external content in Umbraco?

  • Richard Soeteman 4047 posts 12900 karma points MVP 2x
    May 04, 2009 @ 21:29
    Richard Soeteman
    0

    Hi Thomas,

    For displaying external Data. I think XSLT Extensions are the best way. It gives you the power of .NET to retrieve data with the DAL you prefer. and markup the layout with the power of XSLT.

    Currently I'm building a package for a client that can also maintain external data in Umbraco using a custom datatype and Umbraco Controls. So that's another way of working with external data.

    And you can always import data offcourse ;-)

  • Thomas Kahn 602 posts 506 karma points
    May 04, 2009 @ 21:47
    Thomas Kahn
    0

    [quote=rsoeteman]
    Currently I'm building a package for a client that can also maintain external data in Umbraco using a custom datatype and Umbraco Controls. So that's another way of working with external data.

    And you can always import data offcourse ;-) [/quote]

    Wow, that sounds exciting!

    I sometimes find myself in situations where I want the best of two worlds: the fantastic website platform of Umbraco AND the full freedom that a blank C#/ASP.NET project gives me.

    For setting up complex database structures, Umbraco is not a good tool (relations, stored procedures etc). And still it would be great if I could set up such a structure that could live side by side with Umbraco and I could both display the content on the public pages and edit the content of this structure from inside Umbraco.

    I'd love it if you could document your work!

    /Thomas K

  • Daniel Lindstrom 454 posts 271 karma points
    May 05, 2009 @ 09:27
    Daniel Lindstrom
    0

    You probably have all the info you need already, but here is another thread that discusses returning XML data from an XSLT extension:

    http://forum.umbraco.org/yafpostst7905how-to-send-nodeset-to-xslt-extension-and-back.aspx

    In the first post there is also a link to a code example.

  • Thomas Kahn 602 posts 506 karma points
    May 06, 2009 @ 15:21
    Thomas Kahn
    0

    Thank's everyone!
    I now have a working demo of a class that returns custom data as an XPathNodeIterator and I can continue working with it inside my XSLT in Umbraco.

    The method looks like this:
    [code]
    public static XPathNodeIterator GetTestXML()
    {

    XmlDocument xmlDoc = new XmlDocument();

    XmlElement elemMother = xmlDoc.CreateElement("Mom");
    xmlDoc.AppendChild(elemMother);

    XmlElement kidOne = xmlDoc.CreateElement("Kid");
    kidOne.SetAttribute("gender", "male");
    kidOne.SetAttribute("name", "Brian");
    elemMother.AppendChild(kidOne);

    XmlElement kidTwo = xmlDoc.CreateElement("Kid");
    kidTwo.SetAttribute("gender", "female");
    kidTwo.SetAttribute("name", "Elena");
    elemMother.AppendChild(kidTwo);

    XmlElement kidThree = xmlDoc.CreateElement("Kid");
    kidThree.SetAttribute("gender", "male");
    kidThree.SetAttribute("name", "John");
    elemMother.AppendChild(kidThree);

    XPathNodeIterator xNodeIt = xmlDoc.CreateNavigator().Select(".");

    return xNodeIt;
    }
    [/code]

    Here's how I use it in my XSLT:
    [code]

    ]>

    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxml="urn:schemas-microsoft-com:xslt"
    xmlns:umbraco.library="urn:umbraco.library"
    xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon"
    xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes"
    xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath"
    xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions"
    xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings"
    xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets"
    xmlns:ThomasCustomMethods="urn:ThomasCustomMethods"
    exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets MusiktavlingMethods">









    [/code]

    And the result on the page is:
    [b]Brian - male
    Elena - female
    John - male[/b]

    Works great!
    Now I just have to figure out how to build an admin interface for a custom database that I want to place inside Umbraco. I'm thinking about using custom datatypes, but I'm not sure if it will work. For example I will be needing a complete form with drop-down menues that fetch their values from other tables and I will need to store data directly to an external database table. I will only use Umbraco for presentation of data - no storage in Umbraco.

    /Thomas K

  • Richard Soeteman 4047 posts 12900 karma points MVP 2x
    May 06, 2009 @ 15:46
    Richard Soeteman
    0

    Hi Thomas,

    I'm currently building a solution for a client that can do this Here is a picture from a few weeks back. I cannot go into to much details. But what I did was the following:

    - Create a new data type
    - Create a few extra controls for 1/n n/n data relations
    - Create my own implementation of Idata and call the Webcontrol (Class that uses IDataEditor) directly. I did that because now i can get and set the Idata.Value propertyu from my own datasource in my case an sql database.

    This works perfectly except for the TinyMCE editor control (still don't know why).

    Again I cannot go into to much detail but hope this post gives you enough insight.

    Richard

  • Thomas Kahn 602 posts 506 karma points
    May 06, 2009 @ 21:31
    Thomas Kahn
    0

    Thanks Richard! It's very helpful to know that it is doable!

Please Sign in or register to post replies

Write your reply to:

Draft