Below is a picture of the documented fn:replace xml string function posted on the web.

yet when I run this TSQL statement:
SELECT T.C.query('if (fn:local-name(.) = "PId") then (data(.)) else (fn:replace(fn:local-name(.), "P", ""))') As X
FROM @XML_R.nodes('/P/*') T(C)
I get the error message:
XQuery [query()]: There is no function '{http://www.w3.org/2004/07/xpath-functions}:replace()'
1.) By taking out the replace function, I see that the fn:local-name function is recognized. Does SQL Server actually look to the URL in the error message to run this function?
2.) Where/How is the namespace in the error message registered in SQL Server? i.e. How does SQL Sever know about this website?
3.) Is it possible to write your own function and register it in SQL Server with a prefix for the namespace so you can call your own function in any .query method as long as you prefix the function?
4.) Why won't SQL Server recognize fn:replace even though it recognizes fn:local-name?
Note: I've tried finding answers to these questions on the web but still don't understand this well enough.
2. There is an article about XML Query here:
https://www.sqlshack.com/filtering-xml-columns-using-xquery-in-sql-server/