|
|
 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('<?UMBRACO_MACRO macroAlias="XSLTSearch" source="-1" searchFields="@nodeName,PageTitle,secondaryTitle,bodyText" previewFields="bodyText,secondaryTitle,PageTitle" searchBoxLocation="NONE" previewType="CONTEXT" resultsPerPage="5" previewChars="100" showPageRange="1" showOrdinals="1" showScores="0" showStats="1"></?UMBRACO_MACRO>', $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
|
|
 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.
|
|
 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
|
|
 Rank: Addict
Joined: 7/19/2006 Posts: 790 Location: Århus, Denmark
|
You need to return an XPathNodeIterator i think.
|
|
 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 " "> ]> <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('<?UMBRACO_MACRO macroAlias="XSLTSearch" source="0" ></?UMBRACO_MACRO>', $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(" ","");
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
|
|
 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('<?UMBRACO_MACRO macroAlias="XSLTSearch" source="0" ></?UMBRACO_MACRO>', $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(" ","");
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
|
|
 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 " "> ]> <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('<?UMBRACO_MACRO macroAlias="XSLTSearch" source="0" ></?UMBRACO_MACRO>', $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(" ","");
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
|
|
 Rank: Addict
Joined: 7/19/2006 Posts: 790 Location: Århus, Denmark
|
Exactly what I was thinking :-)
|
|
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?
|
|
 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?
|
|
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.
|
|
|
Guest |