1

I can't figure out how to define properly the namespaceResolver function in TypeScript. I have the following resolver function:

(prefix: string): string => {
    if (prefix === "wms") {
        return "http://www.opengis.net/wms";
    } else {
        return null;
    }

But the compiler complains that:

TS2345: Argument of type '(prefix: string) => string' is not assignable to parameter of type 'XPathNSResolver'. Property 'lookupNamespaceURI' is missing in type '(prefix: string) => string'.

If I check the implementation in lib.dom.d.ts, the lookupNaemspaceURI's return value is string too. So I have no idea how should I fix this.

lib.dom.d.ts:

interface XPathNSResolver {
    lookupNamespaceURI(prefix: string): string;
}

EDIT

Here is the whole code:

doc.evaluate("/wms:WMS_Capabilities/wms:Service/wms:Name", doc, (prefix: string): string => {
                    if (prefix === "wms") {
                        return "http://www.opengis.net/wms";
                    } else {
                        return null;
                    }
                }, XPathResult.STRING_TYPE, null);

2 Answers 2

1

Ok, so I figured it out.

I created an XPathNSReslover first:

private xpathResolver: XPathNSResolver = {
        lookupNamespaceURI: prefix => {
            if (prefix === "wms") {
                return "http://www.opengis.net/wms";
            } else {
                return null;
            }
        }
    };

Then I just use this xpathResolver in my evaluate method implementation:

doc.evaluate("/wms:WMS_Capabilities/wms:Service/wms:Name", doc, this.xpathResolver, XPathResult.STRING_TYPE, null);
Sign up to request clarification or add additional context in comments.

Comments

0

For anyone who encounters this issue, I would recommend using 'xpath-ts' instead of 'xpath'. With xpath-ts, convoluted typing (such as in the other solution) can be avoided.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.