Main Content

matlab.io.xml.xpath.PrefixResolver Class

Namespace: matlab.io.xml.xpath

Abstract base class for namespace prefix resolvers

Since R2021a

Description

The matlab.io.xml.xpath.PrefixResolver class is an abstract base class for deriving a resolver that resolves namespace prefixes that occur in XPath expressions. To configure a matlab.io.xml.xpath.Evaluator object to use a prefix resolver, set the PrefixResolver property of the Evaluator object to an object of the derived class and set the ResolvePrefixes property to true.

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

Class Attributes

Abstract
true
HandleCompatible
true
ConstructOnLoad
true

For information on class attributes, see Class Attributes.

Methods

expand all

Examples

collapse all

Implement a namespace prefix resolver and use it to resolve prefixes in XPath expressions evaluated by a matlab.io.xml.xpath.Evaluator object.

Derive the myPrefixResolver class from the matlab.io.xml.xpath.PrefixResolver class. Define the getNamespaceForPrefix and getURL methods.

classdef myPrefixResolver < matlab.io.xml.xpath.PrefixResolver
    
    methods
        function url = getNamespaceForPrefix(resolver,prefix)
            if prefix == "a"
                url = "https://mycompany.com/ns/namespacea";
                            else
                url = "https://mycompany.com/ns/namespaceb";
                            end
        end
        function url = getURL(resolver)
            url = "https://mycompany.com";
        end
    end

end

Define an XPath expression evaluator and set the PrefixResolver property to a myPrefixResolver object.

import matlab.io.xml.xpath.*
evalObj = Evaluator();
evalObj.PrefixResolver = myPrefixResolver;

Suppose that myXML.xml contains the XML to search. This code evaluates an expression that uses a prefix in the namespace specified by the myPrefixResolver object methods.

data = evaluate(evalObj,"//a:Instrument","myXML.xml",EvalResultType.NodeSet);

Version History

Introduced in R2021a