In-depth Analysis of Two Core Functions for Retrieving Element Tag Names in XSLT: name() vs. local-name()

Dec 05, 2025 · Programming · 10 views · 7.8

Keywords: XSLT | XML | XPath | name() function | local-name() function | tag name retrieval

Abstract: This article provides a comprehensive exploration of two core methods for obtaining XML element tag names in XSLT: the name() function and the local-name() function. Through comparative analysis, it explains in detail their semantic differences when handling elements with namespace prefixes, and demonstrates with practical code examples how to correctly use these functions to extract tag names. The article also discusses the shorthand forms of function parameters and their applicable scenarios, offering a thorough technical reference for XSLT developers.

Introduction

In XSLT transformations, it is often necessary to retrieve the tag names of XML elements. While the XSLT standard provides the <xsl:value-of select="expression"/> instruction to extract element values, there is no direct <xsl:tag-of> instruction for obtaining tag names. This article delves into two core XPath functions: name() and local-name(), which are essential tools for addressing this need.

Basic Usage and Code Examples

Consider the following XML data:

<person>
  <name>Robert</name>
  <profession>programmer</profession>
  <hobby>photography</hobby>
</person>

To extract the tag name and value of each child element, the following XSLT code can be used:

<xsl:for-each select="person/*">
  <xsl:value-of select="name(.)"/> : <xsl:value-of select="."/>
</xsl:for-each>

This code will output:

name : Robert
profession : programmer
hobby : photography

The key here is the name(.) function call, which returns the full name of the current context node (i.e., .).

Semantic Differences Between name() and local-name()

Although both name() and local-name() can retrieve element names, they exhibit important differences when handling elements with namespace prefixes:

This distinction is particularly important in XML documents involving multiple namespaces. When elements from different namespaces share the same local name, only the name() function can distinguish between them. For example:

<root xmlns:a="http://example.com/ns1" xmlns:b="http://example.com/ns2">
  <a:element>Value1</a:element>
  <b:element>Value2</b:element>
</root>

Using name() will return a:element and b:element respectively, while local-name() returns element for both.

Shorthand Forms of Function Parameters

Both functions support shorthand forms where parameters can be omitted, which is convenient in practical coding:

Thus, the previous example can be simplified to:

<xsl:for-each select="person/*">
  <xsl:value-of select="name()"/> : <xsl:value-of select="."/>
</xsl:for-each>

Applicable Scenarios and Considerations

The choice between name() and local-name() depends on specific requirements:

  1. Use the name() function when full element identification (including namespace information) is needed. This is particularly important when generating output that must preserve namespace information.
  2. Use the local-name() function when only the local name of the element is of concern, or when namespace differences need to be ignored. This is useful when processing XML data from different sources but with similar structures.
  3. Note that these functions are not limited to element nodes; they can also be applied to attribute nodes, processing instruction nodes, etc. For processing instruction nodes, both functions return the same value.

Conclusion

Through an in-depth analysis of the name() and local-name() functions, we see that XSLT provides flexible and powerful tools for handling XML element tag names. Understanding the subtle semantic differences between these two functions is crucial for writing correct and robust XSLT transformation code. In practical development, the appropriate function should be selected based on specific business needs and XML document structures to ensure consistency and accuracy in data processing.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.