How do get element out of xml file
-
09-06-2019 - |
Question
I get an XML file From a web service. Now I want to get one of those elements out of the file.
I think I should go use XPath - any good starter reference?
Solution
Not VB specific, but try this: http://www.w3schools.com/xsl/xpath_intro.asp
OTHER TIPS
I've just been recovering my XPath skills- this Xslt and XPath Quick Reference sheet is quite a useful reference - it doesn't go into depth but it does list what is available and what you might want to search for more information on.
The w3schools tutorial linked previously isn't that great - it takes a long time to not cover a lot of ground - but it is still worth reading.
One way would be to only extract the needed informations with an xslt file into a new xml and use this new xml as data basis for further processing
If I need to do some XPath, I just tweak one of these examples.
- child::node() selects all the children of the context node, whatever their node type
- attribute::name selects the name attribute of the context node
- attribute::* selects all the attributes of the context node
- descendant::para selects the para element descendants of the context node
- ancestor::div selects all div ancestors of the context node
- ancestor-or-self::div selects the div ancestors of the context node and, if the context node is a div element, the context node as well
- descendant-or-self::para selects the para element descendants of the context node and, if the context node is a para element, the context node as well
- self::para selects the context node if it is a para element, and otherwise selects nothing
- child::chapter/descendant::para selects the para element descendants of the chapter element children of the context node
- child::*/child::para selects all para grandchildren of the context node
- / selects the document root (which is always the parent of the document element)
- /descendant::para selects all the para elements in the same document as the context node
- /descendant::olist/child::item selects all the item elements that have an olist parent and that are in the same document as the context node
- child::para[position()=1] selects the first para child of the context node
- child::para[position()=last()] selects the last para child of the context node
- child::para[position()=last()-1] selects the last but one para child of the context node
- child::para[position()>1] selects all the para children of the context node other than the first para child of the context node
- following-sibling::chapter[position()=1] selects the next chapter sibling of the context node
- preceding-sibling::chapter[position()=1] selects the previous chapter sibling of the context node
- /descendant::figure[position()=42] selects the forty-second figure element in the document
- /child::doc/child::chapter[position()=5]/child::section[position()=2] selects the second section of the fifth chapter of the doc document element
- child::para[attribute::type="warning"] selects all para children of the context node that have a type attribute with value warning
- child::para[attribute::type='warning'][position()=5] selects the fifth para child of the context node that has a type attribute with value warning
- child::para[position()=5][attribute::type="warning"] selects the fifth para child of the context node if that child has a type attribute with value warning
- child::chapter[child::title='Introduction'] selects the chapter children of the context node that have one or more title children with string-value equal to Introduction
- child::chapter[child::title] selects the chapter children of the context node that have one or more title children
- child::*[self::chapter or self::appendix] selects the chapter and appendix children of the context node
- child::*[self::chapter or self::appendix][position()=last()] selects the last chapter or appendix child of the context node
An in depth documentation can be found here. Also these example are taken from there.