xpathquery

PHP xpath contains class and does not contain class

北城以北 提交于 2019-12-18 11:45:27
问题 The title sums it up. I'm trying to query an HTML file for all div tags that contain the class result and does not contain the class grid . <div class="result grid">skip this div</div> <div class="result">grab this one</div> Thanks! 回答1: This should do it: <?php $doc = new DOMDocument(); $doc->loadHTMLFile('test.html'); $xpath = new DOMXPath($doc); $nodeList = $xpath->query( "//div[contains(@class, 'result') and not(contains(@class, 'grid'))]"); foreach ($nodeList as $node) { echo $node-

Iterate over all Xpath results

江枫思渺然 提交于 2019-12-13 01:53:50
问题 I have this code: #!/usr/bin/groovy import javax.xml.xpath.* import javax.xml.parsers.DocumentBuilderFactory def testxml = ''' <Employee> <ID>..</ID> <E-mail>..</E-mail> <custom_1>foo</custom_1> <custom_2>bar</custom_2> <custom_3>base</custom_3> </Employee> ''' def processXml( String xml, String xpathQuery ) { def xpath = XPathFactory.newInstance().newXPath() def builder = DocumentBuilderFactory.newInstance().newDocumentBuilder() def inputStream = new ByteArrayInputStream( xml.bytes ) def

Get node based on siblings value xpath query

时间秒杀一切 提交于 2019-12-12 03:17:46
问题 I want to get all the format nodes text in which its sibling node popularity is less than 8 <collection shelf="Classics"> <movie title="The Enemy" shelf="A"> <type>War, Thriller</type> <format>DVD</format> <year>2001</year> <rating>PG</rating> <popularity>10</popularity> <description>Talk about a war</description> </movie> <movie title="Transformers" shelf="B"> <type>Science Fiction</type> <format>DVD</format> <year>1980</year> <rating>R</rating> <popularity>7</popularity> <description

php DomDocument: how can i print an attribute of an element?

◇◆丶佛笑我妖孽 提交于 2019-12-11 09:57:33
问题 how do I print the an attribute of an element? example: $doc = new DOMDocument(); @$doc->loadHTML($page); $xpath = new DOMXPath($doc); $arts= $xpath->query("/td"); foreach ($arts as $art) { here i wanna print the attribute class of the td element, how do i do so ? } 回答1: use DOMElement::getAttribute $art->getAttribute('class'); also, simpleHTMLDOM is more suitable for dealing with html: $html = str_get_html($page); foreach($html->find('td') as $element) echo $element->class.'<br>'; } 回答2:

XPath Query to parse all IDREFS in an attribute (containing possibly many of IDs)

爷,独闯天下 提交于 2019-12-10 16:17:04
问题 I need to come up with a query that gives the products of the types from which no items were sold Meaning if an item is of the type clothing, and no clothing items appear in the list of transactions, I need to display it. This is my XML file (apologies for the super-Canadian-ness): <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE store [ <!ELEMENT store (product*, transaction*)> <!ATTLIST store name CDATA #REQUIRED > <!ELEMENT product EMPTY> <!ATTLIST product name ID #REQUIRED type CDATA

PHP xpath contains class and does not contain class

巧了我就是萌 提交于 2019-11-30 04:44:38
The title sums it up. I'm trying to query an HTML file for all div tags that contain the class result and does not contain the class grid . <div class="result grid">skip this div</div> <div class="result">grab this one</div> Thanks! This should do it: <?php $doc = new DOMDocument(); $doc->loadHTMLFile('test.html'); $xpath = new DOMXPath($doc); $nodeList = $xpath->query( "//div[contains(@class, 'result') and not(contains(@class, 'grid'))]"); foreach ($nodeList as $node) { echo $node->nodeName . "\n"; } Your XPath would be //div[contains(concat(' ', @class, ' '), ' result ') and not(contains

Getting elements with default namespace (no namespace prefix) using XPath

最后都变了- 提交于 2019-11-27 14:42:40
In this SOAP XML file, how can I get the 7 on a using a XPath query? <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <soap:Body> <HelloWorldResponse xmlns="http://tempuri.org/"> <HelloWorldResult>7</HelloWorldResult> </HelloWorldResponse> </soap:Body> </soap:Envelope> This XPath query is not working //*[name () ='soap:Body'] . If you have a namespace prefix set, you could use it, like: //soap:Body But since the nodes you are trying to get use a default namespace , without a

How to use “not” in xpath?

↘锁芯ラ 提交于 2019-11-27 11:15:56
I want to write something of the sort: //a[not contains(@id, 'xx')] (meaning all the links that there 'id' attribute doesn't contain the string 'xx') I can't find the right syntax. not() is a function in xpath (as opposed to an operator), so //a[not(contains(@id, 'xx'))] you can use not(expression) function or expression != true() None of these answers worked for me for python. I solved by this a[not(@id='XX')] Also you can use or condition in your xpath by | operator. Such as a[not(@id='XX')]|a[not(@class='YY')] Sometimes we want element which has no class. So you can do like a[not(@class)]

Getting elements with default namespace (no namespace prefix) using XPath

﹥>﹥吖頭↗ 提交于 2019-11-26 16:54:34
问题 In this SOAP XML file, how can I get the 7 on a using a XPath query? <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <soap:Body> <HelloWorldResponse xmlns="http://tempuri.org/"> <HelloWorldResult>7</HelloWorldResult> </HelloWorldResponse> </soap:Body> </soap:Envelope> This XPath query is not working //*[name () ='soap:Body'] . 回答1: If you have a namespace prefix set, you

How to use “not” in xpath?

ε祈祈猫儿з 提交于 2019-11-26 12:08:20
问题 I want to write something of the sort: //a[not contains(@id, \'xx\')] (meaning all the links that there \'id\' attribute doesn\'t contain the string \'xx\') I can\'t find the right syntax. 回答1: not() is a function in xpath (as opposed to an operator), so //a[not(contains(@id, 'xx'))] 回答2: you can use not(expression) function or expression != true() 回答3: None of these answers worked for me for python. I solved by this a[not(@id='XX')] Also you can use or condition in your xpath by | operator.