How can I cascade XSLT within Umbraco? ( maybe RenderMacroContent ?! ) Options
readingdancer
Posted: Tuesday, December 18, 2007 10:47:24 AM

Rank: Enthusiast

Joined: 9/27/2007
Posts: 24
Location: Reading, UK
Hi All,

I would like to transform the result produced by another XSLT Macro, is this possible?

The XSLTSearch produces XML and I would like to take that result and transform the XML mark up

I thought this might be possible using RenderMacroContent, but this returns a text string not a node.

E.g.

Code:
<xsl:variable name="results"><xsl:value-of select="umbraco.library:RenderMacroContent('&lt;?UMBRACO_MACRO macroAlias=&quot;XSLTSearch&quot; source=&quot;-1&quot; searchFields=&quot;@nodeName,PageTitle,secondaryTitle,bodyText&quot; previewFields=&quot;bodyText,secondaryTitle,PageTitle&quot; searchBoxLocation=&quot;NONE&quot; previewType=&quot;CONTEXT&quot; resultsPerPage=&quot;5&quot; previewChars=&quot;100&quot; showPageRange=&quot;1&quot; showOrdinals=&quot;1&quot; showScores=&quot;0&quot; showStats=&quot;1&quot;&gt;&lt;/?UMBRACO_MACRO&gt;', $currentPage/@id)"/></xsl:variable>

<xsl:value-of select="$results" disable-output-escaping="yes"/>


So in the above code, ideally I'd like to use the $results as though it was a node..

Thanks in advance for your help!

Chris

Chris Houston - Business Dev Director for Ava / CTO for Clik Media Group / Umbraco Developer
mortenbock
Posted: Tuesday, December 18, 2007 12:29:00 PM

Rank: Addict

Joined: 7/19/2006
Posts: 790
Location: Århus, Denmark
You could probably do it by making an xslt extension that takes the string as a parameter.

I used this C# code to convert a string to an xpathnodeiterator:

Code:

XmlDocument cart = new XmlDocument();

cart.LoadXml("<empty>0</empty>");

return cart.CreateNavigator().Select("/");


Maybe you can even do it with inline scripting like the functions used in XSLT search.

Morten Bock - Level 2 certified - MVP 2008/2009 - My danish blog with a few english posts

readingdancer
Posted: Tuesday, December 18, 2007 4:40:17 PM

Rank: Enthusiast

Joined: 9/27/2007
Posts: 24
Location: Reading, UK
Hi Morten,

I've given your idea a go, I can get the C# working no problem, but when it tries to pass the XML back to the XSLT it seems to be seen by the XSLT processor as a text string.

Very frustrating!

I have tried both returning XPathNavigator or an XmlDocument but both seem to have the same result.

Code:

public XPathNavigator createnodes(string myxmlstring)
    {
    XmlDocument myXml = new XmlDocument();   
        myXml.LoadXml(myxmlstring);
    return myXml.CreateNavigator(); 
    }
    public XmlDocument createnodesDoc(string myxmlstring)
    {
    XmlDocument myXml = new XmlDocument();   
        myXml.LoadXml(myxmlstring);
    return myXml; 
    }


Chris

Chris Houston - Business Dev Director for Ava / CTO for Clik Media Group / Umbraco Developer
mortenbock
Posted: Tuesday, December 18, 2007 5:12:52 PM

Rank: Addict

Joined: 7/19/2006
Posts: 790
Location: Århus, Denmark
You need to return an XPathNodeIterator i think.

Morten Bock - Level 2 certified - MVP 2008/2009 - My danish blog with a few english posts

drobar
Posted: Tuesday, December 18, 2007 8:49:34 PM

Rank: Umbracoholic

Joined: 9/8/2006
Posts: 1,698
Location: KY, USA
This was a really cool question... so I spent some time and here's how you do it! Feel free to modify it to meet your needs.

Code:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:Stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
<xsl:stylesheet
    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:ps="urn:percipientstudios-com:xslt"
    
exclude-result-prefixes="msxml umbraco.library ps">


<xsl:output method="xml" omit-xml-declaration="yes"/>

<xsl:param name="currentPage"/>

<xsl:template match="/">


    <xsl:variable name="results" select="ps:parse(umbraco.library:RenderMacroContent('&lt;?UMBRACO_MACRO  macroAlias=&quot;XSLTSearch&quot; source=&quot;0&quot; &gt;&lt;/?UMBRACO_MACRO&gt;', $currentPage/@id))"/>


    <p>TRANSFORMED RESULTS</p>
    <xsl:for-each select="$results/descendant-or-self::div[@class='xsltsearch_result']">
        <p>TRANSFORMED RESULT...</p>
        <p>
        TITLE:<xsl:value-of select="p[@class='xsltsearch_result_title']"/>
        </p>
        <p>
        DESCRIPTION:<xsl:value-of select="p[@class='xsltsearch_result_description']" disable-output-escaping="yes"/>
        </p>
    </xsl:for-each>


</xsl:template>



<msxml:script language="C#" implements-prefix="ps">
<![CDATA[

public XPathNodeIterator parse(String strXML)
{
    strXML = strXML.Replace("&nbsp;","");

    System.IO.StringReader rdr = new System.IO.StringReader(strXML);
    XPathDocument doc = new XPathDocument(rdr);
    XPathNavigator nav = doc.CreateNavigator();

    XPathExpression expr;
    expr = nav.Compile("/");

    XPathNodeIterator iterator = nav.Select(expr);

    return iterator;
}

]]>
</msxml:script>

</xsl:stylesheet>


cheers,
doug.

