Html程序  |  47行  |  1.87 KB

<html>
<head>
<link rel="stylesheet" href="../js/resources/js-test-style.css">
<script src="../js/resources/js-test-pre.js"></script>
<script src="xpath-test-pre.js"></script>
</head>
<body>
<p>This tests that XPath expressions with prefixes work correctly.</p>
<div id="console"></div>
<script>
    var xmlString = '<ns:foo xmlns:ns="http://www.example.org" xmlns:foo="urn:foobar"/>';

    var doc = (new DOMParser()).parseFromString(xmlString, "text/xml");
    var contextNode = doc.documentElement;
    var nsResolver = document.createNSResolver(contextNode);

    var expr = doc.createExpression("/ns:foo", nsResolver);
    var result = expr.evaluate(contextNode, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
    checkSnapshot("/ns:foo", result, [doc.documentElement]);

    var expr = doc.createExpression("/ns:*", nsResolver);
    var result = expr.evaluate(contextNode, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
    checkSnapshot("/ns:*", result, [doc.documentElement]);

    var expr = doc.createExpression("/foo:*", nsResolver);
    var result = expr.evaluate(contextNode, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
    checkSnapshot("/foo:*", result, []);

    // Now try a resolver originating from the function
    var namespaces = { xmpl: "http://www.example.org" };
    var mapResolver = function(prefix) { return namespaces[prefix]; };

    var expr = doc.createExpression("/xmpl:*", mapResolver);
    var result = expr.evaluate(contextNode, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
    checkSnapshot("/xmpl:*", result, [doc.documentElement]);

    var evaluator = new XPathEvaluator();
    var result = evaluator.evaluate("/xmpl:*", doc, mapResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
    checkSnapshot("/xmpl:*", result, [doc.documentElement]);

    var successfullyParsed = true;

</script>
<script src="../js/resources/js-test-post.js"></script>
</body>
</html>