XSLT Error Reference

XPTY0004

XSLT 2.0+ XPath type error
Error message
A sequence of more than one item is not allowed as the first argument of fn:string() / Cannot compare xs:string to xs:integer

A type error: an expression returned a sequence or type that does not match what the function, operator or comparison expects. The strict type rules of XPath 2.0+ are usually behind it.

What it means

XPath 2.0 and 3.0 are strongly typed. XPTY0004 fires when a value’s type or cardinality (how many items) does not match what the context requires — situations XSLT 1.0 silently tolerated by taking the first node or coercing values.

Common causes

  1. A path selects multiple nodes where one is expected. string(//item) fails if there are two <item> elements. XSLT 1.0 silently used the first one; 2.0+ refuses.
  2. Comparing different atomic types. @id = 42 fails when @id is untyped in some contexts, or '5' > 3 — a string cannot be compared with a number.
  3. Passing a node where an atomic value is required (or vice versa) to a typed xsl:param/xsl:function.

How to fix it

  • Select exactly one item: string(//item[1]), or process all of them with for-each/string-join():
<!-- Before (XPTY0004 if several items) -->
<xsl:value-of select="string(//item)"/>

<!-- After -->
<xsl:value-of select="string-join(//item, ', ')"/>
  • Make comparisons type-consistent: number(@qty) > 3, or cast explicitly with xs:integer(@qty).
  • When migrating 1.0 stylesheets, run them under 2.0 in XSLT Playground — the exact line number in the error tells you which expression needs attention.