Skip to main content

Change XML attribute of a specific resource version

org.openrewrite.xml.ChangeNamespaceValue

Alters XML Attribute value within specified element of a specific resource versions.

Recipe source​

GitHub: ChangeNamespaceValue.java, Issue Tracker, Code Genome Project

This recipe is available under the Apache License Version 2.0.

Options​

TypeNameDescriptionExample
StringelementNameOptional. The name of the element whose attribute's value is to be changed. Interpreted as an XPath Expression.property
StringoldValueOptional. Only change the property value if it matches the configured oldValue.newfoo.bar.attribute.value.string
StringnewValueThe new value to be used for the namespace.newfoo.bar.attribute.value.string
StringversionMatcherOptional. The version of resource to change1.1
BooleansearchAllNamespacesOptional. Specify whether evaluate all namespaces. Defaults to truetrue
StringnewVersionThe new version of the resource2.0
StringnewSchemaLocationOptional. The new value to be used for the namespace schema location.newfoo.bar.attribute.value.string

Used by​

This recipe is used as part of the following composite recipes:

Example​

Parameters​
ParameterValue
elementNameweb-app
oldValuenull
newValuehttp://java.sun.com/xml/ns/j2ee
versionMatcher2.4
searchAllNamespacesfalse
newVersionnull
newSchemaLocationnull
Before​
<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.4"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
id="WebApp_ID">
<display-name>testWebDDNamespace</display-name>
</web-app>
After​
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" version="2.4"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
id="WebApp_ID">
<display-name>testWebDDNamespace</display-name>
</web-app>

Usage​

This recipe has required configuration parameters. Recipes with required configuration parameters cannot be activated directly (unless you are running them via the Moderne CLI). To activate this recipe you must create a new recipe which fills in the required parameters. In your rewrite.yml create a new recipe with a unique name. For example: com.yourorg.ChangeNamespaceValueExample. Here's how you can define and customize such a recipe within your rewrite.yml:

rewrite.yml
---
type: specs.openrewrite.org/v1beta/recipe
name: com.yourorg.ChangeNamespaceValueExample
displayName: Change XML attribute of a specific resource version example
recipeList:
- org.openrewrite.xml.ChangeNamespaceValue:
elementName: property
oldValue: newfoo.bar.attribute.value.string
newValue: newfoo.bar.attribute.value.string
versionMatcher: 1.1
searchAllNamespaces: true
newVersion: 2.0
newSchemaLocation: newfoo.bar.attribute.value.string

This recipe has no required configuration parameters and comes from a rewrite core library. It can be activated directly without adding any dependencies.

Now that com.yourorg.ChangeNamespaceValueExample has been defined, activate it in your build file:

OpenRewrite artifacts, including the Maven and Gradle plugins themselves, are distributed through the Code Genome Project repository (https://artifacts.codegenomeproject.org/maven), which requires authentication. Sign in to the Code Genome Project to create a download token; your build authenticates with the email or username you signed in with, plus that token as the password. See the quickstart guide for details.

  1. Put your Code Genome Project credentials in ~/.gradle/gradle.properties, so that they are not committed alongside your build:
    ~/.gradle/gradle.properties
    codeGenomeUsername=you@example.com
    codeGenomeToken=your-download-token
  2. Add the Code Genome Project repository to your settings.gradle file, so that the plugin itself can be resolved:
    settings.gradle
    pluginManagement {
    repositories {
    maven {
    url = "https://artifacts.codegenomeproject.org/maven"
    credentials {
    username = providers.gradleProperty("codeGenomeUsername").get()
    password = providers.gradleProperty("codeGenomeToken").get()
    }
    }
    // Keep the portal for any other plugins your build applies
    gradlePluginPortal()
    }
    }
  3. Add the following to your build.gradle file:
    build.gradle
    plugins {
    id("org.openrewrite.rewrite") version("latest.release")
    }

    rewrite {
    activeRecipe("com.yourorg.ChangeNamespaceValueExample")
    setExportDatatables(true)
    }

    repositories {
    mavenCentral()
    maven {
    url = "https://artifacts.codegenomeproject.org/maven"
    credentials {
    username = providers.gradleProperty("codeGenomeUsername").get()
    password = providers.gradleProperty("codeGenomeToken").get()
    }
    }
    }
  4. Run gradle rewriteRun to run the recipe.

See how this recipe works across multiple open-source repositories​

Run this recipe on OSS repos at scale with the Moderne SaaS.

The community edition of the Moderne platform enables you to easily run recipes across thousands of open-source repositories.

Please contact Moderne for more information about safely running the recipes on your own codebase in a private SaaS.

Data Tables​

Source files that had results​

org.openrewrite.table.SourcesFileResults

Source files that were modified by the recipe run.

Column NameDescription
Source path before the runThe source path of the file before the run. null when a source file was created during the run.
Source path after the runA recipe may modify the source path. This is the path after the run. null when a source file was deleted during the run.
Parent of the recipe that made changesIn a hierarchical recipe, the parent of the recipe that made a change. Empty if this is the root of a hierarchy or if the recipe is not hierarchical at all.
Recipe that made changesThe specific recipe that made a change.
Estimated time savingAn estimated effort that a developer to fix manually instead of using this recipe, in unit of seconds.
CycleThe recipe cycle in which the change was made.