Skip to main content

edit-xml

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


A Declarative Hook of the type edit-xml defines changes made to existing XML files. Check below an example of a Declarative Hook definition of the type edit-xml:

schema-version: v3
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 inform the moment when file editing should occur.

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

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

enconding:

Defines the encoding type that will be saved in the file, and accepts common XML formats like [utf-8, ascii, 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, default value is ``t`. Its usage is Optional.

indent: "\t"

changes:

It's a list of editing actions that are executed in the order they appear. And its block has the parameter xpath as mandatory.

info

During the search process on the changes attribute, all nodes that match the criteria (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:

Click here to expand!
ExpressionDescription
Ponto (.)Defines the object of the current element.
/Defines the object of the root element.
()Defines grouping on xpath.
[]Applies filter to script expression.
/This is a child operator that has been used in expressions.
..This is a parent operator that has been used in expressions.
//This is a recursive descent.
*Is used on object elements regardless of whether you use their names.
@Is used to access the attribute.
[]Is the Xpath subscript operator which uses this operator to iterate over a collection of element predicates.
|Is a union operator on xpath results which is a combination of sets of nodes.

For all the available expressions, check the Xpath documentation.

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

xpath:

Allows to insert the XML search language. It's 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. Available fields are one or more name fields.

  - 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 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 XML.

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

snippet
Receives the snippet to be added to the XML.

append:
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 XML.

append:
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.

append:
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 XML.

append:
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.

append:
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 XML.

append:
value: version-10.0.1

snippet
Receives the snippet to be added to the XML.

append:
snippet: snippets/description.txt

attributes:(Optional)

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

name
Receives the value to be added to XML.

value
Receives the snippet 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 it exists, the clear, remove-attributes, append, prepend, next, previous, text and attributes actions will not be performed.

hooks:
- 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']