Copied to clipboard

Flag this post as spam?

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


  • Andrew Blackmore 84 posts 127 karma points
    Mar 08, 2010 @ 18:01
    Andrew Blackmore
    0

    Ancestor-or-self node issues

    Hello all,

     

    I am having what I believe to be a similar problem is many places on my site. I have made a set up with multiple blogs and each blog has a different header image which is selected on dashboard for the blog. I have a simple macro doing this. i'd rather do it this way because it will be easier for people to switch out the artwork later. Anyways, I had this all set up so it worked on the home page for each blog...not thinking to test it on the articles page until further down the road. Doh! I'm having many problems like this and I believe it is my pathing. Here is one example of something I tried to rectify this...and got an error

            <xsl:value-of select="umbraco.library:GetMedia($currentPage/ancestor-or-self::node [@nodeTypeAlias = 'Blog']//data [@alias='bannerImage'], 'false')/data [@alias = 'umbracoFile']" /> 

    I (apparently incorrectly) assumed that this would look for the blog that was an ancestor of this node and pull the alias bannerImage.

    Not only does it not do this, it throws this error

    System.OverflowException: Value was either too large or too small for an Int32.
    at System.Convert.ToInt32(Double value)
    at System.Double.System.IConvertible.ToInt32(IFormatProvider provider)
    at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
    at System.Xml.Xsl.Runtime.XmlQueryRuntime.ChangeTypeXsltArgument(XmlQueryType xmlType, Object value, Type destinationType)
    at System.Xml.Xsl.Runtime.XmlQueryContext.InvokeXsltLateBoundFunction(String name, String namespaceUri, IList`1[] args)
    at (XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime)
    at Root(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime)
    at Execute(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime)
    at System.Xml.Xsl.XmlILCommand.Execute(Object defaultDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlSequenceWriter results)
    at System.Xml.Xsl.XmlILCommand.Execute(Object defaultDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlWriter writer, Boolean closeWriter)
    at System.Xml.Xsl.XmlILCommand.Execute(IXPathNavigable contextDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlWriter results)
    at System.Xml.Xsl.XmlILCommand.Execute(IXPathNavigable contextDocument, XmlResolver dataSources, XsltArgumentList argumentList, TextWriter results)
    at System.Xml.Xsl.XslCompiledTransform.Transform(IXPathNavigable input, XsltArgumentList arguments, TextWriter results)
    at umbraco.presentation.webservices.codeEditorSave.SaveXslt(String fileName, String oldName, String fileContents, Boolean ignoreDebugging)

     

    I've tried using this and similar in many macros and obviously my two-week-old knowledge of xslt is not helping. Hopefully I've missed something simple.

     

    Thanks for any insights.

  • Tommy Poulsen 514 posts 708 karma points
    Mar 08, 2010 @ 18:32
    Tommy Poulsen
    0

    The GetMedia library function needs an integer as input, and you have to validate that it's not empty before.

    Wrap you function in an if-statement

    <xsl:if test="$currentPage/ancestor-or-self::node [@nodeTypeAlias = 'Blog']//data [@alias='bannerImage'] != ''"/>

    ...

    </xsl:if>

     

    >Tommy

  • Andrew Blackmore 84 posts 127 karma points
    Mar 08, 2010 @ 18:40
    Andrew Blackmore
    0

    Hey Tommy,

     

    Thanks for the quick response.

    If I ignore the image I want added for a minute and use this code

            <xsl:if test="$currentPage/ancestor-or-self::node [@nodeTypeAlias = 'Blog']//data [@alias='bannerImage'] != ''">
                <p>This is working</p>
            </xsl:if>

    I get a lovely

    Error parsing XSLT file: \xslt\HeaderImage.xslt

     

    I do agree with you that I should be making sure that there is a value and wrapping it in an if statement but the statement I am using to call the images still returns that error if it is in the macro. I would understand if I was returning a wrong value but I'm not...it's giving me an error <<see above>>.

  • Tommy Poulsen 514 posts 708 karma points
    Mar 08, 2010 @ 18:42
    Tommy Poulsen
    0

    Could you post the full xslt ?

  • Andrew Blackmore 84 posts 127 karma points
    Mar 08, 2010 @ 18:47
    Andrew Blackmore
    0

    Sure...

    <?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: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:autofolders.library="urn:autofolders.library" xmlns:tagsLib="urn:tagsLib" xmlns:BlogLibrary="urn:BlogLibrary" xmlns:fmautolink="urn:fmautolink"
        exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets autofolders.library tagsLib BlogLibrary fmautolink ">


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

    <xsl:param name="currentPage"/>

    <xsl:template match="/">

    <!-- start writing XSLT -->

        <xsl:if test="$currentPage/data [@alias='bannerImage'] != ''">
                <img alt="{$currentPage/@nodeName}">
                  <xsl:attribute name="src">
            <xsl:if test="$currentPage/ancestor-or-self::node [@nodeTypeAlias = 'Blog']//data [@alias='bannerImage'] != ''">
                <xsl:value-of select="umbraco.library:GetMedia($currentPage/ancestor-or-self::node [@nodeTypeAlias = 'Blog']//data [@alias='bannerImage'] , 'false')/data [@alias = 'umbracoFile']" />
            </xsl:if>
                  </xsl:attribute>   
                </img>
             </xsl:if>

    </xsl:template>

    </xsl:stylesheet>
  • Tommy Poulsen 514 posts 708 karma points
    Mar 08, 2010 @ 18:52
    Tommy Poulsen
    0

    When do you get the error - when saving or on the page when using it?

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Mar 08, 2010 @ 18:57
    Lee Kelleher
    2

    Hi Andrew,

    You are so close... the parsing error is due to the XPath syntax you used.  The double-slash (//data) would return a node-set, as opposed to a string-value.  Best bet would be to take a step back and work through the data you need.

    <!-- assign the root blog node to a variable-->
    <xsl:variable name="blog" select="$currentPage/ancestor-or-self::node[@nodeTypeAlias = 'Blog']" />
    
    <!-- check if the blog node has a data element for 'banner image' -->
    <xsl:if test="count($blog/data[@alias='bannerImage']) &gt; 0 and $blog/data[@alias='bannerImage'] != ''">
    
        <!-- assign the GetMedia to a variable -->
        <xsl:variable name="media" select="umbraco.library:GetMedia($blog/data[@alias='bannerImage'], 0)" />
    
        <!-- check that the media contains the data -->
        <xsl:if test="count($media/data[@alias='umbracoFile']) &gt; 0">
    
            <!-- do whatever you want to do here -->
            <xsl:value-of select="$media/data[@alias='umbracoFile']" />
    
        </xsl:if>
    
    </xsl:if>

    If you want to read up a little more about GetMedia, I wrote a blog post about it a while ago:

    http://blog.leekelleher.com/2009/11/30/how-to-use-umbraco-library-getmedia-in-xslt/

    Cheers, Lee.

  • Andrew Blackmore 84 posts 127 karma points
    Mar 08, 2010 @ 18:59
    Andrew Blackmore
    0

    Hmmm, odd. I'm not sure what I did but the error is gone...but its still not behaving properly. Thanks for your help so far. Now I'm back to the original issue. When I'm at the top level of the blog, the iamge shows up. The minutes that I go a node deeper, the image is gone, and I figured by using this code to grab the ancestor-or-self and look for the blog node type that I should have been able to get this image for all nodes below the blog.

    The nodes are set up like this

     

    root

    --blog @bannerImage

    ----blog post (these nodes don't find that image)

    ----blog post

    --blog @bannerImage

    ----blog post

    ----blogpost

     

     

  • Andrew Blackmore 84 posts 127 karma points
    Mar 08, 2010 @ 19:11
    Andrew Blackmore
    0

    @lee good catch but that still isn't the issue. I changed that out but oddly enough it grabs the image on the self node but not if it is one of the post nodes.

    The frustration has been whatever issue is holding me up with this issue is the same pathing issue that is catching me with several macros so I must be missing some sort of important concept.

  • Tommy Poulsen 514 posts 708 karma points
    Mar 08, 2010 @ 19:16
    Tommy Poulsen
    0

    do you get any img tag at all in your blog post? I assume your blog post nodes are of some other type than Blog.

  • Tommy Poulsen 514 posts 708 karma points
    Mar 08, 2010 @ 19:18
    Tommy Poulsen
    1

    oops,

    <xsl:if test="$currentPage/data [@alias='bannerImage'] != ''"> 

    that only goes for the blog nodes

     

  • Andrew Blackmore 84 posts 127 karma points
    Mar 08, 2010 @ 19:19
    Andrew Blackmore
    0

    @Tommy I do not get any images unless they were part of the post. Any images (blog roll, header graphic, etc.) do not show up

  • Andrew Blackmore 84 posts 127 karma points
    Mar 08, 2010 @ 19:22
    Andrew Blackmore
    0

    You are correct about it only working for the blog nodes. As a side note, if I remove that <xsl:if part, thats when I get an error while saving. It still works though on the page.

  • Tommy Poulsen 514 posts 708 karma points
    Mar 08, 2010 @ 19:22
    Tommy Poulsen
    1

    But if you use your original source code the outer if-statement will only work for the blog nodes, right?

  • Andrew Blackmore 84 posts 127 karma points
    Mar 08, 2010 @ 19:23
    Andrew Blackmore
    0

    OOOOOOOOO wow. I get what you are saying. Yes you are correct...but unsure of my way around that

  • Tommy Poulsen 514 posts 708 karma points
    Mar 08, 2010 @ 19:24
    Tommy Poulsen
    0

    Use Lee's code would probably work- he defines the right path as a variable used in testing, getmedia etc.

  • Andrew Blackmore 84 posts 127 karma points
    Mar 08, 2010 @ 19:25
    Andrew Blackmore
    0

    Duhhh.

    <?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: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:autofolders.library="urn:autofolders.library" xmlns:tagsLib="urn:tagsLib" xmlns:BlogLibrary="urn:BlogLibrary" xmlns:fmautolink="urn:fmautolink"
        exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets autofolders.library tagsLib BlogLibrary fmautolink ">


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

    <xsl:param name="currentPage"/>

    <xsl:template match="/">

    <!-- start writing XSLT -->

        <xsl:if test="$currentPage/ancestor-or-self::node [@nodeTypeAlias = 'Blog']/data [@alias='bannerImage'] != ''">
                <img alt="{$currentPage/@nodeName}">
                  <xsl:attribute name="src">

                <xsl:value-of select="umbraco.library:GetMedia($currentPage/ancestor-or-self::node [@nodeTypeAlias = 'Blog']/data [@alias='bannerImage'] , 'false')/data [@alias = 'umbracoFile']" />

                  </xsl:attribute>   
                </img>
             </xsl:if>

    </xsl:template>

    </xsl:stylesheet>

    Thank you SOOOOO much for your help

Please Sign in or register to post replies

Write your reply to:

Draft