siyuan-note/siyuan · error

svg contains multiple root elements

Error message

svg contains multiple root elements

What it means

SanitizeSVG requires exactly one root element. Once the root <svg> element is closed, any further start element triggers this error — the input contains multiple top-level elements. Well-formed XML allows exactly one root; extra siblings indicate malformed or smuggled content, so the sanitizer refuses it.

Solutions

  1. Provide a single SVG document — wrap multiple icons in one root <svg> with <symbol>/<use> if needed
  2. Remove trailing duplicate <svg> fragments after the root closes
  3. Fix the upstream tooling/clipper that concatenates SVG documents

Example fix

// before
<svg viewBox="0 0 10 10"></svg><svg viewBox="0 0 10 10"></svg>
// after
<svg viewBox="0 0 10 10"><symbol id="a">...</symbol></svg>
Defensive patterns

Strategy: validation

Validate before calling

func singleRoot(svg string) bool {
    roots := 0
    d := xml.NewDecoder(strings.NewReader(svg))
    depth := 0
    for {
        t, err := d.RawToken()
        if err == io.EOF { break }
        if err != nil { break }
        switch t.(type) {
        case xml.StartElement:
            depth++
            if depth == 1 { roots++ }
        case xml.EndElement:
            depth--
        }
    }
    return roots == 1
}

Try / catch

clean, err := util.SanitizeSVG(input)
if err != nil && err.Error() == "svg contains multiple root elements" {
    return fmt.Errorf("paste one SVG document at a time: %w", err)
}

Prevention

When it happens

Trigger: SanitizeSVG gets input like '<svg>...</svg><svg>...</svg>' or '<svg/><!--comment--><svg/>', i.e. concatenated SVG fragments, or content where HTML-style parsing would hide sibling roots.

Common situations: Users paste several copied SVGs at once into a custom-emoji upload, clipper concatenates multiple captures, or tooling appends a second <svg> (e.g. sprite sheets pasted whole).

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19). Data as JSON: /api/errors/ababb5d3bc4dc006. Report an issue: GitHub.

Appendix: source

Thrown at kernel/util/misc.go:373

			break
		}
		if err != nil {
			return "", fmt.Errorf("parse svg failed: %w", err)
		}
		tokenCount++
		if tokenCount > maxSVGTokens {
			return "", fmt.Errorf("svg contains too many tokens")
		}

		switch typed := token.(type) {
		case xml.StartElement:
			elementStack = append(elementStack, typed.Name)
			depth++
			if depth > maxSVGDepth {
				return "", fmt.Errorf("svg nesting depth exceeds %d", maxSVGDepth)
			}
			if rootClosed {
				return "", fmt.Errorf("svg contains multiple root elements")
			}
			if !rootSeen {
				if !strings.EqualFold(typed.Name.Local, "svg") {
					return "", fmt.Errorf("root element is not svg")
				}
				rootSeen = true
			}

			if skipDepth > 0 {
				skipDepth++
				continue
			}
			if _, unsafe := unsafeSVGElements[strings.ToLower(typed.Name.Local)]; unsafe {
				skipDepth = 1
				continue
			}

			typed.Name = preserveXMLName(typed.Name)

View on GitHub (pinned to 9f775e8a12)