Main Content

evaluate

Class: matlab.io.xml.xpath.Evaluator
Namespace: matlab.io.xml.xpath

Evaluate XPath expression

Since R2021a

Syntax

result = evaluate(evaluatorObj,xpExpr,xmlFilePath)
result = evaluate(evaluatorObj,xpExpr,xmlFilePath,resType)
result = evaluate(evaluatorObj,xpExpr,xmlFilePath,allowDoctype)
result = evaluate(evaluatorObj,xpExpr,xmlFilePath,resType,allowDoctype)
result = evaluate(evaluatorObj,xpExpr,ctxNode)
result = evaluate(evaluatorObj,xpExpr,ctxNode,resType)

Description

result = evaluate(evaluatorObj,xpExpr,xmlFilePath) evaluates the specified XPath expression in the context of the specified XML file and returns an object whose type is determined by the XPath expression.

result = evaluate(evaluatorObj,xpExpr,xmlFilePath,resType) returns the specified result type.

result = evaluate(evaluatorObj,xpExpr,xmlFilePath,allowDoctype) specifies whether the XML file will be parsed if it contains a document type definition (DTD).

result = evaluate(evaluatorObj,xpExpr,xmlFilePath,resType,allowDoctype) returns the specified result type and specifies whether the XML file will be parsed if it contains a document type definition (DTD).

result = evaluate(evaluatorObj,xpExpr,ctxNode) evaluates the specified XPath expression in the context of the parsed document node specified by ctxNode and returns an object whose type is determined by the XPath expression.

result = evaluate(evaluatorObj,xpExpr,ctxNode,resType) evaluates the specified XPath expression in the context of the parsed document node specified by ctxNode and returns the specified result type.

Input Arguments

expand all

XPath expression evaluator, specified as a matlab.io.xml.xpath.Evaluator object.

XPath 1.0 expression, specified as a character vector, string scalar, or matlab.io.xml.xpath.CompiledExpression object.

Path of XML file, specified as a character vector or string scalar.

Evaluation context node, specified as a node object, such as a matlab.io.xml.dom.Element object. If ctxNode is the modified result of a previous expression evaluation, evaluate throws an error.

Evaluation result type, specified as a matlab.io.xml.xpath.EvalResultType object.

Document type definition (DTD) permissions, specified as true or false, determines whether the XML file can contain a DTD. If the specified file contains a DTD, a value of false will result in an error. Enable this option only for trusted sources.

Output Arguments

expand all

Evaluation result, returned as a string scalar or as the specified result type. If the resType argument is not provided, the result is returned as a string scalar. If the resType is provided, the result is returned according to the table.

Result TypeOutput Type

EvalResultType.Boolean

logical
EvalResultType.Numberdouble
EvalResultType.NodeNode object, such as a matlab.io.xml.xpath.Element object
EvalResultType.NodeSetVector of node objects

EvalResultType.String

string scalar

Examples

expand all

This example evaluates an XPath expression that finds the nodes with the name Instrument in the file music.xml.

The file music.xml contains this XML markup:

<MusicalEnsemble>
	<Ensemble>
		<Music>Jazz</Music>
            <BandName>Kool Katz</BandName>
		<Instrumentation>
			<Instrument type="wind">Trumpet
            </Instrument>
			<Instrument type="percussion">Piano
                <pianotype>concert grand</pianotype>
            </Instrument>
			<Instrument type="percussion">Drums
                <drumkit>Bass drum</drumkit>
                <drumkit>Floor tom</drumkit>
                <drumkit>Snare drum</drumkit>
                <drumkit>Hi-hat</drumkit>
                <drumkit>Ride cymbal</drumkit>
            </Instrument>
			<Instrument type="string">Bass
                <basstype>upright</basstype>
            </Instrument>
		</Instrumentation>
	</Ensemble>
    <Musicians>
        <Name role="trumpeter">Miles</Name>
        <Name role="vocalist">Roger</Name>
        <Name role="pianist">Diana</Name>
        <Name role="drummer">George</Name>
        <Name role="bassist">John</Name>
    </Musicians>
</MusicalEnsemble>

Define the XPath expression and specify the XML file.

import matlab.io.xml.xpath.*

xpExpr = "//Instrument";
xmlFilePath = "music.xml";

Evaluate the XPath expression and return the nodes as a vector of node objects.

data = evaluate(Evaluator,xpExpr,xmlFilePath,EvalResultType.NodeSet)
data=1×4 Element array with properties:
    TagName
    HasAttributes
    TextContent
    Children

This example parses an XML file and then evaluates an XPath expression using the parsed document node

Parse the file music.xml into a document node.

import matlab.io.xml.dom.*

doc = parseFile(Parser,"music.xml");

Evaluate an XPath expression that returns the instrument elements that have a type attribute of percussion. Specify that the result type is a vector of node objects.

import matlab.io.xml.xpath.*

xpExpr = "//Instrument[@type='percussion']";
xmlFilePath = "music.xml";
data = evaluate(Evaluator,xpExpr,doc,EvalResultType.NodeSet)
data=1×2 Element array with properties:
    TagName
    HasAttributes
    TextContent
    Children

Version History

Introduced in R2021a

expand all