Skip to content

XML Module Reference

The xml module provides functions for querying XML content using XPath expressions.

Run an XPath expression on XML content:

result = xml.xpath(
content = "<root><item>value</item></root>",
expression = "/root/item/text()",
type = "STRING",
)
ParameterTypeDefaultDescription
contentstringrequiredThe XML content to query
expressionstringrequiredXPath expression to evaluate
typestringrequiredReturn type: STRING, BOOLEAN, NUMBER
TypeDescriptionExample Result
STRINGText content or string value"example"
BOOLEANBoolean result of expressionTrue or False
NUMBERNumeric value42.0

Get the text content of an element:

xml_content = """
<project>
<artifactId>my-project</artifactId>
<version>1.2.3</version>
</project>
"""
version = xml.xpath(
content = xml_content,
expression = "/project/version/text()",
type = "STRING",
)
# version = "1.2.3"

Check if an element exists:

has_snapshot = xml.xpath(
content = pom_content,
expression = "boolean(/project/version[contains(text(), 'SNAPSHOT')])",
type = "BOOLEAN",
)
# has_snapshot = True or False

Count matching elements:

dep_count = xml.xpath(
content = pom_content,
expression = "count(/project/dependencies/dependency)",
type = "NUMBER",
)
# dep_count = 5.0

Get an attribute value:

xml_content = """
<root>
<item id="123" name="example"/>
</root>
"""
item_id = xml.xpath(
content = xml_content,
expression = "/root/item/@id",
type = "STRING",
)
# item_id = "123"

Extract Maven project coordinates:

def _read_maven_coords(ctx):
pom = ctx.read_path(ctx.path("pom.xml"))
group_id = xml.xpath(
content = pom,
expression = "/project/groupId/text()",
type = "STRING",
)
artifact_id = xml.xpath(
content = pom,
expression = "/project/artifactId/text()",
type = "STRING",
)
version = xml.xpath(
content = pom,
expression = "/project/version/text()",
type = "STRING",
)
ctx.console.info(group_id + ":" + artifact_id + ":" + version)
return ctx.success()
core.dynamic_transform(impl = _read_maven_coords)

Verify required elements exist:

def _validate_manifest(ctx):
manifest = ctx.read_path(ctx.path("AndroidManifest.xml"))
has_internet = xml.xpath(
content = manifest,
expression = "boolean(//uses-permission[@android:name='android.permission.INTERNET'])",
type = "BOOLEAN",
)
if not has_internet:
return ctx.error("Missing INTERNET permission in manifest")
return ctx.success()

Apply transformations based on XML content:

def _update_if_snapshot(ctx):
pom = ctx.read_path(ctx.path("pom.xml"))
is_snapshot = xml.xpath(
content = pom,
expression = "contains(/project/version/text(), 'SNAPSHOT')",
type = "BOOLEAN",
)
if is_snapshot:
ctx.console.warn("Syncing SNAPSHOT version - this may be unstable")
return ctx.success()

Common XPath expressions:

ExpressionDescription
/root/childSelect child of root
//elementSelect element anywhere in document
/root/item/text()Get text content of item
/root/item/@attrGet attribute value
count(//item)Count matching elements
boolean(//item)Check if element exists
//item[1]First matching item
//item[@id='123']Item with specific attribute value
contains(text(), 'value')Text contains substring