Home > database >  XML XPATH based on atribute and value as filter
XML XPATH based on atribute and value as filter

Time:09-03

I have the following XML


<tickerfeed version="2.4">
    <playlist type="flipping_carousel" name="Ctl_Foreign" target="carousel">
        <element>
            <template>Ctl_Foreign</template>
            <field name="foreign_stock">DJI</field>
            <field name="price">31301.66</field>
            <field name="arrow">2</field>
        </element>
        <element>
            <template>Ctl_Foreign</template>
            <field name="foreign_stock">SPX</field>
            <field name="price">3918.92</field>
            <field name="arrow">2</field>
        </element>
        <element>
            <template>Ctl_Foreign</template>
            <field name="foreign_stock">COMP</field>
            <field name="price"/>
            <field name="arrow">3</field>
        </element>
    </playlist>
</tickerfeed>

How can I filter the based on field name="foreign_stock" that has value "SPX" and get all the rest fields of "element" node?

for example, to filter only the:

<template>Ctl_Foreign</template>
<field name="foreign_stock">SPX</field>
<field name="price">3918.92</field>
<field name="arrow">2</field>

I know that need to use somthing like /tickerfeed/playlist/element/field[@name='foreign_stock' and xyz]

but how to combine a search on attribute and the value together. What to write in xyz part or XPath?

Thanks in advance for your help EG

CodePudding user response:

Please try the following XPath expression:

/tickerfeed/playlist/element[field[@name="foreign_stock"]/text()="SPX"]/*
  • Related