XSLT Getting attribute from root node in template

Viewed 39

how can I get a "locale" attribute from urlset tag inside in template, tried many variants but nothing works from it...

<?xml version="1.0" encoding="UTF-8"?>
    <?xml-stylesheet type="text/xsl" href="https://kazik.reved-rfs.ru/sitemap.xsl"?>
    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" locale="hu-hu">
<xsl:template match="sitemap:urlset">
    ...
    <td>
        <xsl:value-of select="urlset/@locale"/>
    </td>
1 Answers

Inside the template for sitemap:urlset if you use an XPath like urlset/@locale it will be looking for a urlset element that is a child of the sitemap:urlset element, and then looking for the @locale on that element.

In order to address the @locale element for the matched sitemap:urlset element, use:

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

You could also use an explicit XPath that "jumps up" to the top of the tree and selects from that element (but since you are already "standing on" that matched element, I'd use the relative XPath above):

<xsl:value-of select="/sitemap:urlset/@locale"/>
Related