openrewrite / rewrite

Automated mass refactoring of source code.
https://docs.openrewrite.org
Apache License 2.0
1.97k stars 292 forks source link

Support for XML Namespaces #3919

Open ammachado opened 5 months ago

ammachado commented 5 months ago

What problem are you trying to solve?

Change namespaced tags without being required to know the namespace prefix. For example, on the following XML, I want to replace the schemaLocation attribute on the beans tag.

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
                           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
</bean>

There are situations where the namespace URI remains the same, but the prefix changes. I can try change the attribute xsi:schemaLocation, but for that the prefix should match.

Describe the solution you'd like

Have you considered any alternatives or workarounds?

No.

Additional context

Are you interested in contributing this feature to OpenRewrite?

timtebeek commented 5 months ago

Seems reasonable, thanks! Might tie into some of the schemaLocation changes we do in rewrite-migrate-java.

ammachado commented 5 months ago

Probably a few of the existing Java EE9/10 recipes could be improved with this, also create new ones, specially new recipes to refactor schemaLocation for Spring XML files.

knutwannheden commented 2 weeks ago

With that PR merged, I think this ticket can now be closed. What fo you guys think, @ammachado and @timtebeek ?

timtebeek commented 2 weeks ago

We had added a disabled tests previously; think we can reevaluate the @Disabled on that tests before we close https://github.com/openrewrite/rewrite/blob/22ac272d0a37fe1023af153ec68aef3ab7926849/rewrite-xml/src/test/java/org/openrewrite/xml/XPathMatcherTest.java#L223-L226

ammachado commented 2 weeks ago

3925 does not consider namespaces on org.openrewrite.xml.SemanticallyEqual.

evie-lau commented 1 week ago

Also need to finish support for name and namespace-uri functions in XPathMatcher. The latter function may potentially require a refactor or rewrite.