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?
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:
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>
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
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:
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
Hi Jing,
Try this out - it's fairly self-explanatory and can be easily modified:
/Chriztian
Yay, that seems to be the elegant version of it, definitely something to remember. :D
Thanks Chriztian!
Thanks Sascha!
I think it helps illustrate the difference in thinking, using XSLT compared to a lot of other languages.
/Chriztian
Thanks Chriztian
Hi Chriztian. Just some question about your XSLT
in the select, is "News" a document type or you can use any name you like?
also
Is "NewsArticle" a document type as well?
Thanks
Hi Jing,
Yes, News and NewsArticle are the Document Types you've set up.
/Chriztian
is working on a reply...