xpath

Check if a node is present

Syntax#

  • boolean(path_to_node)

Remarks#

Boolean function has other uses

  1. Check if a string is empty
  2. Check if the argument is not a number (NaN) or is 0

Does the animal have tusks?

XML

<Animal>
    <legs>4</legs>
    <eyes>2</eyes>
    <horns>2</horns>
    <tail>1</tail>
</Animal>

XPATH

boolean(/Animal/tusks)

OUTPUT

false

Does the animal have horns?

XPATH

<Animal>
    <legs>4</legs>
    <eyes>2</eyes>
    <horns>2</horns>
    <tail>1</tail>
</Animal>

XPATH

boolean(/Animal/horns)

OUTPUT

true

Are there plants in the house?

XML

<House>
    <LivingRoom>
        <plant name="rose"/>
    </LivingRoom>
    <TerraceGarden>
        <plant name="passion fruit"/>
        <plant name="lily"/>
        <plant name="golden duranta"/>
    </TerraceGarden>
</House>

XPATH

boolean(/House//plant)

OUTPUT

true

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow