質問

xhtmlページで実行すると、xpathは結果を返さないようです。

var result = document.evaluate( "//a/img", document.body, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null );

たとえば、空の結果セットを返します。ここでの問題は何ですか、どうすれば修正できますか?

役に立ちましたか?

解決

xhtml要素はnull名前空間にないため、名前空間リゾルバを渡す必要がありますメソッドへ:

function resolver(prefix) {
    return prefix === 'x' ? 'http://www.w3.org/1999/xhtml' : null;
}
var result = document.evaluate( "//x:a/x:img", document.body, resolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null );

他のヒント

この解決策に同意しますが、xpathExpressionで前の兄弟を使用しています。

function nsResolver(prefix){
    var ns = {
        'mathml' : 'http://www.w3.org/1998/Math/MathML', // for example's sake only
        'h' : 'http://www.w3.org/1999/xhtml'
    };
    return ns[prefix];
}

次に:

document.evaluate('//h:' + node.tagName + '/preceding-sibling::' + node.tagName, node, nsResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null).snapshotLength

xhtmlで常に0を返します。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top