I am using the web.sitemap which is created using VS.net 2010 along with a XSLT to create a clean CSS-able menu.
I have modified the xslt from Cyotec to strip out the first node however I am so far unable to work out how to search within to display only the links depending on the role of the user.
The XSLT is as below:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:map="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" exclude-result-prefixes="map">
<xsl:output method="xml" encoding="utf-8" indent="yes"/>
<xsl:template name="mapNode" match="/">
<ul id="main-menu">
<xsl:apply-templates select="*"/>
</ul>
</xsl:template>
<xsl:template match="/*/*">
<xsl:apply-templates select="*"/>
</xsl:template>
<xsl:template match="map:siteMapNode">
<xsl:if test="/siteMap/SiteMapNode[#roles != 'Admin']">
<li>
<a href="{substring(#url, 2)}" title="{#description}">
<xsl:value-of select="#title"/>
</a>
<xsl:if test="map:siteMapNode">
<xsl:call-template name="mapNode"/>
</xsl:if>
</li>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
The XML looks like this:
<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
<siteMapNode url="~/" title="" description="Anon" roles="*">
<siteMapNode url="~/anon.aspx" title="Anon" description="Anon" roles="*" />
<siteMapNode url="~/admin1.aspx" title="Admin1" description="Admin Only" roles="Admin"/>
<siteMapNode url="~/admin2.aspx" title="Admin2" description="Admin Only" roles="Admin">
<siteMapNode url="~/admin3.aspx" title="Admin3" description="Admin Only" roles="Admin"/>
</siteMapNode>
</siteMapNode>
</siteMap>
I am wanting to only output the urls titles and descriptions where roles != Admin
Everything works fine without the search.
Is anyone able to shed some light on the 'if' function, or suggest a better way of achieving this?
Thanks in advance
The problem with your current xsl:if condition....
<xsl:if test="/siteMap/SiteMapNode[#roles != 'Admin']">
.... is that the first forward slash means it is an absolute path, starting from the root element, so all the xsl:if is saying is whether there is any SiteMapNode, immediately under the siteMap element, that is not an Admin role. This means it will always be true in your case.
You really only want to check the role of the current element
<xsl:if test="#roles != 'Admin'">
However, there is a tidier way of doing this. Remove the xsl:if condition, and just have a separate template to match the admin role elements, and ignore them.
<xsl:template match="map:siteMapNode[#roles='Admin']"/>
Here is the full XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:map="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" exclude-result-prefixes="map">
<xsl:output method="xml" encoding="utf-8" indent="yes"/>
<xsl:template name="mapNode" match="/">
<ul id="main-menu">
<xsl:apply-templates select="*"/>
</ul>
</xsl:template>
<xsl:template match="/*/*">
<xsl:apply-templates select="*"/>
</xsl:template>
<xsl:template match="map:siteMapNode[#roles='Admin']"/>
<xsl:template match="map:siteMapNode">
<li>
<a href="{substring(#url, 2)}" title="{#description}">
<xsl:value-of select="#title"/>
</a>
<xsl:if test="map:siteMapNode">
<xsl:call-template name="mapNode"/>
</xsl:if>
</li>
</xsl:template>
</xsl:stylesheet>
When applied to your sample XML, the following is output
<ul id="main-menu">
<li>
Anon
</li>
</ul>
Do note the template that matches the admin role element is more specific that the template that matches any SiteMapNode element and so the XSLT processor will give priority to this when matching.
Related
I'm working on a DDR Treeview menu for DotNetNuke to display only the selected Root items and its child node to be expanded. Here is what I'm trying to achieve.
(Left vertical menu)
Any advice please?
This is the xslt code and is currently displaying all root items.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:param name="ControlID" />
<xsl:param name="Options" />
<xsl:template match="/*">
<xsl:apply-templates select="root" />
</xsl:template>
<xsl:template match="root">
<xsl:if test="node">
<ul class="treeview filetree" id="{$ControlID}">
<xsl:apply-templates select="node" />
</ul>
<script type="text/javascript">
jQuery(function($) {
$("#<xsl:value-of select="$ControlID" />").treeview(
<xsl:value-of select="$Options" disable-output-escaping="yes" />
);
});
</script>
</xsl:if>
</xsl:template>
<xsl:template match="node">
<li>
<xsl:if test="node and (#depth != 0 or #breadcrumb = 1)">
<xsl:attribute name="class">open</xsl:attribute>
</xsl:if>
<xsl:choose>
<xsl:when test="#enabled = 0">
<xsl:value-of select="#text" />
</xsl:when>
<xsl:otherwise>
<a href="{#url}">
<xsl:choose>
<xsl:when test="#selected=1">
<xsl:attribute name="class">selected breadcrumb</xsl:attribute>
</xsl:when>
<xsl:when test="#breadcrumb=1">
<xsl:attribute name="class">breadcrumb</xsl:attribute>
</xsl:when>
</xsl:choose>
<xsl:value-of select="#text" />
</a>
</xsl:otherwise>
</xsl:choose>
<xsl:if test="node">
<ul style="list-item-style:none">
<xsl:apply-templates select="node" />
</ul>
</xsl:if>
</li>
</xsl:template>
</xsl:stylesheet>
It would help if you supplied an example of the input code you would like to transform.
I assume its basically something like this:
<root>
<node enabled="1" depth="1" text="Service" selected="true" breadcrumb="0"/>
<node>
<node>
<node/>
</node>
</node>
<node>
<node/>
</node>
<node/>
</root>
You can skip the first template-match and those first if-element and directly match only what you're interested in. Without testing, something like this should do the trick:
<!-- ... -->
<!-- process only "root" elements that have at least one "node" element -->
<xsl:template match="/root[node]">
<ul class="treeview filetree" id="{$ControlID}">
<xsl:apply-templates select="node" />
</ul>
<!-- ... -->
</xsl:template>
<xsl:template match="node">
<!-- ... -->
</xsl:template>
Without the source XML it's really hard to work out what you're trying to do here, but I'd say the main reason you're getting all nodes is the the template to match the node element is recursive and does not hide the descendants. If you add display:none to the style attribute on the ul element at the end of the node template (or change list-item-style to display), you may get what you want.
If you're only getting root items, you'll want to change the NodeSelector defined for the menu. I believe that the shorthand value RootChildren will give you what you want.
I am using umbraco cms. here i have a problem in navigation menu for multilingual site. my xslt files code is given below. it only renders menu in english. i am new to umbraco. so can any one please tell me that where i have to change in this code to make it work correct according to different languages. my code is
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet [
<!ENTITY nbsp " ">
]>
<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"
exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets ">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
<xsl:param name="currentPage"/>
<xsl:variable name="rootPage" select="$currentPage/ancestor-or-self::root"/>
<xsl:variable name="siteRoot" select="$currentPage/ancestor-or-self::*[#level = 1]" />
<xsl:variable name="propertyAlias" select="/macro/PropertyAlias"/>
<xsl:template match="/">
<xsl:variable name="homepage" select="$currentPage/ancestor-or-self::Homepage"/>
<xsl:variable name="nodeIds" select="umbraco.library:Split($homepage/*[name()=$propertyAlias],',')" />
<ul class="navigation fc">
<xsl:for-each select="$nodeIds/value">
<xsl:variable name="linkNone" select="$rootPage//*[#isDoc][#id = string(current()/.)]"/>
<xsl:if test="string-length($linkNone/#id)> 0">
<li>
<xsl:attribute name="class">
<xsl:if test="$currentPage/ancestor-or-self::*[#level > 1]/#id = $linkNone/#id">
<xsl:text>selected</xsl:text>
</xsl:if>
<xsl:if test="position() = last()">
<xsl:text> last</xsl:text>
</xsl:if>
</xsl:attribute>
<xsl:choose>
<xsl:when test="string-length($linkNone/umbracoUrlAlias) > 0">
<a href="{$linkNone/umbracoUrlAlias}">
<xsl:value-of select="$linkNone/#nodeName"/>
</a>
<xsl:if test="position() != last()">
<xsl:text> | </xsl:text>
</xsl:if>
</xsl:when>
<xsl:otherwise>
<a href="{umbraco.library:NiceUrl($linkNone/#id)}">
<xsl:value-of select="$linkNone/#nodeName"/>
</a>
<xsl:if test="position() != last()">
<xsl:text> | </xsl:text>
</xsl:if>
</xsl:otherwise>
</xsl:choose>
</li>
</xsl:if>
</xsl:for-each>
</ul>
</xsl:template>
</xsl:stylesheet>
You shouldn't be using the $rootPage ever. For multilingual or multisite you want to stop at the home page above the current content ($siteRoot).
It looks like your navigation is selected by a picker on the home page. Any reason why you don't just let the content tree structure determine the navigation? May be simpler in this case.
If this doesn't help, please post your content tree structure example.
i'm trying to make a report with corporate header but it doesn't work, the report is working fine but i don't have any corporate header, here is my code:
report.xml:
<report id="tracker_Labels"
model="noc.trajet"
name="noc.trajet"
string="Tracker Labels"
xml="module_coll/report/tracker_label.xml"
xsl="module_coll/report/tracker_label.xsl"
header="True"/>
tracker_label.xsl:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:import href="../../base/report/corporate_defaults.xsl" />
<xsl:template match="/">
<xsl:call-template name="rml" />
</xsl:template> <!-- CONTENT -->
</xsl:stylesheet>
Regards,
I'm trying to write some xsl to style an RSS feed. I need to trim the first 10 characters off the title of each item.
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/rss">
<ul>
<xsl:for-each select="channel/item">
<li><strong><xsl:value-of select="title"/>
</strong>
More</li>
</xsl:for-each>
</ul>
</xsl:template>
<xsl:template name="trimtitle">
<xsl:param name="string" select="." />
<xsl:if test="$string">
<xsl:text>Foo</xsl:text>
<xsl:call-template name="trimtitle">
<xsl:with-param name="string" select="substring($string, 10)" />
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template match="title">
<xsl:call-template name="title" />
<xsl:value-of select="." />
</xsl:template>
</xsl:stylesheet>
I think you should write your substring function as this:
substring($string,1, 10)
Look at here
http://www.zvon.org/xxl/XSLTreference/Output/function_substring.html
What are you doing in your trimtitle template?
Why are you calling trimtitle recursive..?
The easiest way to show a trimmed string is with:
<xsl:value-of select="substring(title,0,10)"/>
i have the following xslt sheet:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:variable name="nhits" select="Answer[#nhits]"></xsl:variable>
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
<div>
<xsl:call-template name="resultsnumbertemplate"/>
</div>
</xsl:template>
<xsl:template name="resultsnumbertemplate">
<xsl:value-of select="$nhits"/> matches found
</xsl:template>
</xsl:stylesheet>
And this is the xml that im trying to mix with the previous xslt:
<Answer xmlns="exa:com.exalead.search.v10" context="n%3Dsl-ocu%26q%3Dlavadoras" last="9" estimated="false" nmatches="219" nslices="0" nhits="219" start="0">
<time>
<Time interrupted="false" overall="32348" parse="0" spell="0" exec="1241" synthesis="15302" cats="14061" kwds="14061">
<sliceTimes>15272 </sliceTimes>
</Time>
</time>
</Answer>
Im using a xslcompiledtransform and that's working fine:
XslCompiledTransform transformer = new XslCompiledTransform();
transformer.Load(HttpContext.Current.Server.MapPath("xslt\\" + requestvariables["xslsheet"].ToString()));
transformer.Transform(xmlreader, null, writer);
My problems comes when im trying to put into a variable the "nhits" attribute value placed on the Answer element, but i'm not rendering anything using my xslt sheet.
Do you know what could be the cause?
Could be the xmlns attribute in my xml file?
Thanks in advance.
Best Regards.
Jose
Your immediate problem is that your XPath is wrong. Try
<xsl:variable name="nhits" select="/Answer/#nhits" />
However, I suggest a change to get rid of the variable altogether, you don't need it.
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:output method="html" indent="yes"/>
<xsl:template match="Answer">
<div>
<xsl:value-of select="#nhits"/>
<xsl:text> matches found</xsl:text>
</div>
</xsl:template>
</xsl:stylesheet>
Your variable should be select="Answer/#nhits"
Your currect xpath of "Answer[#nhits]" tries to select Answer element that has an attribute named nhits..