1
0
mirror of https://github.com/alexandrev/xslt-lab.git synced 2026-09-18 19:43:16 +00:00
Files
xslt-lab/site/content/xslt/functions/xpath-resolve-qname.md
T
alexandrev-tibco 53e90ef86e feat(blog): complete XSLT/XPath reference — 229 function pages
Full coverage of XSLT 1.0, 2.0 and 3.0 elements and XPath functions:
- 59 XSLT elements (xsl:stylesheet → xsl:use-accumulators)
- 170 XPath functions (1.0 node/string/numeric/boolean, 2.0 sequence/
  date/QName/string, 3.0 HOF/map/array/JSON/streaming)
Each page: description, parameters table, return value, 2 runnable
Saxon examples with input XML + stylesheet + output, notes, cross-links.
xsltCompletions.js: all 229 entries now have blogSlug for hover links.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-19 09:28:05 +02:00

3.9 KiB

title, description, date, version, versionLabel, category, syntax, tags
title description date version versionLabel category syntax tags
resolve-QName() Resolves a lexical QName string against the in-scope namespace bindings of a given element node. 2026-04-19T00:00:00Z 2.0 XSLT 2.0 QName function resolve-QName(lexical-qname, element)
xslt
reference
xpath
xslt2

Description

resolve-QName() converts a string containing a lexical QName (such as "xs:integer" or "myns:product") into a typed xs:QName value by looking up the prefix in the namespace bindings of the supplied element node. The resulting xs:QName carries the namespace URI associated with the prefix in that element's scope.

This function is particularly useful when an XML document stores QName-valued content as strings in attributes or text nodes — a common pattern in configuration files, WSDL documents, and schema instances. Without resolve-QName(), resolving the prefix to a URI requires manual namespace node traversal.

If the lexical QName has no prefix, the resulting QName has no namespace (not the default namespace). If the prefix is not declared in scope on the element, a dynamic error is raised.

Parameters

Parameter Type Required Description
lexical-qname xs:string? Yes A string containing the lexical QName to resolve.
element element() Yes The element node whose in-scope namespace bindings are used for resolution.

Return value

xs:QName? — the resolved QName, or the empty sequence if lexical-qname is the empty sequence.

Examples

Resolving QName content from an attribute

Input XML:

<?xml version="1.0" encoding="UTF-8"?>
<types xmlns:xs="http://www.w3.org/2001/XMLSchema"
       xmlns:app="http://example.com/types">
  <type ref="xs:integer"/>
  <type ref="app:ProductCode"/>
</types>

Stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/types">
    <resolved>
      <xsl:for-each select="type">
        <xsl:variable name="q" select="resolve-QName(@ref, .)"/>
        <type local="{local-name-from-QName($q)}"
              uri="{namespace-uri-from-QName($q)}"/>
      </xsl:for-each>
    </resolved>
  </xsl:template>
</xsl:stylesheet>

Output:

<resolved>
  <type local="integer" uri="http://www.w3.org/2001/XMLSchema"/>
  <type local="ProductCode" uri="http://example.com/types"/>
</resolved>

Comparing resolved QNames

Stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xsl:output method="text"/>

  <xsl:template match="/types">
    <xsl:variable name="first" select="resolve-QName(type[1]/@ref, type[1])"/>
    <xsl:variable name="xsInteger" select="QName('http://www.w3.org/2001/XMLSchema', 'integer')"/>
    <xsl:value-of select="if ($first eq $xsInteger) then 'Matches xs:integer' else 'Other type'"/>
  </xsl:template>
</xsl:stylesheet>

Output:

Matches xs:integer

Notes

  • The element node provides the namespace context; the QName is resolved in the namespace scope of that specific element, including inherited namespace declarations.
  • A prefix that is not in scope on the supplied element raises FOCA0002 (invalid value for cast or constructor).
  • To resolve a QName using the stylesheet's own namespace bindings rather than those in the source document, construct a QName with QName() directly.
  • resolve-QName() is the inverse of serializing a QName value to a prefixed string.

See also