Main Content

matlab.io.xml.xpath.EvalResultType Class

Namespace: matlab.io.xml.xpath

Result type for XPath expression evaluation

Since R2021a

Description

The matlab.io.xml.xpath.EvalResultType class enumerates the types of values that an XPath expression evaluation can return. To specify a result type for an evaluation, provide the enumeration member as the last argument of the evaluate method of a matlab.io.xml.xpath.Evaluator object.

Enumeration MemberReturns
matlab.io.xml.xpath.EvalResultType.Node

The first node selected by the XPath expression as a matlab.io.xml.dom node object, such as a matlab.io.xml.dom.Element object.

matlab.io.xml.xpath.EvalResultType.NodeSet

The nodes selected by the XPath expression as a vector of matlab.io.xml.dom node objects.

matlab.io.xml.xpath.EvalResultType.Number

The value of the first node selected by the XPath expression as a double.

matlab.io.xml.xpath.EvalResultType.Boolean

The value of the first node selected by the XPath expression as a logical.

matlab.io.xml.xpath.EvalResultType.String

The value of the first node selected by the XPath expression as a string scalar.

Examples

collapse 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

Version History

Introduced in R2021a