Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter with XPath on all elements without a specific attribute

Tags:

My XPath is a little bit rusty... Let's say I have this simple XML file:

<?xml version="1.0" encoding="utf-8" ?> <States xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <States> <StateProvince name="ALABAMA" abbrev="AL" /> .... <StateProvince name="AMERICAN SAMOA" abbrev="AS" territory="true"  /> </States> </States> 

I would like to run a simple XPath query to parse out all of the true states (so don't pull in states where territory = true). I tried \StateProvince[@territory!='true'] but I got zero. Other variations seem to be failing. This seems like it should be simple, but not finding what I want.

Any help appreciated.

like image 365
lyngbym Avatar asked May 01 '09 16:05

lyngbym


People also ask

How do I filter in XPath?

Filtering with XPath First, add your filtering rules by specifying the rule criteria and clicking the Add Rule button. Then click Apply Filters to XPath to generate an XPath expression. You can also manually write an XPath expression to filter your file.

What is the * indicates in XPath?

By adding '//*' in XPath you would be selecting all the element nodes from the entire document. In case of the Gmail Password fields, .//*[@id='Passwd'] would select all the element nodes descending from the current node for which @id-attribute-value is equal to 'Passwd'.

How use attribute for contain in XPath?

Using the XPath contains() function, we can extract all the elements on the page that match the provided text value. Here, tag: tag is the name of the tag that contains the specific word. word: In this case, the word refers to the text that must be discovered in a specific string.


2 Answers

One XPath expression that selects the wanted elements:

        /*/States/StateProvince[not(@territory='true')]

Do note that one must avoid the // abbreviation whenever possible as it causes the whole document (subtree rooted at the context node) to be scanned.

The above XPath expression avoids the use of the // abbreviation by taking into account the structure of the originally-provided XML document.

Only if the structure of the XML document is completely unknown (and the XPath expression is intended to be used accross many XML documents with unknown structure) should the use of the // abbreviation be considered.

like image 122
Dimitre Novatchev Avatar answered Oct 21 '22 01:10

Dimitre Novatchev


You are very close:

//StateProvince[not(@territory) or @territory != 'true'] 

Should get you the result you want.

like image 31
Jordan S. Jones Avatar answered Oct 21 '22 01:10

Jordan S. Jones