XSLT 从模板中的根节点获取属性

XSLT Getting attribute from root node in template

我如何从模板中的 urlset 标签中获取“区域设置”属性,尝试了很多变体但没有任何效果...

<?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>

sitemap:urlset 的模板中,如果您使用像 urlset/@locale 这样的 XPath,它将寻找 urlset 元素,该元素是 sitemap:urlset 元素的子元素,然后在该元素上寻找 @locale

为了找到匹配的 sitemap:urlset 元素的 @locale 元素,使用:

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

您还可以使用显式 XPath,它“跳到”树的顶部并从该元素中进行选择(但是由于您已经“站在”那个匹配的元素上,所以我会使用上面的相对 XPath ):

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