Pretty simple: I'm trying to add the nodeName of the current page as a CSS class to that page's opening HTML tag. I would like to convert any spaces in the nodeName to underscores. I tried adding an XSLT file to the page template that selects the nodeName attribute on $currentPage, does a XSLT translate, and outputs that value, but I haven't been able to get it to work.
That code gives me the error System.Xml.Xsl.XslTransformException: Expression must evaluate to a node-set.
Does anyone know the proper way to do this, or what might be wrong with my code? I suspect my $pageName select is wrong, but I don't know why. Any help would be appreciated.
You could also just use @urlName instead of @nodeName, which will give you the friendly URL name which should already have spaces replaced with '-' by default, ie 'installing-modules'
Also keep in mind that this might not be the best route, since the name of a page can change, then your CSS might not apply. Not sure what exactly you're using this for though - maybe it makes sense to do it by template instead?
Oh, sorry. I just noticed you are declaring the currentPage variable yourself. You don't need to do that, there should already be a line like this at the top of your XSLT file that gets populated automatically. Try removing your variable and making sure this is at the top of your file...
I hadn't thought about the fact that the page name could easily change. What I'm trying to do is carry over a convention from when I hand-code, where I use the page name as a unique identifier for that page in CSS. You're right, perhaps it's better to go by template. I notice there's no 'templateName' attribute on a page, though. Is there a way I could get the current page's template *name* instead of the template ID number? If not, I guess I can just use the number.
Not sure if you are using uComponents, but if you are, they have an XSLT Extension method for getting the template alias from it's ID. This is what I've used in a past project:
Add currentPage's 'nodeName' as CSS class?
Pretty simple: I'm trying to add the nodeName of the current page as a CSS class to that page's opening HTML tag. I would like to convert any spaces in the nodeName to underscores. I tried adding an XSLT file to the page template that selects the nodeName attribute on $currentPage, does a XSLT translate, and outputs that value, but I haven't been able to get it to work.
<xsl:template match="/">
<xsl:variable name="currentPage"/>
<xsl:variable name="pageName" select="$currentPage/@nodeName"/>
<xsl:variable name="newtext" select="translate($pageName,' ','_')"/>
<xsl:value-of select="$newtext"/>
</xsl:template>
That code gives me the error System.Xml.Xsl.XslTransformException: Expression must evaluate to a node-set.
Does anyone know the proper way to do this, or what might be wrong with my code? I suspect my $pageName select is wrong, but I don't know why. Any help would be appreciated.
Hi David,
Try using umbraco.library:Replace instead of translate, ie:
You could also just use @urlName instead of @nodeName, which will give you the friendly URL name which should already have spaces replaced with '-' by default, ie 'installing-modules'
Also keep in mind that this might not be the best route, since the name of a page can change, then your CSS might not apply. Not sure what exactly you're using this for though - maybe it makes sense to do it by template instead?
HTH,
Tom
Tom,
Your latest comment got everything working as I was trying to do it, thanks!
Oh, sorry. I just noticed you are declaring the currentPage variable yourself. You don't need to do that, there should already be a line like this at the top of your XSLT file that gets populated automatically. Try removing your variable and making sure this is at the top of your file...
-Tom
Tom,
I hadn't thought about the fact that the page name could easily change. What I'm trying to do is carry over a convention from when I hand-code, where I use the page name as a unique identifier for that page in CSS. You're right, perhaps it's better to go by template. I notice there's no 'templateName' attribute on a page, though. Is there a way I could get the current page's template *name* instead of the template ID number? If not, I guess I can just use the number.
Not sure if you are using uComponents, but if you are, they have an XSLT Extension method for getting the template alias from it's ID. This is what I've used in a past project:
and something like this in the template:
Note for the XSLT extension to work you need to add it to your /config/xsltExtensions.config file:
And also add it to your namespace declarations at the top of your XSLT
-Tom
Ok, I'll look into that. Thanks for all your help.
is working on a reply...