Avoiding // in XPath

Asked 2 years ago, Updated 2 years ago, 167 views

I have been advised not to use // for XPath in XSLT style sheets that handle large XML documents (data). For example, //para[exists(b)] for XML files in the summary structure.

<?xml version="1.0" encoding="UTF-8"?>
<doc>
    <!--Actually, the following structures are repeated-->
    <part>
        <chapter>
            <section>
                <p>This is <b><u>sample>/u> that is <b> structure.</p>
                <p>This is <i><u>sample>/u> that is <i> structure.</p>
                <p>~</p>
                <p>~</p>
                <p>~</p>
            </section>
        </chapter>
    </part>
</doc>

This should be /doc/part/chapter/section/p [exists(b)].

Why shouldn't I use //?

xml xpath xsl xslt

2022-09-30 21:28

1 Answers

It's self RES. I think I found out by writing another question.

The abbreviated notation //para[exists(b)] is (fn:root(self::node())/descendant-or-self::node()/para[exists(b)].

3.2 Path Expressions

A"//" at the beginning of a path expression is an ablation for the initial steps (fn:root(self::node())/descendant-or-self::node()/(however,"//"by itself is not a valid path expression [XrPS3].) The effect of these initial steps is to establish an initial node sequence that contains the root of the tree in which the context node is found, plus all nodes desired from this root.

That is, whatever the context is

That means 2. lick all the nodes in the input document (text node, comment node or whatever!). This will mean loading the entire document. It can take some time.

By contrast, /doc/part/chapter/section/p[exists(b)] you can avoid loading all of those nodes as you follow the elements.

I've been using // without hesitation, but now I understand that I shouldn't use it often.


2022-09-30 21:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.