MVP 2007-2009 - Official Umbraco Trainer for North America - Percipient Studios
drobar
Posted: Tuesday, December 18, 2007 8:51:41 PM

Rank: Umbracoholic

Joined: 9/8/2006
Posts: 1,698
Location: KY, USA
This was a really cool question... so I spent some time and here's how you do it! Feel free to modify it to meet your needs.

You'll need to add the following line to the top of your xslt file, and exclude the ps prefix, as shown below.

Code:

        xmlns:ps="urn:percipientstudios-com:xslt"
    
exclude-result-prefixes="msxml umbraco.library ps">



Code:

<xsl:template match="/">


    <xsl:variable name="results" select="ps:parse(umbraco.library:RenderMacroContent('&lt;?UMBRACO_MACRO  macroAlias=&quot;XSLTSearch&quot; source=&quot;0&quot; &gt;&lt;/?UMBRACO_MACRO&gt;', $currentPage/@id))"/>


    <p>TRANSFORMED RESULTS</p>
    <xsl:for-each select="$results/descendant-or-self::div[@class='xsltsearch_result']">
        <p>TRANSFORMED RESULT...</p>
        <p>
        TITLE:<xsl:value-of select="p[@class='xsltsearch_result_title']"/>
        </p>
        <p>
        DESCRIPTION:<xsl:value-of select="p[@class='xsltsearch_result_description']" disable-output-escaping="yes"/>
        </p>
    </xsl:for-each>


</xsl:template>



<msxml:script language="C#" implements-prefix="ps">
<![CDATA[

public XPathNodeIterator parse(String strXML)
{
    strXML = strXML.Replace("&nbsp;","");

    System.IO.StringReader rdr = new System.IO.StringReader(strXML);
    XPathDocument doc = new XPathDocument(rdr);
    XPathNavigator nav = doc.CreateNavigator();

    XPathExpression expr;
    expr = nav.Compile("/");

    XPathNodeIterator iterator = nav.Select(expr);

    return iterator;
}

]]>
</msxml:script>

</xsl:stylesheet>


cheers,
doug.

MVP 2007-2009 - Official Umbraco Trainer for North America - Percipient Studios
drobar
Posted: Tuesday, December 18, 2007 8:53:15 PM

Rank: Umbracoholic

Joined: 9/8/2006
Posts: 1,698
Location: KY, USA
This was a really cool question... so I spent some time and here's how you do it! Feel free to modify it to meet your needs.

I put spaces in the opening and closing CDATA code to allow it to be posted on the forum. You'll have to remove the extra spaces...

Code:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:Stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
<xsl:stylesheet
    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:ps="urn:percipientstudios-com:xslt"
    
exclude-result-prefixes="msxml umbraco.library ps">


<xsl:output method="xml" omit-xml-declaration="yes"/>

<xsl:param name="currentPage"/>

<xsl:template match="/">


    <xsl:variable name="results" select="ps:parse(umbraco.library:RenderMacroContent('&lt;?UMBRACO_MACRO  macroAlias=&quot;XSLTSearch&quot; source=&quot;0&quot; &gt;&lt;/?UMBRACO_MACRO&gt;', $currentPage/@id))"/>


    <p>TRANSFORMED RESULTS</p>
    <xsl:for-each select="$results/descendant-or-self::div[@class='xsltsearch_result']">
        <p>TRANSFORMED RESULT...</p>
        <p>
        TITLE:<xsl:value-of select="p[@class='xsltsearch_result_title']"/>
        </p>
        <p>
        DESCRIPTION:<xsl:value-of select="p[@class='xsltsearch_result_description']" disable-output-escaping="yes"/>
        </p>
    </xsl:for-each>


</xsl:template>



<msxml:script language="C#" implements-prefix="ps">
<! [ C D A T A [

public XPathNodeIterator parse(String strXML)
{
    strXML = strXML.Replace("&nbsp;","");

    System.IO.StringReader rdr = new System.IO.StringReader(strXML);
    XPathDocument doc = new XPathDocument(rdr);
    XPathNavigator nav = doc.CreateNavigator();

    XPathExpression expr;
    expr = nav.Compile("/");

    XPathNodeIterator iterator = nav.Select(expr);

    return iterator;
}

] ] >
</msxml:script>

</xsl:stylesheet>


cheers,
doug.

MVP 2007-2009 - Official Umbraco Trainer for North America - Percipient Studios
mortenbock
Posted: Tuesday, December 18, 2007 9:18:42 PM

Rank: Addict

Joined: 7/19/2006
Posts: 790
Location: Århus, Denmark
Exactly what I was thinking :-)

Morten Bock - Level 2 certified - MVP 2008/2009 - My danish blog with a few english posts

Arjandb
Posted: Wednesday, December 19, 2007 9:11:13 AM
Rank: Devotee

Joined: 8/31/2007
Posts: 50
Location: Utrecht NL
Can't msxml:node-set() be used for this instead of the custum function?
mortenbock
Posted: Wednesday, December 19, 2007 10:21:36 AM

Rank: Addict

Joined: 7/19/2006
Posts: 790
Location: Århus, Denmark
maybe, but I've never seen a msxml function used in umbraco, and just assumed that is was not supported. Can anyone test it?

Morten Bock - Level 2 certified - MVP 2008/2009 - My danish blog with a few english posts

Arjandb
Posted: Wednesday, December 19, 2007 11:20:13 AM
Rank: Devotee

Joined: 8/31/2007
Posts: 50
Location: Utrecht NL
I've used msxml:node-set(), it works fine as long as you add xmlns:msxml="urn:schemas-microsoft-com:xslt" to xsl:stylesheet.
Users browsing this topic
Guest


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.