Copied to clipboard

Flag this post as spam?

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


  • lingjing 28 posts 45 karma points
    Aug 10, 2010 @ 04:45
    lingjing
    0

    Problem with listing HTLM strcuture with XSLT

    Hi all. I am using Umbraco 4.5.1. and I have below site structure with my news section

    content

      -- news

         -- news article 1

         -- news article 2

         -- etc

     

    and my HTLM code for listing news article is:

    <div class = 'news-row'>

      <div class = 'news-cell' > <a href= "" >news article 1 title </a> </div>

      <div class = 'news-cell' > <a href= "" >news article 2 title </a> </div>

    </div>

    <div class = 'news-row'>

      <div class = 'news-cell' > <a href= "" >news article 3 title </a> </div>

      <div class = 'news-cell' > <a href= "" >news article 4 title </a> </div>

    </div>

     

    I am having problem with populating this HTLM strcuture with XSLT. It does not allow me to have open div tag without closing tag. Can anyone let me now how to fix it?

    Thanks

    Jing

  • Sascha Wolter 615 posts 1101 karma points
    Aug 10, 2010 @ 12:21
    Sascha Wolter
    0

    Hi Jing,

    I guess you are doing something like: loop through all news items, bundle every two together by surrounding them with a div?

    I would have a workaround for you which is not really elegant but - well - works: you can output the divs directly to the response without using <div> tags in xslt like so:

    <xsl:text disable-output-escaping="yes">&lt;div&gt;</xsl:text>
    hello
    <xsl:text disable-output-escaping="yes">&lt;/div&gt;</xsl:text>

    That will take care of Xslt comlaining about unclosed divs. However it really is a hack and I would avoid that unless absolutely necessary.

    Regards,
    Sascha

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 8x admin c-trib
    Aug 10, 2010 @ 20:48
    Chriztian Steinmeier
    2

    Hi Jing,

    Try this out - it's fairly self-explanatory and can be easily modified:

    <?xml version="1.0" encoding="utf-8" ?>
    <xsl:stylesheet
        version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:umbraco.library="urn:umbraco.library"
        exclude-result-prefixes="umbraco.library"
    >
    
        <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
    
        <xsl:param name="currentPage" />
        <xsl:variable name="siteRoot" select="$currentPage/ancestor-or-self::*[@level = 1]" />
    
        <!-- You can also grab this by id if you want -->
        <xsl:variable name="news" select="$siteRoot/descendant-or-self::News" />
    
        <xsl:template match="/">
            <!-- Apply templates to every second article in "row" mode, starting with the first  -->
            <xsl:apply-templates select="$news/NewsArticle[position() mod 2 = 1]" mode="row" />
        </xsl:template>
    
        <!-- Template to start a row -->
        <xsl:template match="NewsArticle" mode="row">
            <div class="news-row">
                <!-- Now render this article and the following -->
                <xsl:apply-templates select=". | following-sibling::NewsArticle[1]" mode="cell" />
            </div>
        </xsl:template>
    
        <!-- Template for a News Article -->
        <xsl:template match="NewsArticle" mode="cell">
            <div class="news-cell">
                <a href="{umbraco.library:NiceUrl(@id)}">
                    <xsl:value-of select="@nodeName" />
                </a>
            </div>
        </xsl:template>
    
    </xsl:stylesheet>
    

    /Chriztian 

  • Sascha Wolter 615 posts 1101 karma points
    Aug 10, 2010 @ 23:57
    Sascha Wolter
    0

    Yay, that seems to be the elegant version of it, definitely something to remember. :D

    Thanks Chriztian!

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 8x admin c-trib
    Aug 11, 2010 @ 00:15
    Chriztian Steinmeier
    0

    Thanks Sascha!

    I think it helps illustrate the difference in thinking, using XSLT compared to a lot of other languages.

    /Chriztian  

  • lingjing 28 posts 45 karma points
    Aug 11, 2010 @ 01:22
    lingjing
    0

    Thanks Chriztian

  • lingjing 28 posts 45 karma points
    Aug 11, 2010 @ 02:10
    lingjing
    0

    Hi Chriztian. Just some question about your XSLT

    <xsl:variable name="news" select="$siteRoot/descendant-or-self::News" />

    in the select, is "News" a document type or you can use any name you like?

    also

    <xsl:apply-templates select="$news/NewsArticle[position() mod 2 = 1]" mode="row" />

    Is  "NewsArticle" a document type as well?

    Thanks

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

    Hi Jing,

    Yes, News and NewsArticle are the Document Types you've set up.

    /Chriztian

Please Sign in or register to post replies

Write your reply to:

Draft