Copied to clipboard

Flag this post as spam?

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


  • Antonio 3 posts 23 karma points
    Mar 13, 2012 @ 15:48
    Antonio
    0

    Help Parse Macro Parameters from Macro Container

    Hi,

    my xml of macro container is:

    <slideShow>

    <?UMBRACO_MACRO  macroalias="SlideShowHomeItem"  linkArticolo="1078"  imgAlternativa=""  titleAlternativo=""  />

    <?UMBRACO_MACRO  macroalias="SlideShowHomeItem"  linkArticolo="1079"  imgAlternativa=""  titleAlternativo=""  />

    </slideShow>

    My requirement is to read  through a xslt/for-each properties of each macro in the macro container.


    How can I do? Thanks to everyone for availability.

  • Antonio 3 posts 23 karma points
    Mar 14, 2012 @ 16:59
    Antonio
    0

    To be clearer, I have a macro container and a new macro I have to read through the items in the container macro.

    I'm looking at every way to read items through the new macro with a foreach loop, but without success.

    The XML above is generated by the macro container.

    I hope you understand. Thanks in advance for the answer

     

  • Antonio 3 posts 23 karma points
    Mar 15, 2012 @ 12:01
    Antonio
    0

    I solved with XSLT extensions:

    ///<summary>
    /// XSLT extensions, referenced from /config/xsltExtensions.config:
    ///   <ext assembly="/bin/MyAssembly" type="MyTypeName" alias="MyExtensions"/>
    ///</summary>
    public class MyExtensions
    {
        private static Regex MacroExpression = new Regex("<?umbraco_macro (.*?)/>",
            RegexOptions.Singleline | RegexOptions.IgnoreCase);
        private static Regex PropertyExpression = new Regex("(\\S*?)=\"(.*?)\"",
            RegexOptions.Singleline | RegexOptions.IgnoreCase);
     
        public XPathNodeIterator DecodeMacroReferences(string input)
        {
            var doc = new XDocument();
            var macros = new XElement("macros");
            doc.Add(macros);
     
            foreach (Match match in MacroExpression.Matches(input))
            {
                var macro = new XElement("macro");
                foreach (Match propMatch in PropertyExpression.Matches(match.Groups[1].Value))
                {
                    macro.Add(new XAttribute(propMatch.Groups[1].Value, propMatch.Groups[2].Value));
                }
                macros.Add(macro);
            }
     
            return doc.CreateNavigator().Select("/");
        }
    }

    found at: http://www.rightpoint.com/community/blogs/viewpoint/archive/2010/09/29/treating-umbraco-macro-references-as-data.aspx

    that has transformed the xml generated by the macro container:

    <macros>

    <macro macroalias="SlideShowHomeItem" linkArticolo="1078" imgAlternativa="" titleAlternativo="" />

    <macro macroalias="SlideShowHomeItem" linkArticolo="1079" imgAlternativa="" titleAlternativo="" />

    </macros>

    Now I can access the properties of each element with a simple loop:

    <xsl:variable name="macroSlideShow" select="$currentPage/slideShow" /><!--Alias Macro Container in Document Types-->

     <xsl:for-each select="msxml:node-set(Ext.ItemMacroContainer:DecodeMacroReferences($macroSlideShow))/macros/child::*">
        <xsl:variable name="currentNode" select="."/>
        <textarea>
          <xsl:value-of select="$currentNode/@linkArticolo"/>
        </textarea>
      </xsl:for-each

    Happy to have solved, I was about to leave.

    Umbraco v 4.7.1.1

    If there is a better solution let me know :-)

Please Sign in or register to post replies

Write your reply to:

Draft