Are these two xpath styles logical and equivalent?

Suppose I have xml:

<Products>
  <Product name="Liquid Oxygen">
    <Manufacturer name="Universal Exports" country="UK" />
  </Product>
  <Product name="Hydrazine">
    <Manufacturer name="ACME Inc." country="USA" />
  </Product>
  <Product name="Gaseous Oxygen" obsolete="true">
    <Manufacturer name="Universal Exports" country="UK" />
  </Product>
  <Product name="Liquid Nitrogen">
    <Manufacturer name="Penguins R Us" country="Antarctica" />
  </Product>
</Products>

And I want to allocate Product nodes with a node Manufacturerfrom @countryfrom UK, but it doesn’t @obsolete true. I can say that

/Products/Product[Manufacturer/@country = 'UK' and not(@obsolete = 'true)]

or

/Products/Product[Manufacturer/@country = 'UK'][not(@obsolete = 'true')]

and both will get the nodes I need.

My question is, is there any functional difference between the two approaches to the i-conditions? Is there a situation where different approaches can give different results? (I understand that andserves purpose in more difficult conditions). Stylistically preferable to another?

(I use C # and .NET 2.0, but I do not believe that this will affect the answer)

+3
source share
4 answers

and . , , .

, , position(), count(), first() last(), .

:

/Products/Product[Manufacturer/@country = 'UK' and (position() = 1)]

node, , :

/Products/Product[Manufacturer/@country = 'UK'][position() = 1]

node , :

/Products/Product[Manufacturer/@country = 'UK'][1]
+7

:

  • (… and …) - ;
  • (…][…) - ;

( "" ) , . ( "" ) .

, , ,

/Products/Product[Manufacturer/@country = 'UK'][2]

" , ",

/Products/Product[Manufacturer/@country = 'UK' and position() = 2]

" , ".

+5

XmlVisualizer ( Windows/.NET) xpath xpath:

alt text

.

+2

, , . , .

I personally prefer the first ( and), as it’s easier to say what happens, especially when you add more complex comparisons (ands and ors) to the mix.

+1
source

Source: https://habr.com/ru/post/1725623/


All Articles