• - to change the n-th element's value where n is not known before hand (i.e. avoid putting some constant like [1] in the xpath),

    you should use sql:variable function like this:

    declare @xml xml='<root><element>1</element><element>2</element></root>'

    declare @n int=2

    SET @xml.modify('replace value of (//root/element[position()=sql:variable("@n")]/text())[1] with "12"')

    select @xml