edit-xml
The Declarative Hook edit-xml
defines changes made to existing XML files. Below is an example of defining a Declarative Hook of type edit-xml
:
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
Available Actions
path:
Defines the path to 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 tell you when file editing should occur.
before-input
:
Executes the Declarative Hook before receiving the input parameters from the user.
trigger: before-input
before-render
:
Run the Declarative Hook before the Template generates files in the project.
trigger: before-render
after-render
:
Run 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 such as [utf-8, ascii, windows-1252
], if not specified, the file's original default will be kept. Its use is Optional.
encoding: UTF-8
indent:
Sets the character(s) for the indentation of the generated document, 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. And its block has the parameter xpath
as mandatory.
During the lookup process in the changes
attribute, all nodes that match the criteria (xpath
attribute) are changed at once. You can make several changes in a file just by using expressions.
Check out some accepted expressions:
Click here to expand!
Expression | Description |
---|---|
Dot (. ) | Define the object of the current element. |
/ | Define the object of the root element. |
() | Define the grouping in XPath. |
[] | Apply the filter to the script's expression. |
/ | This is a child operator which was used in expressions. |
.. | This is the parent operator used in expressions. |
// | This is a recursive descent. |
* | It is used in object elements regardless of using their names. It is a wildcard in expressions. |
@ | It is used to access the attribute. |
[] | It is the operator of subscript Xpath that use this operator to iterate a collection of element predicates. |
\| | It is a union operator in the results of XPath which is a combination of sets of nodes. |
To know all available expressions, access the Xpath. documentation
changes:
- xpath: /people/person[last()]
...
xpath:
Allows you to enter the XML search language. Its use is Mandatory..
- xpath: //ns:project.build.sourceEncoding
It is possible to 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}}>
The following attributes are optional, you must implement at least one option, in case of multiple attributes, they will be applied in the following order:
clear:
(Opcional)
Removes all contents of the node given the xpath
context.
- xpath: //ns:dependency
clear: true
remove-attributes:
(Opcional)
Removes the attributes from a node. Available fields are one or more name
fields..
- xpath: //ns:dependency
remove-attributes:
- name: css
- name: style
- name: data
append:
(Opcional)
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 XML.
append:
snippet: snippets/fragment.xml
prepend:
(Opcional)
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 XML.
append:
snippet: snippets/fragment.xml
next:
(Opcional)
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 XML.
append:
snippet: snippets/fragment.xml
previous:
(Opcional)
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 XML.
append:
snippet: snippets/fragment.xml
text:
(Opcional)
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 XML.
append:
snippet: snippets/description.txt
attributes:
(Opcional)
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 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 whether the insertion will take effect. 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 so, the actions clear
, remove-attributes
, append
, prepend
, next
, previous
, text
and attributes
will not take effect.
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']
Read Also
Was this page helpful?