Xpath best practices - linq

I have a readonly xml file and I have a set of xpath values.
I need to create a function which would take in the xpath and return the value(s) corresponding to the xpath.
I am a little confused regarding what would be the best way to proceed. The options I am thinking are using the regular XPathDocument/Navigator/Iterator classes or using LINQ to xml.
The function I am trying to implement is:
T GetString(string inputXpath) where T could be bool/string/array etc.
Can someone help?Also, this function is going to be called all across the application, so performance might be a consideration.
Thank you!
-Agent

What you want to write will just return:
XpathNavigator.Evaluate(inputXpath);
Obviously, T must be just... object :)
Read the XpathNavigator.Evaluate() documentation here.

Related

Xpath implementation in Google Sheets

Xpath newbie question, so forgive me if this seems straight forward, but I really have looked everywhere for the answer!
I'm trying to build a process for extracting all my playlists from Spotify and making it universal, allowing migration across various platforms. I will gladly share once completed as I know many people would find this useful.
I'm unfortunately stumped on trying to extract some data from:
[http://musicbrainz.org/ws/2/artist/?query=%22faith%20no%20more%22][1]
I am looking to extract the id from the artist element, which should be b15ebd71-a252-417d-9e1c-3e6863da68f8. I can get this working in Base X with the following:
declare namespace mmd="http://musicbrainz.org/ns/mmd-2.0#";
declare variable $doc := doc("http://musicbrainz.org/ws/2/artist/?query=%22faith%20no%20more%22");
$doc/mmd:metadata/mmd:artist-list/mmd:artist/#id
However, in Google Sheets using Importxml, the best I can do is:
=IMPORTXML("http://musicbrainz.org/ws/2/artist/?query=%22faith%20no%20more%22","//#id")
This results in all 3 id results being returned:
b15ebd71-a252-417d-9e1c-3e6863da68f8
489ce91b-6658-3307-9877-795b68554c98
83f22bb6-4631-443c-bace-9fae8540362a
I am completely stumped and any help will be greatly appreciated.
Kind regards,
James
I haven't been able to find any useful documentation on Google's IMPORTXML, but there is no evidence that it provides any way to establish a namespace binding, or that it supports the XPath 2.0 syntax *:metadata to select elements independent of namespace. If that's the case then you may need to resort to the horrible construct *[local-name()='metadata']/*[local-name()='artist-list']/*[local-name()='artist']

Understanding X-Path Expression

I'm trying to get an understanding of XPath in order to parse a diffxml file. I skimmed over the w3schools site. Am I understanding these correctly?
Statement 1: /node()[1]/node()[3]
Selects the third child of the root node
Statement 2: /node()[1]/node()[1]/node()[1]
Selects the child of the first node of the root node
Statement 3: /node()[1]/node()[3]/node()[2]
Selects the second child of the third node under the root node.
Yes, you understand them correctly, but this is not how you'd use XPath. First node() can be anything, not just elements. Then the pure index is arguably the wort way of selecting things, you should really use names, and possibly predicates for filtering the node-sets.
You'll find a lot of criticism of w3schools on this site. Personally I find it a useful resource, but only when I'm trying to remind myself of something I once knew. It's not really designed for teaching yourself things from scratch, and I suggest you need a different learning strategy. Call me old-fashioned, but when I'm learning a new technology I find there's nothing better than a good book.
You've understood your examples correctly as far as I can tell. But have you understood what a "node" is? For example, do you know under what circumstances whitespace text counts as a node? The key to understanding XPath is to understand the data model, and the way in which the data model relates to the lexical (angle-bracket) form of the XML.

Oracle updateXML - What is the text() for?

When I'm using the Oracle updateXML()-Function, I have to address the path to the attribute which I'm trying to update (e.g. '/supplier/companyName/text()', 'Some Company Ltd').
OK, I'm fine with that. But what I actually doesn't understand is the text(). It seems that it is a function. Is that correct?
But what else is possible to use there?
Here's a pretty good explanation of the usage of text():
In the case of updateXML() I think it makes sense to use it because of what you want the resulting update to look like.

howto get Builder to create <tag></tag> instead of <tag/>

I,m using Builder::XmlMarkup to create xml. I want to create a tag without content because the api force me to create this.
If I use a blog
xml.tag do
end
I get what i need
<tag></tag>
but I want it shorter
xml.mytag
this gives me
<mytag/>
but i want
<mytag></mytag>
what do I have to pass as option.
regards Kai
Just pass empty string as a parameter. xml.mytag('')
Why do you want <mytag></mytag> instead of <mytag/>? Since the output is XML, downstream applications should not know or care about the difference.
According to the Infoset spec (Appendix D point 7), "The difference between the two forms of an empty element: <foo/> and <foo></foo>" is not represented in the XML Information Set.
This doesn't answer your "how" question, but if you discover that you actually don't need to do what you're trying to do, it may save you from a difficult and unnecessary wild goose chase.
ok empty string is nice, another one-line-way is empty block I found out.
xml.mytag{}

Is there an XPath equivilent for Linq to XML?

I have been using Linq to XML for a few hours and while it seems lovely and powerful when it comes to loops and complex selections, it doesn't seem so good for situations where I just want to select a single node value which XPath seems to be good at.
I may be missing something obvious here but is there a way to use XPath and Linq to XML together without having to parse the document twice?
You can still use XPath, with the XPathEvaluate, XPathSelectElement and XPathSelectElements extension methods. You can also call CreateNavigator to create an XPathNavigator.

Resources