# Failing to use //div as xpath selector

**URL:** <https://swi-prolog.discourse.group/t/failing-to-use-div-as-xpath-selector/2065>\
**Category:** Help!\
**Tags:** bug\
**Created:** [March 25, 2020, 9:53pm UTC](https://swi-prolog.discourse.group/t/failing-to-use-div-as-xpath-selector/2065 "2020-03-25T21:53:01Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![emacstheviking](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/emacstheviking/32/228_2.png) [@emacstheviking](https://swi-prolog.discourse.group/u/emacstheviking)\
**Post date:** [March 25, 2020, 9:53pm UTC](https://swi-prolog.discourse.group/t/failing-to-use-div-as-xpath-selector/2065/1 "2020-03-25T21:53:01Z")

</div>

?- prolog\_flag(version,X).  
X = ‘SWI-Prolog 8.0.3 (x86\_64-linux): Jun 19 2019, 14:01:15’.

My code looks like this:

```prolog
:- use_module(library(http/http_client)).
:- use_module(library(http/http_open)).
:- use_module(library(xpath)).

?- j2(P, //div, X),length(X,XL), show(X).
ERROR: Syntax error: Unbalanced operator
ERROR: j2(P, //di
ERROR: **here**
ERROR: v, X),length(X,XL), show(X) . 

```

If I wrap it in singles as an atom, it just fails:

```prolog
?- j2(P, '//div', X),length(X,XL), show(X).
false.

```

I am sure I had this problem a very long time ago but I couldn’t find it on the forums… how on earth do I get to be able to specify a DIV tag!

I was having such fun until this came along!! I am sure it’s simple once you know what you are doing…

Thanks,  
Sean.

---

<div class="post-metadata">

**Author:** ![jamesnvc](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jamesnvc/32/14_2.png) [@jamesnvc](https://swi-prolog.discourse.group/u/jamesnvc)\
**Post date:** [March 25, 2020, 10:51pm UTC](https://swi-prolog.discourse.group/t/failing-to-use-div-as-xpath-selector/2065/2 "2020-03-25T22:51:07Z")

</div>

I believe that the ‘//’ is an operator, which is why `'//div'` fails. Does `'//'(div)` work?

EDIT: not sure why it’s not working as `//div` though; `xpath` should be exporting that `op`…

---

<div class="post-metadata">

**Author:** ![emacstheviking](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/emacstheviking/32/228_2.png) [@emacstheviking](https://swi-prolog.discourse.group/u/emacstheviking)\
**Post date:** [March 25, 2020, 10:54pm UTC](https://swi-prolog.discourse.group/t/failing-to-use-div-as-xpath-selector/2065/3 "2020-03-25T22:54:23Z")

</div>

div/2 is also an operator!

Let me try… yes, it does.

So…attempting to understand…using //div just like that is functor(args), is ‘//’(div) is writing it in functional form so that it doesn’t get parsed the “wrong” way because of operator precedence?

And, thanks James!

---

<div class="post-metadata">

**Author:** ![jamesnvc](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jamesnvc/32/14_2.png) [@jamesnvc](https://swi-prolog.discourse.group/u/jamesnvc)\
**Post date:** [March 25, 2020, 10:56pm UTC](https://swi-prolog.discourse.group/t/failing-to-use-div-as-xpath-selector/2065/4 "2020-03-25T22:56:19Z")

</div>

Oh right, `div` is also an arithmetic operator, of course!

> [@emacstheviking](#):
>
> using //div just like that is functor(args), is ‘//’(div) is writing it in functional form so that it doesn’t get parsed the “wrong” way because of operator precedence?

I believe so; `//'div'` should work properly as well and is probably a better solution (because the problem isn’t about `//` as an operator, it’s that `div` is being treated as an operator, not an atom).

---

<div class="post-metadata">

**Author:** ![emacstheviking](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/emacstheviking/32/228_2.png) [@emacstheviking](https://swi-prolog.discourse.group/u/emacstheviking)\
**Post date:** [March 25, 2020, 11:02pm UTC](https://swi-prolog.discourse.group/t/failing-to-use-div-as-xpath-selector/2065/5 "2020-03-25T23:02:33Z")

</div>

Yes, //‘div’ also works… but not sure why…so…hacky…I see that…

?- ‘//’(div) = //‘div’.  
true.

so they are the same thing, the functor ‘//’ with the atom div as the sole argument.

---

<div class="post-metadata">

**Author:** ![pmoura](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/pmoura/32/11_2.png) [@pmoura](https://swi-prolog.discourse.group/u/pmoura)\
**Post date:** [March 25, 2020, 11:07pm UTC](https://swi-prolog.discourse.group/t/failing-to-use-div-as-xpath-selector/2065/6 "2020-03-25T23:07:11Z")

</div>

Just a case of operator clash as @jamesnvc pointed out:

```nohighlight
?- current_op(P, T, //).
P = 400,
T = fx ;
P = 400,
T = yfx.

?- current_op(P, T, div).
P = 400,
T = yfx.

```

As usual, you can solve the clash using parenthesis:

```nohighlight
?- write(//(div)).
// (div)
true.

```

---

<div class="post-metadata">

**Author:** ![emacstheviking](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/emacstheviking/32/228_2.png) [@emacstheviking](https://swi-prolog.discourse.group/u/emacstheviking)\
**Post date:** [March 25, 2020, 11:10pm UTC](https://swi-prolog.discourse.group/t/failing-to-use-div-as-xpath-selector/2065/7 "2020-03-25T23:10:59Z")

</div>

Ah! Thanks Paulo.

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [March 26, 2020, 8:13am UTC](https://swi-prolog.discourse.group/t/failing-to-use-div-as-xpath-selector/2065/8 "2020-03-26T08:13:08Z")

</div>

If you use modules and have no use for `div` for arithmetic in that module you can also do this to locally kill the operator:

```
:- op(0, yfx, div).

```

Not sure this should be considered good or bad style. It certainly suffers from portability as some Prolog systems (still) have only global operators.

---

<div class="post-metadata">

**Author:** ![emacstheviking](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/emacstheviking/32/228_2.png) [@emacstheviking](https://swi-prolog.discourse.group/u/emacstheviking)\
**Post date:** [March 26, 2020, 1:18pm UTC](https://swi-prolog.discourse.group/t/failing-to-use-div-as-xpath-selector/2065/9 "2020-03-26T13:18:04Z")

</div>

the op//3 and generally definiting operators is not something I have ever done so it’s all a bit mysterious to me…it’s something I keep meaning to study but somehow I have managed thus far without needed a custom operator.

Do you know of any good tutorials, I have found some but they tend to be terse and not very helpful…if I can understand I#d write my own…currently working in a custom little web server with Prolog (I didn’t get on with
