XPath uses path expressions to select nodes or node-sets in an XML document. The node is selected by following a path or steps.
XPath Selecting Nodes
| Expression | Description |
| nodename | Selects all nodes with the name “nodename“ |
| / | Selects from the root node |
| // | Selects nodes in the document from the current node that match the selection no matter where they are. |
| . | Selects the current node |
| .. | Selects the parent of the current node |
| @ | Selects attributes |
| * | Matches any element node |
| @* | Matches any attribute node |
XPath Functions
| Function | Description |
| last() | Selects the last element that is the child of the parent element. |
| position() | The position function returns a number equal to the context position from the expression evaluation context. |
| node() | Matches any node of any kind |
| string-length( string or fieldname ) | The string-length function returns a number equal to the number of characters in a given string. |
| contains(haystack, needle) | The contains function determines whether the first argument string contains the second argument string and returns boolean true or false. |
| not( expression ) | The not function evaluates a boolean expression and returns the opposite value. |
| starts-with(haystack, needle) | The starts-with checks whether the first string starts with the second string and returns true or false. |
| substring(string, start) substring(string, start, length) | The substring function returns a part of a given string. |
| translate(string, abc, XYZ) | The translate function evaluates a string and a set of characters to translate and returns the translated string. |
XPath Operators
| Operator | Description |
| | | Computes two node-sets |
| + | Addition |
| – | Subtraction |
| * | Multiplication |
| div | Division |
| = | Equal |
| != | Not Equal |
| < | Less Than |
| <= | Less Than or Equal To |
| > | Greater Than |
| >= | Greater Than or Equal To |
| or | Logical OR |
| and | Logical AND |
| mod | Modulus ( division remainder ) |
Regular Expression Basics
| Operator | Description |
| (?i) | Case Insensitive |
| ^ | Start of string |
| $ | End of string |
Examples
All XPath queries in orchestrator start the same as you can see below
letstrXPathQuery:string=`xpath:`;TypeScriptEquals
letstrXPathQuery:string=`xpath:name='${strName}'`;TypeScriptMatches
letstrRegExp:string=`^${strName}$`;
letstrXPathQuery:string=`xpath:matches(name,${strRegExp})`;TypeScriptMatches Case Insensitive
letstrRegExp:string=`(?i)^${strName}$`;
letstrXPathQuery:string=`xpath:matches(name,${strRegExp})`;TypeScriptContains
letstrXPathQuery:string`xpath:contains(name,'${strName}')`;TypeScriptNot
letstrXPathQuery:string=`xpath:not('${strName}'='test')`;TypeScriptStarts-With
letstrXPathQuery:string=`xpath:starts-with(name,'${strName}')`;TypeScriptSubstring
letstrXPathQuery:string=`xpath:substring(name,${intStartPosition},${intLength})`;TypeScriptTranslate to Upper Case
letstrLowerCase:string="abcdefghijklmnopqrstuvwxyz";
letstrUpperCase:string="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
letstrXPathQuery:string=`xpath:translate(name,${strLowerCase},${strUpperCase})='${strName.toUpperCase()}'`;TypeScriptTranslate to Lower Case
letstrLowerCase:string="abcdefghijklmnopqrstuvwxyz";
letstrUpperCase:string="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
letstrXPathQuery:string=`xpath:translate(name,${strUpperCase},${strLowerCase})='${strName.toLowerCase()}'`;TypeScriptVMware Specific Use Case Examples
VcSdkConnection ID
letstrXPathQuery:string=`xpath:sdkConnection/id='${objVcSdkConnection.id}'`;TypeScriptVcSdkConnection ID and Name Equals
letstrXPathQuery:string=`xpath:sdkConnection/id='${objVcSdkConnection.id}'`;
strXPathQuery=strXPathQuery+` and name='${strName}'`;TypeScriptVcSdkConnection ID and Name Contains
letstrXPathQuery:string=`xpath:sdkConnection/id='${objVcSdkConnection.id}'`;
strXPathQuery=strXPathQuery+` and contains(name, '${strName}')`;TypeScriptName Starts With
letstrXPathQuery:string=`xpath:name[starts-with(.,'${strNamePrefix}')]`;TypeScriptTemplate Equals and IsTemplate Equals and Name Starts With
letstrXPathQuery:string=`xpath:summary/config/template='true'`;
strXPathQuery=strXPathQuery+` and isTemplate='true'`;
strXPathQuery=strXPathQuery+` and name[starts-with(.,'${strTempalateNamePrefix}')]`;TypeScriptTemplate Equals and IsTemplate Equals and Name Equals and Not
letstrXPathQuery:string=`xpath:summary/config/template='true'`;
strXPathQuery=strXPathQuery+` and isTemplate='true'`;
strXPathQuery=strXPathQuery+` and name='${strName}'`;
strXPathQuery=strXPathQuery+` and not(summary/config/managedBy)``;TypeScriptProperty Matches Value
letstrXPathQuery:string=`xpath:matches(${strProperty}, '${strValue}')`;TypeScriptDiscover more from Cloud Build Tools
Subscribe to get the latest posts sent to your email.
