troubleshooting Question

fn:replace xml string function not recognized in SQL Server 2012

Avatar of Declan Basile
Declan BasileFlag for United States of America asked on
SQLXMLMicrosoft SQL Server
5 Comments2 Solutions11 ViewsLast Modified:
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 '{}: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.

Senior Developer

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 2 Answers and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros