Skip to main content

edit-xml

In this section, you will find a reference to a Declarative Hook of the edit-xml type.


A Declarative Hook of the edit-xml type is used to define changes made to existing XML files. Check the following example of a Declarative Hook definition of the edit-xml type:

schema-version: v4
kind: plugin
metadata:
name: sqs-consumer-plugin
display-name: sqs-consumer-plugin
description: Plugin configuring Spring Cloud AWS for SQS consumption.
version: 0.0.1
spec:
type: app
compatibility:
- kotlin
- spring
docs:
pt-br: docs/pt-br/docs.md
en-us: docs/en-us/docs.md
technologies: # Ref: https://docs.stackspot.com/create-use/create-content/yaml-files/plugin-yaml/#technologies
- AWS SQS
- Kotlin
- Maven
- Springboot
inputs:
- label: Select the connection for your sqs-conn
name: sqs-conn
type: required-connection
connection-interface-type: aws-sqs-conn
- label: input tag
name: input_tag
type: text
- label: input body
name: input_body
type: text
// highlight-start
hooks:
- type: edit-xml
trigger: after-render
path: pom.xml
encoding: UTF-8
indent: "\t"
changes:
- xpath: .//dependency
append:
value: |
<config>
<vsync>1</vsync>
</config>
prepend:
value: <{{input_tag}}>{{input_body}}</{{input_tag}}>

- xpath: .//modelVersion
text:
value: 10.0.1

- xpath: .//description
text:
snippet: snippets/description.txt

- xpath: .//dependencies
remove-attributes:
- name: css

- xpath: .//project.build.sourceEncoding
attributes:
- name: btn-name
value: Build UTF-8

- xpath: .//comments
clear: true

- xpath: ./dependencies/dependency/artifactId[.='spring-boot-starter-web']/..
remove: true
// highlight-end

Available Actions

path:

Defines the path of the file that will be edited. The path can be composed of Jinja expressions.

path: "./some/dir_xml/{{file_name}}"

trigger:

Field to set triggers that define when file editing should occur.

before-input:
Executes the Declarative Hook before receiving the input parameters from the user.

trigger: before-input

before-render:
Executes the Declarative Hook before the Template generates files in the project.

trigger: before-render

after-render:
Executes the Declarative Hook after the Template generates files in the project.

trigger: after-render

encoding:

Defines the encoding type that will be saved in the file and accepts common XML formats such as utf-8, ascii or windows-1252. If not specified, the file's original default will be kept. Its use is optional.

encoding: UTF-8

indent:

Defines the character(s) for the indentation of the generated document. The default value is \t. Its use is optional.

indent: "\t"

changes:

It is a list of editing actions that are executed in the order they appear. Each action block requires the xpath parameter.

info

During the search process on the changes attribute, all nodes that match the criteria (the xpath attribute) are changed at once. You can make multiple changes to a file just by using expressions.

Check out some of the supported expressions:

See detailed expressions
ExpressionDescription
Dot (.)Defines the current element.
/Defines the root element.
()Defines grouping in XPath.
[]Applies a filter to the expression.
/Child operator used in expressions.
..Parent operator used in expressions.
//Represents a recursive descent in the node tree.
*Used on object elements regardless of their names.
@Used to access an attribute.
[]XPath subscript operator used to iterate over element collections.
|Union operator on XPath results, combining sets of nodes.

For all the available expressions, check the XPath documentation.

changes:
- xpath: /people/person[last()]
...

xpath:

Allows you to define the XML query expression. Its use is mandatory.

- xpath: //ns:project.build.sourceEncoding

You can interpolate the values of the xpath field with Jinja:

hooks:
- type: edit-xml
trigger: after-render
path: pom.xml
encoding: UTF-8
indent: "\t"
changes:
- xpath: ".//{{INTERPOLATE-INPUT-HERE}}"
append:
value: |
<config>
<vsync>1</vsync>
</config>
prepend:
value: <{{input_tag}}>{{input_body}}</{{input_tag}}>
caution

The following attributes are optional. At least one option must be implemented. In case of multiple attributes, they will be applied in the following order:

clear: (Optional)

Removes all contents of the node given the xpath context.

  - xpath: //ns:dependency
clear: true

remove-attributes: (Optional)

Removes the attributes of a node. The available fields are one or more attributes with the name field.

  - xpath: //ns:dependency
remove-attributes:
- name: css
- name: style
- name: data

append: (Optional)

Adds XML nodes within the context of the xpath result at the end of the list. Available fields are value or snippet.

value
Receives the value to be added to the XML.

append:
value: |
<config>
<vsync>1</vsync>
</config>

snippet
Receives the snippet to be added to the XML.

append:
snippet: snippets/fragment.xml

prepend: (Optional)

Adds XML nodes within the context of the xpath result at the beginning of the list. Available fields are value or snippet.

value
Receives the value to be added to the XML.

prepend:
value: <{{input_tag}}>{{input_body}}</{{input_tag}}>

snippet
Receives the snippet to be added to the XML.

prepend:
snippet: snippets/fragment.xml

next: (Optional)

Adds XML nodes at the same context level as the result of the xpath at the end of the list. Available fields are value or snippet.

value
Receives the value to be added to the XML.

next:
value: |
<dependency>
<groupId>com.stackspot.lib</groupId>
<artifactId>some-lib</artifactId>
<version>1.0.0</version>
</dependency>

snippet
Receives the snippet to be added to the XML.

next:
snippet: snippets/fragment.xml

previous: (Optional)

Adds XML nodes at the same context level as the result of the xpath at the beginning of the list. Available fields are value or snippet.

value
Receives the value to be added to the XML.

previous:
value: |
<dependency>
<groupId>com.stackspot.lib</groupId>
<artifactId>some-lib</artifactId>
<version>1.0.0</version>
</dependency>

snippet
Receives the snippet to be added to the XML.

previous:
snippet: snippets/fragment.xml

text: (Optional)

Adds text within the context of the xpath result. Available fields are value or snippet.

value
Receives the value to be added to the XML.

text:
value: version-10.0.1

snippet
Receives the snippet to be added to the XML.

text:
snippet: snippets/description.txt

attributes: (Optional)

Adds attributes and values to a node. Available fields are name and value.

name
Receives the name of the attribute to be added to the XML.

value
Receives the value of the attribute to be added to the XML.

- xpath: //ns:button
attributes:
- name: btn-name
value: Build UTF-8
- name: style
value: "color: #007"

remove:

Option to remove a node from the XML file. Use the true value to remove the node from the XML file.

hooks:
- type: edit-xml
trigger: after-render
path: pom-remove.xml
changes:
- xpath: ./dependencies/dependency/artifactId[.='spring-boot-starter-web']/..
remove: true

when:

Conditional action that evaluates if the insertion will take place. The available option is not-exists, which receives the text or file to be evaluated by the condition.

not-exists:
Checks if the values entered in the value field exist in the file. If they do, the clear, remove-attributes, append, prepend, next, previous, text and attributes actions will not be performed.

changes:
- xpath: ./dependencies
append:
value: |
<dependency>
<groupId>com.stackspot</groupId>
<artifactId>test</artifactId>
<version>1.0.0</version>
</dependency>
when:
not-exists: ./dependencies/dependency/groupId[.='com.stackspot']/../artifactId[.='test']