How to get node value / innerHTML with XPath?
node() = innerXml text() = innerText both are arrays, so text()[1] is a first children text node…
node() = innerXml text() = innerText both are arrays, so text()[1] is a first children text node…
This will retrieve all text elements with a parent ul element. //ul/descendant::*/text()
Same concept as bytebuster, but a different xpath: /*/p[count(preceding-sibling::divider)=1]
Can someone explain the difference between text() and string() functions. I. text() isn’t a function but a node test. It is used to select all text-node children of the context node. So, if the context node is an element named x, then text() selects all text-node children of x. Other examples: /a/b/c/text() selects all text-node … Read more
You should remove the forwardslash prefix from “/img[@alt]” as it signifies that you want to start at the root of the document. HtmlNode imageNode = linkNode.SelectSingleNode(“img[@alt]”);
There are a few techniques that you might try; which you use will depend on exactly what information you need to get out of the document, how rigorous you want to be, and how conformant the XPath implementation you’re using is. One way to get the namespace URI associated with a particular prefix is using … Read more
All namespaces need to be registered when parsing. Nokogiri automatically registers namespaces on the root node. Any namespaces that are not on the root node you have to register yourself. This should work: puts doc.xpath(‘//dc:title’, ‘dc’ => “URI”) Alternately, you can remove namespaces altogether. Only do this if you are certain there will be no … Read more
You can check multiple conditions inside the same square brackets: /Location/Addr[State=”TX” or State=”AL” or State=”MA”] Or if you have a really long list, you can create a list of states and use the contains() function. /Location/Addr[contains(‘TX AL MA’, State)] This will work fine for two-letter state abbreviations. If you want to make it more robust … Read more
This is surprisingly difficult to do. Take a look at the XPath Recommendation, and you’ll see that it defines a literal as: Literal ::= ‘”‘ [^”]* ‘”‘ | “‘” [^’]* “‘” Which is to say, string literals in XPath expressions can contain apostrophes or double quotes but not both. You can’t use escaping to get … Read more
Query: SELECT x.i.value(‘(./text())[1]’, ‘VARCHAR(10)’) FROM MyTable.SomeXmlColumn.nodes(‘./people/person/firstName’) AS x(i); doesn’t work, for the same reason why this query doesn’t work: SELECT * FROM Person.Person.FirstName; but this does: SELECT FirstName FROM Person.Person; – FROM clause expects rowset, so this is valid, since nodes() returns rowset: DECLARE @xml AS XML = ‘<people> <person><firstName>Jon</firstName><lastName>Johnson</lastName></person> <person><firstName>Kathy</firstName><lastName>Carter</lastName></person> <person><firstName>Bob</firstName><lastName>Burns</lastName></person> </people>’; SELECT x.i.value(‘(./text())[1]’, … Read more