Copied to clipboard

Flag this post as spam?

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


  • Carl Winblad 1 post 21 karma points
    Dec 03, 2010 @ 10:05
    Carl Winblad
    0

    How to get a html attribute?

    Hi, I am currently trying to get the value of the html lang attribute.

    Currently I have three buttons that change the value with JavaScript, however as the top menu is made in xslt I need it to change depending on what value html lang has. For example right now it says:

    <html lang="sv-se"></html>

    I tried using the following xslt code to output the attribute value:

    <xsl:template match="/">

    <h1>
    <xsl:for-each select="html">
        <xsl:value-of select="@lang"/>
    </xsl:for-each>
    </h1>

    <h1><xsl:value-of select="html/@lang"/></h1>
    <h1><xsl:apply-templates select="html"/></h1>

    </xsl:template>

    <xsl:template match="html">
      <xsl:value-of select="@lang"/>
    </xsl:template>

    The site that I'm working on:

    www.trollstigar.se

    I hope there is an easy solution for my problem

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Jan 01, 2011 @ 09:18
    Lee Kelleher
    0

    Hi Carl,

    The problem here is XML namespaces!  Try adding the following to your XSLT:

    <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:html="http://www.w3.org/1999/xhtml"
        exclude-result-prefixes="msxml umbraco.library html">

    Then, whenever you access an element/tag from the HTML document, you'll need to prefix it with the namespace "html:"...

    <xsl:value-of select="html:html/@lang"/>

    I know it seems like there are too many references to "html" in there ... but you can change the namespace to whatever you prefer (internally in the XSLT).

    Cheers, Lee.

  • Thijs Kuipers 43 posts 66 karma points
    Jan 03, 2011 @ 16:45
    Thijs Kuipers
    0

    I might be mistaken, but how do you set the "lang" property of the HTML element? I don't see how you would access the rendered HTML document from a macro, unless it is specifically loaded as input.

    @Lee Kelleher: how do you see this?

    I personally think you (Carl Winblad) probably either have a site which, at some level, is split up in language nodes. In that case you could look up the chain to see in what language you are.

    Or you're using a language mapped to a hostname, in which case you could determine your current culture using an XSLT extension method (no default method exists in umbraco.library).

Please Sign in or register to post replies

Write your reply to:

Draft