I have a xsl template that groups items by their category. Now i want to group them together inside those categories.
my xsl script:
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
<xsl:variable name="prefix_param">udt_<xsl:value-of select="//udt:Context/udt:ModuleId" />_param</xsl:variable>
<xsl:key name="data-by-Category" match="udt:Data" use="udt:Category" />
<xsl:template match="udt:Data" mode="list">
<li>
<xsl:value-of select="udt:Subcategory" disable-output-escaping="yes" />
<xsl:value-of select="udt:Information" disable-output-escaping="yes" />
</li>
</xsl:template>
<xsl:template match="/udt:UserDefinedTable">
<xsl:for-each select="udt:Data[count(. | key('data-by-Category', udt:Category)[1]) = 1]">
<xsl:sort select="udt:Zap" />
<span>
<h1>
<xsl:value-of select="udt:Category" disable-output-escaping="yes" /> </h1>
</span>
<xsl:variable name="currentData" select="key('data-by-Category', udt:Category)" />
<xsl:if test="$currentData">
<ul>
<xsl:apply-templates select="$currentData" mode="list">
<xsl:sort select="udt:PodCategory" order="ascending" />
</xsl:apply-templates>
</ul>
</xsl:if>
</xsl:for-each>
</xsl:template>
<xsl:template name="EditLink">
<xsl:if test="udt:EditLink">
<a href="{udt:EditLink}">
<img border="0" alt="edit" src="{//udt:Context/udt:ApplicationPath}/images/edit.gif" />
</a>
</xsl:if>
</xsl:template>
The sample output i get now:
<h1>Category 1</h1>
<ul>
<li>Subcategory 1 info 1</li>
<li>Subcategory 2 info 1</li>
<li>Subcategory 2 info 2</li>
<li>Subcategory 2 info 3</li>
</ul>
<h1>Category 2</h1>
<ul>
<li>Subcategory 1 info 1</li>
<li>Subcategory 1 info 2</li>
<li>Subcategory 2 info 1</li>
</ul>
And this is the sample output i want to achieve:
<h1>Category 1</h1>
<h2>Subcategory 1</h2>
<ul>
<li>info 1</li>
</ul>
<h2>Subcategory 2</h2>
<ul>
<li>info 1</li>
<li>info 2</li>
<li>info 3</li>
</ul>
<h1>Category 2</h1>
<h2>Subcategory 1</h2>
<ul>
<li>info 1</li>
<li>info 2</li>
</ul>
<h2>Subcategory 1</h2>
<ul>
<li>info 1</li>
</ul>
I want to gtoup the items inside each group in a secondary group and display its title aswell.
Define a second key
<xsl:key name="data-by-Category-and-Subcategory" match="udt:Data" use="concat(udt:Category, '|', udt:Subcategory)" />
and then use e.g. that to identify sub groups
<xsl:template match="/udt:UserDefinedTable">
<xsl:for-each select="udt:Data[count(. | key('data-by-Category', udt:Category)[1]) = 1]">
<xsl:sort select="udt:Zap" />
<span>
<h1>
<xsl:value-of select="udt:Category" disable-output-escaping="yes" /> </h1>
</span>
<xsl:variable name="currentData" select="key('data-by-Category', udt:Category)" />
<xsl:for-each select="$currentData[generate-id() = generate-id(key('data-by-Category-and-Subcategory', concat(udt:Category, '|', udt:Subcategory))[1])]">
<h2><xsl:value-of select="udt:Subcategory"/></h2>
<xsl::variable name="$sub-group" select="key('data-by-Category-and-Subcategory', concat(udt:Category, '|', udt:Subcategory)"/>
<ul>
<xsl:apply-templates select="$sub-group" mode="list">
<xsl:sort select="udt:PodCategory" order="ascending" />
</xsl:apply-templates>
</ul>
</xsl:if>
</xsl:for-each>
</xsl:template>
Related
I have a xslt code to convert an xml file to html list. The input and output samples are as follows:
Input:
<Beverages>
<Water/>
<Coffee/>
<Tea>
<BlackTea/>
<WhiteTea id="cti" value="ctv" >Camomile Tea</WhiteTea>
<GreenTea id="gti" value="gtv">
<Sencha/>
<Gyokuro/>
<Matcha/>
<PiLoChun/>
</GreenTea>
</Tea>
</Beverages>
and the Output:
<ul>
<li>
<span class="caret"><Beverages></span>
<ul class="nested">
<li><span><Water/></span></li>
<li><span><Coffee/></span></li>
<li>
<span class="caret"><Tea></span>
<ul class="nested">
<li><span><BlackTea/></span></li>
<li><span><WhiteTea id="cti" value="ctv"></span>Camomile Tea<span></WhiteTea></span></li>
<li>
<span><GreenTea id="gti" value="gtv"></span>
<ul class="nested">
<li><span><Sencha/></span></li>
<li><span><Gyokuro/></span></li>
<li><span><Matcha/></span></li>
<li><span><PiLoChun/></span></li>
</ul>
<span></GreenTea></span>
</li>
</ul>
</li>
</ul>
<span></Beverages></span>
</li>
</ul>
Here is my xslt but it is not exactly the same as what I want:
<xsl:template match="/">
<ul><xsl:apply-templates/></ul>
</xsl:template>
<xsl:template match="*">
<li> <span class="caret"><xsl:value-of select="concat('<',name())" />
<xsl:for-each select="#*">
<xsl:value-of select="concat(' ',name())"/>=<xsl:value-of select="concat('"',.,'"')" />
</xsl:for-each>></span>
<xsl:if test="text()">
<xsl:apply-templates select="text()" />
</xsl:if>
<xsl:if test="*">
<ul class="nested"><xsl:apply-templates/></ul>
</xsl:if>
<span><xsl:value-of select="concat('<','/' ,name() , '>')" /></span></li>
</xsl:template>
The problem is that this code can not differentiate between parent elements and non parent element. for parent elements we need to add class="caret" attribute, but for non parent elements we shouldn't add this class.
This should work for you.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<ul>
<xsl:apply-templates/>
</ul>
</xsl:template>
<xsl:template match="*">
<li>
<span>
<xsl:if test="*">
<xsl:attribute name="class">
<xsl:value-of select="'caret'"/>
</xsl:attribute>
</xsl:if>
<xsl:value-of select="concat('<',name())"/>
<xsl:for-each select="#*">
<xsl:value-of select="concat(' ', name(), '=', '"', ., '"')"/>
</xsl:for-each>
<xsl:choose>
<xsl:when test="text() or *">
<xsl:value-of select="'>'"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'\>'"/>
</xsl:otherwise>
</xsl:choose>
</span>
<xsl:if test="text()">
<xsl:value-of select="text()"/>
</xsl:if>
<xsl:if test="*">
<ul class="nested">
<xsl:apply-templates/>
</ul>
</xsl:if>
<xsl:if test="text() or *">
<span>
<xsl:value-of select="concat('<','/' ,name() , '>')"/>
</span>
</xsl:if>
</li>
</xsl:template>
</xsl:stylesheet>
I am fairly new at this and I am not sure if it is possible to do what I want to achieve here. I have 3 for-each loops that stores a number for every chapter, section and paragraph. I want to get that stored number from the previous for-each loop and display it in the nested loop, but I can't get it to work.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head>
<title>Table of Contents, Chapter 3</title>
</head>
<body>
<xsl:for-each select="chapter">
<tr>
<xsl:variable name="capnr">
<xsl:number value="3" format="1. "/>
</xsl:variable>
<xsl:value-of select="$capnr"/>
<xsl:value-of select="#title"/>
</tr>
<br />
<xsl:for-each select="section">
<tr>
<xsl:variable name="secnr">
<xsl:number format="1. "/>
</xsl:variable>
<xsl:value-of select="$capnr"/>
<xsl:value-of select="$secnr"/>
<xsl:value-of select="#title"/>
</tr>
<br />
<xsl:for-each select="paragraph">
<tr>
<xsl:variable name="parnr">
<xsl:number format="1 "/>
</xsl:variable>
<xsl:value-of select="$capnr"/>
<xsl:value-of select="$secnr"/>
<xsl:value-of select="$parnr"/>
<xsl:value-of select="#title"/>
</tr>
</xsl:for-each>
<br />
</xsl:for-each>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
XML:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="ToC-3.xsl"?>
<chapter title="Chapter 3: Expressions">
<section title="Variables">
<paragraph title="Simple variables"></paragraph>
<paragraph title="Text variables"></paragraph>
<paragraph title="Remote identifiers"></paragraph>
</section>
<section title="The logical operators">
<paragraph title="Precedence of Boolean operators"></paragraph>
</section>
<section title="Designational expressions">
</section>
</chapter>
Without more details on the input and output, this should work. Of note is that the variable name is on the root, to allow all templates to access it. but since there appears to be one XSl per chapter this shouldn't be a problem.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="capnr">
<xsl:number value="3" format="1."/>
</xsl:variable>
<xsl:template match="/">
<html>
<head>
<title>Table of Contents, Chapter 3</title>
</head>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="chapter">
<h1>
<xsl:value-of select="$capnr"/>
<xsl:value-of select="#title"/>
</h1>
<ol><xsl:apply-templates/></ol>
</xsl:template>
<xsl:template match="section">
<li>
<xsl:value-of select="$capnr"/>
<xsl:value-of select="position()"/>
<xsl:text> - </xsl:text>
<xsl:value-of select="#title"/>
<ol><xsl:apply-templates/></ol>
</li>
</xsl:template>
<xsl:template match="paragraph">
<li>
<xsl:value-of select="$capnr"/>
<xsl:value-of select="count(../preceding-sibling::*) + 1"/>
<xsl:text>.</xsl:text>
<xsl:value-of select="position()"/>
<xsl:text> - </xsl:text>
<xsl:value-of select="#title"/>
<ol><xsl:apply-templates/></ol>
</li>
</xsl:template>
</xsl:stylesheet>
The above when applied to the given input XMl gives:
<html>
<head>
<title>Table of Contents, Chapter 3</title>
</head>
<body>
<h1>3.Chapter 3: Expressions</h1>
<ol><li>3.1 - Variables<ol><li>3.1.1 - Simple variables<ol></ol>
</li>
<li>3.1.2 - Text variables<ol></ol>
</li>
<li>3.1.3 - Remote identifiers<ol></ol>
</li>
</ol>
</li>
<li>3.2 - The logical operators<ol><li>3.2.1 - Precedence of Boolean operators<ol></ol>
</li>
</ol>
</li>
<li>3.3 - Designational expressions<ol></ol>
</li>
</ol>
</body>
</html>
I am trying to create a nav menu in Composite C1 for the Bootstrap framework, but I am unsure how to get submenus to show up in the menu. Here is what I have
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:in="http://www.composite.net/ns/transformation/input/1.0"
xmlns:lang="http://www.composite.net/ns/localization/1.0"
xmlns:f="http://www.composite.net/ns/function/1.0"
xmlns="http://www.w3.org/1999/xhtml"
exclude-result-prefixes="xsl in lang f">
<xsl:param name="sitemap" select="/in:inputs/in:result[#name='SitemapXml']/Page" />
<xsl:template match="/">
<html>
<head>
</head>
<body>
<div class="container">
<div class="navbar">
<div class="navbar-inner">
<ul class="nav">
<xsl:for-each select="$sitemap[#isopen='true']">
<li>
<a href="{#URL}">
<xsl:if test="#iscurrent='true'" >
<xsl:attribute name="class">active</xsl:attribute>
</xsl:if>
<xsl:value-of select="#MenuTitle" />
</a>
</li>
<xsl:apply-templates select="Page" />
</xsl:for-each>
</ul>
</div>
</div>
</div>
</body>
</html>
</xsl:template>
<xsl:template match="Page">
<xsl:if test="count(#MenuTitle)">
<li>
<a href="{#URL}">
<xsl:if test="#isopen='true'" >
<xsl:attribute name="class">active</xsl:attribute>
</xsl:if>
<xsl:value-of select="#MenuTitle" />
</a>
</li>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
I am unsure what I need to add to get the sub-menus. Suggestions?
Try out something like this
<xsl:template match="/">
<html>
<head>
</head>
<body>
<div class="container">
<div class="navbar">
<div class="navbar-inner">
<ul class="nav">
<xsl:for-each select="$sitemap[#isopen='true']">
<li>
<xsl:if test="#iscurrent='true'" >
<xsl:attribute name="class">active</xsl:attribute>
</xsl:if>
<a href="{#URL}">
<xsl:value-of select="#MenuTitle" />
</a>
</li>
<xsl:apply-templates select="Page" />
</xsl:for-each>
</ul>
</div>
</div>
</div>
</body>
</html>
</xsl:template>
<xsl:template match="Page">
<xsl:variable name="depth" select="#Depth" />
<xsl:variable name="isDropDown" select="count(./*) > 0 and $depth < 3" />
<xsl:if test="count(#MenuTitle)">
<li>
<xsl:if test="#isopen='true'" >
<xsl:attribute name="class">active</xsl:attribute>
</xsl:if>
<xsl:if test="$isDropDown" >
<xsl:attribute name="class">dropdown</xsl:attribute>
</xsl:if>
<a href="{#URL}">
<xsl:if test="$isDropDown" >
<xsl:attribute name="class">dropdown-toggle</xsl:attribute>
</xsl:if>
<xsl:if test="$isDropDown" >
<xsl:attribute name="data-toggle">dropdown</xsl:attribute>
</xsl:if>
<xsl:value-of select="#MenuTitle" />
<xsl:if test="$isDropDown" >
<b class="caret"></b>
</xsl:if>
</a>
<xsl:if test="$isDropDown">
<ul class="dropdown-menu">
<xsl:apply-templates select="./*" />
</ul>
</xsl:if>
</li>
</xsl:if>
I have this xsl bellow :
<xsl:template match="/" xmlns:x="http://www.w3.org/2001/XMLSchema" xmlns:d="http://schemas.microsoft.com/sharepoint/dsp" xmlns:asp="http://schemas.microsoft.com/ASPNET/20" xmlns:__designer="http://schemas.microsoft.com/WebParts/v2/DataView/designer" xmlns:SharePoint="Microsoft.SharePoint.WebControls">
<div id="vtab">
<ul>
<xsl:call-template name="dvt_1.body"/>
</ul>
</div>
</xsl:template>
<xsl:template name="dvt_1.body">
<xsl:for-each select="/dsQueryResponse/Rows/Row[generate-id(.)=generate-id(key('TitleKey', #Title))]">
<li>
<xsl:value-of select="#Title"/>
</li>
<xsl:variable name="thisClassification" select="#Title" />
<div>
<xsl:for-each select="../Row[#Title = $thisClassification]">
<xsl:value-of select="#Model"/>
</xsl:for-each>
</div>
</xsl:for-each>
</xsl:template>
I would like have this output:
<div id="vtab">
<ul>
<li>HTC</li>
<li>Nokia</li>
</ul>
<div>HTC Sensation 345-HTC Ev</div>
<div>Nokia</div>
</div>
But now this is what i'm getting
<div id="vtab">
<ul>
<li>HTC</li>
<div>HTC Sensation 345HTC Evo</div>
<li>Nokia</li>
<div>Nokia</div>
</ul>
</div>
How can i exit the UL element and start the DIV element.
Thanks alot
It is not optimal, but consider making two loops something like the following:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<div id="vtab">
<xsl:call-template name="LOOP_1"/>
<xsl:call-template name="LOOP_2"/>
</div>
</xsl:template>
<xsl:template name="LOOP_1">
<ul>
<xsl:for-each select="/dsQueryResponse/Rows/Row[generate-id(.)=generate-id(key('TitleKey', #Title))]">
<li>
<xsl:value-of select="#Title"/>
</li>
<xsl:variable name="thisClassification" select="#Title"/>
<div>
<xsl:for-each select="../Row[#Title = $thisClassification]">
<xsl:value-of select="#Model"/>
</xsl:for-each>
</div>
</xsl:for-each>
</ul>
</xsl:template>
<xsl:template name="LOOP_2">
<div>
<xsl:for-each select="/dsQueryResponse/Rows/Row[generate-id(.)=generate-id(key('TitleKey', #Title))]">
<xsl:variable name="thisClassification" select="#Title"/>
<div>
<xsl:for-each select="../Row[#Title = $thisClassification]">
<xsl:value-of select="#Model"/>
</xsl:for-each>
</div>
</xsl:for-each>
</div>
</xsl:template>
</xsl:stylesheet>
I am not sure of your requirements, but it may be more efficient to review the desired output, and output nested lists and format it using CSS rather than creating separate loops
<xsl:param name="currentPage"/>
<xsl:param name="group-size" select="'2'" />
<xsl:template match="/">
<xsl:variable name="userBlogSource" select="umbraco.library:GetXmlNodeById(umbraco.library:GetDictionaryItem('WeblogNode'))/node [string(data [#alias='umbracoNaviHide']) != '1' and data[#alias='author'] = $currentPage/#id]" />
<xsl:for-each select="$userBlogSource">
<xsl:sort select="data[#alias='dato']" order="descending" />
</xsl:for-each>
<xsl:if test="count($userBlogSource) > 0">
<h3><xsl:value-of select="umbraco.library:GetDictionaryItem('BlogpostsWrittenBy')"/> <xsl:value-of select="$currentPage/#nodeName" />:</h3>
<xsl:apply-templates select="$userBlogSource[(position() mod $group-size) = 1]" />
<ul>
<xsl:for-each select="$userBlogSource">
<li>
<a rel="bookmark" href="{umbraco.library:NiceUrl(#id)}" title="{#nodeName}">
<xsl:value-of select="#nodeName" />
</a>
</li>
</xsl:for-each>
</ul>
</xsl:if>
</xsl:template>
<xsl:template match="node">
<xsl:variable name="postnum" select="position()" />
<div class="weblog-posts-wrap">
<ul>
<xsl:for-each select=". | following-sibling::node[position() < $group-size]">
<li>
<a rel="bookmark" href="{umbraco.library:NiceUrl(#id)}" title="{#nodeName}">
<xsl:value-of select="#nodeName" />
</a>
</li>
</xsl:for-each>
</ul>
</div>
</xsl:template>
When I check the count of $userBlogSource it contains three elements. When I attempt to print the in a regular XSLT for loop it does print the correct elements. But when the variable is passed to the second template matching node, the content is suddenly different. Instead of the three nodes it should contain, it contains 4 nodes by entirely different authors.
Any idea of what I may be doing wrong?