Main Content

matlab.io.xml.xpath.Evaluator Class

Namespace: matlab.io.xml.xpath

XPath expression evaluator

Since R2021a

Description

Use an object of the matlab.io.xml.xpath.Evaluator class to create an XPath expression evaluator.

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

Class Attributes

ConstructOnLoad
true
HandleCompatible
true

For information on class attributes, see Class Attributes.

Creation

Description

evaluator = matlab.io.xml.xpath.Evaluator() creates an XPath expression evaluator.

example

Properties

expand all

Custom prefix resolver, specified as an object of a subclass of the matlab.io.xml.xpath.PrefixResolver class.

Attributes:

GetAccess
public
SetAccess
public
NonCopyable
true

Whether to resolve namespace prefixes, specified as true or false. If the value is true, the evaluator attempts to resolve namespace prefixes that occur in the XPath expression. If you also set the setPrefixResolver property, the evaluator uses the specified resolver to resolve prefixes. If you do not set the setPrefixResolver property, the evaluator uses the parent document to resolve prefixes.

Attributes:

GetAccess
public
SetAccess
public
NonCopyable
true

Methods

expand all

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

Algorithms

The MATLAB® Evaluator object supports the use of the following standard XPath functions in XPath expressions:

  • current

  • document

  • element-available

  • format-number

  • function-available

  • generate-id

  • key

  • system-property

  • unparsed-entity-uri

For more information on definitions and usage of these functions, see XPath and XQuery Functions and Operators 3.1 on the World Wide Web Consortium website.

Version History

Introduced in R2021a