Copied to clipboard

Flag this post as spam?

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


  • AlexR 39 posts 61 karma points
    May 05, 2010 @ 09:20
    AlexR
    0

    What is the best way to return XML from template

    I created template that return teaserImage XML and is protected from empty numbers:

    <xsl:template name="teaserImage">
    <xsl:param name="currentPage" />

    <xsl:if test="number($currentPage/data[@alias='teaserImage'])">
    <xsl:copy-of select="umbraco.library:GetMedia($currentPage/data[@alias='teaserImage'], 'false')" />
    </xsl:if>
    </xsl:template>

    <xsl:variable name="teaserImage">
    <xsl:call-template name="teaserImage">
    <xsl:with-param name="currentPage" select="$currentPage" />
    </xsl:call-template>
    </xsl:variable>

     

    Here I used copy-of function. And I think that using it is not the best way.

    Can I return full XML without copy it?

    For example, I can set variable without copy XML, but it is not protected:

    <xsl:variable name="teaserImage" select="umbraco.library:GetMedia($currentPage/data[@alias='teaserImage'], 'false')" />

     

     What is the best way to return XML from my template?

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 8x admin c-trib
    May 05, 2010 @ 09:37
    Chriztian Steinmeier
    0

    Hi AlexR,

    I just posted an answer to your other post about this.

    /Chriztian 

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 8x admin c-trib
    May 05, 2010 @ 09:42
    Chriztian Steinmeier
    1

    Hi again,

    Sorry, totally misunderstood your question I think.

    If you need the media item XML, you need to use the copy-of instruction - that's how you do it.

    /Chriztian

  • AlexR 39 posts 61 karma points
    May 05, 2010 @ 09:45
    AlexR
    0

    Thank you for answer. I asked because I just started to use XSLT.

    If using copy-of is the only way to return XML I will use it.

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 8x admin c-trib
    May 05, 2010 @ 10:00
    Chriztian Steinmeier
    0

    Just to clarify:

    The XML for the media item is only available through the library extension, so that's why you need to use the copy-of instruction to actually get it back from a template call.

    If you were to return the finished <img> (XML) tag from the template, you could just build it as a literal result, e.g.:

    <xsl:template name="teaserImage">
      <xsl:param name="currentPage" />
    
      <xsl:if test="number($currentPage/data[@alias='teaserImage'])">
        <xsl:variable name="media" select="umbraco.library:GetMedia($currentPage/data[@alias='teaserImage'], false())" />
        <img src="{$media/data[@alias = 'umbracoFile']}" width="..." height="..." />
      </xsl:if>
    </xsl:template>

     

    Please note I've changed 'false' to false() - a common pitfall with XSLT :-)

    Also note that you could do more checking for missing values etc. if you want to bulletproof it - Lee Kelleher has a blogpost on this, that you should check out.

    /Chriztian

  • AlexR 39 posts 61 karma points
    May 05, 2010 @ 11:05
    AlexR
    0

    Chriztian, thank you for your note about false().

    I returned XML for media because I want to work with image attributes in main template and so on.

    I am creating one XSLT file with useful templates. I will include it into every usual XSLT files and I will use templates from included XSLT file.

    I don't want write same functions in many templates.

     

    So I need to know how to return node-sets from templates.

Please Sign in or register to post replies

Write your reply to:

Draft