Main Content

matlab.io.xml.xpath.CompiledExpression Class

Namespace: matlab.io.xml.xpath

Compiled XPath expression

Since R2021a

Description

An object of the matlab.io.xml.xpath.CompiledExpression class represents a compiled XPath expression. You can provide a CompiledExpression object, instead of the source XPath expression, to the evaluate method of a matlab.io.xml.xpath.Evaluator object.

The matlab.io.xml.xpath.CompiledExpression class is a handle class.

Class Attributes

ConstructOnLoad
true
HandleCompatible
true

For information on class attributes, see Class Attributes.

Creation

To create a matlab.io.xml.xpath.CompiledExpression object, call the compileExpression method of a matlab.io.xml.xpath.Evaluator object.

Properties

expand all

Source XPath expression, specified as a character vector.

Attributes:

GetAccess
public
SetAccess
immutable
Transient
true
NonCopyable
true

Examples

collapse all

This example compiles an XPath expression and passes the compiled expression to an XPath expression evaluator.

The example uses the file music.xml, which 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>

Compile the XPath expression //Instrument by calling the compileExpression method of a matlab.io.xml.xpath.Evaluator object.

import matlab.io.xml.xpath.*

evalObj = Evaluator();
compExpObj = compileExpression(evalObj,"//Instrument");

Evaluate the compiled expression in the context of music.xml and return the resulting nodes as a vector of node objects.

xmlFilePath = "music.xml";
data = evaluate(evalObj,compExpObj,xmlFilePath,EvalResultType.NodeSet)
data=1×4 Element array with properties:
    TagName
    HasAttributes
    TextContent
    Children

Version History

Introduced in R2021a