All functions with XML processing support are working with the xPath. We are using xPath as element selector.

 

Available functions

Function

Description

xpathElementAttribute(String content, String xpathExpression, String attributeId)
Returns the value of the attribute specified by attributeId of the element selected by xpathExpression.
xpathElementAttribute(String content, String xpathExpression, String attributeId, String encoding)
Returns the value of the attribute specified by attributeId of the element selected by xpathExpression. Content is in specified encoding.
xpathElementContent(String content, String xpathExpression)
Returns the content of the element selected by xpathExpression.
xpathElementContent(String content, String xpathExpression, String encoding)
Returns the content of the element selected by xpathExpression. Content is in specified encoding.

Examples

Consider following XML content:

<?xml version="1.0" encoding="UTF-8"?>
<Employees>
    <Employee id="3" code="EMPL3">
        <age>40</age>
        <name>Tom</name>
        <gender>Male</gender>
        <role>Manager</role>
    </Employee>
    <Employee id="4" code="EMPL4">
        <age>25</age>
        <name>Meghna</name>
        <gender>Female</gender>
        <role>Manager</role>
    </Employee>
</Employees>

Results:

//get value of attribute of element selected by other attribute
xpathElementAttribute(content, "/Employees/Employee[@id=3]", "code") //returns EMPL3
xpathElementAttribute(content, "/Employees/Employee[@id=4]", "code", "utf-8") //returns EMPL4
 
//get text content of element selected by parent's attribute
xpathElementContent(content, "/Employees/Employee/age[ancestor::Employee[@id=4]]", "utf-8") //returns 25
 
//get text content of first employee element
xpathElementContent(content, "/Employees/Employee/age") //returns 40

Next steps

  • No labels