diff --git a/pkgs/racket-test/tests/xml/test.rkt b/pkgs/racket-test/tests/xml/test.rkt
index df7cc7f1e2..fb5c2e5783 100644
--- a/pkgs/racket-test/tests/xml/test.rkt
+++ b/pkgs/racket-test/tests/xml/test.rkt
@@ -462,6 +462,12 @@ END
(test-syntax:read-xml/element
"inner"
'(a ([href "#"]) "inner"))
+
+ (test-syntax:read-xml/element
+ "inner"
+ '(a ([c "1"] [a "2"] [b "3"]) "inner"))
+
+ (test-syntax:read-xml/element/exn "inner" "read-xml: lex-error: at position 1.20/21: duplicated attribute name c")
(test-syntax:read-xml/element
" "
diff --git a/racket/collects/xml/private/reader.rkt b/racket/collects/xml/private/reader.rkt
index 7291aa6997..63c7257217 100644
--- a/racket/collects/xml/private/reader.rkt
+++ b/racket/collects/xml/private/reader.rkt
@@ -270,17 +270,16 @@
;; lex-attributes : Input-port (-> Location) -> (listof Attribute)
(define (lex-attributes in pos)
- (sort (let loop ()
- (skip-space in)
- (cond [(name-start? (peek-char-or-special in))
- (cons (lex-attribute in pos) (loop))]
- [else null]))
- (lambda (a b)
- (let ([na (attribute-name a)]
- [nb (attribute-name b)])
- (cond
- [(eq? na nb) (lex-error in pos "duplicated attribute name ~a" na)]
- [else (string (symbol->string na) (symbol->string nb))])))))
+ (let* ([result_list
+ (let loop ()
+ (skip-space in)
+ (cond [(name-start? (peek-char-or-special in))
+ (cons (lex-attribute in pos) (loop))]
+ [else null]))]
+ [check_dup (check-duplicates result_list (lambda (a b) (eq? (attribute-name a) (attribute-name b))))])
+ (if check_dup
+ (lex-error in pos "duplicated attribute name ~a" (attribute-name check_dup))
+ result_list)))
;; lex-attribute : Input-port (-> Location) -> Attribute
(define (lex-attribute in pos)