3

I would like to be able to find an element named 'test' with no attributes at all.

How can I do that using XPATH?

If i just query for /test it finds me all the test elements even with the ones that contain attributes.

example:

<main>
<test id="test1">txt</test>
<test>txt2</test>
</main>

Querying for //test will find me both of the elements. I want only the one that contains no attributes. I can query for //test[not(@id)] but I was wondering if there is a command for an element with no attributes at all.

Mads Hansen
  • 63,927
  • 12
  • 112
  • 147
ufk
  • 30,912
  • 70
  • 235
  • 386
  • Is there something that will distinguish the element to make it unique? Or are you saying that it will be the only element with no attributes while all the others will have attributes? – spinon Jul 06 '10 at 17:24
  • Possible duplicate of [XPath: How to select nodes which have no attributes?](http://stackoverflow.com/questions/1323755/xpath-how-to-select-nodes-which-have-no-attributes) – Smandoli Dec 14 '16 at 15:06

3 Answers3

6

You almost had it. If you want to find the the test element(s) that have no attributes at all, use a wildcard match for the attribute name in your predicate filter:

//test[not(@*)]
Mads Hansen
  • 63,927
  • 12
  • 112
  • 147
0

Query for test.

http://www.w3schools.com/XPath/xpath_syntax.asp

mcandre
  • 22,868
  • 20
  • 88
  • 147
0
<?xml version="1.0" encoding="windows-1250"?>
<test>
Oh hai
</test>

test = /test
Oh hai = /test/text()

Not sure about your actual intent.

Ta01
  • 31,040
  • 13
  • 70
  • 99