Skip to content

XML doctype system IDs can serialize as invalid XML #2571

Closed

Description

@jhy

I bumped into this odd edge case while importing some old technical documentation.

XML system literals in a doctype can use either single or double quotes as their delimiter.

jsoup accepts the valid XML below, but DocumentType currently always serializes the system identifier using double quotes:

String xml = "<!DOCTYPE root SYSTEM 'a\"b.dtd'><root />";
Document doc = Jsoup.parse(xml, "", Parser.xmlParser());

System.out.println(doc.outerHtml());
// <!DOCTYPE root SYSTEM "a"b.dtd"><root />

Escaping the quote as &quot; doesn't work because entity references are not interpreted inside a system literal; it would change the identifier’s value. Converting it to %22 would also change the stored identifier, and the XML external-identifier specification says URI escaping should happen only when necessary and as late as possible during dereferencing.

The fix will be to continue using double quotes normally, but use single quotes when the system identifier contains a double quote.

Activity

  1. added
    bugA confirmed bug, that we should fix
    fixedAn {bug|improvement} that has been {fixed|implemented}
    on Aug 15, 2026
  2. added this to the 1.23.2 milestone on Aug 15, 2026
  3. added a commit that references this issue on Aug 15, 2026
    598a895
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA confirmed bug, that we should fixfixedAn {bug|improvement} that has been {fixed|implemented}

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions