XPath on .NET
- XPath defines a common syntax for accessing XML nodes through the XML DOM as well as from XML Stylesheet language tranformation
- System.Xml.XPath is the parent namespace for XPath functionality in .NET framework
- XPath expression => Run time evaluation engine => Against a data source (XML DOM)
- XPath expressions are always evaluated in the context of a node (context node similar to current directory)
- The result of execution is a context node set
- XML DOM (Select nodes within the context of the XMLDocument class)
- An XML Node Name is the Namespace + Local Name
- An XPath expression returns: Boolean, String, Number, or Node-Set
Context of an XPath query:- Context Node (XmlNode.SelectNodes or XmlNode.SelectSingleNode)
- Context Node-set(axis like drive letters)
- Position (Ordinal position of the context node in the context node-set to which it belongs)
- Size (The number of nodes being parsed by expression)
- Namespace
- Variable binding
- Standard library of functions
Location Path:- Absolute path starts with forward slash /
- Relative path is in the context of the context node
Location path => Location step/location step/location step A Fully Qualified Path (axis::node-test[predicate])- Axis
- self
- child
- descendant
- ancestor
- following
- following-sibling
- preceding
- preceding-sibling
- attribute
- Node-test => Sequence of node names in the node-set
- Predicate => criteria
Abbreviations for axis:- . for context node
- .. for parent node
- no axis for child::
- @ for attribute
- * for all
Sample XPath queries To get all children with name attribute = "AttName"child::*[@Name='AttName'] To get all the attribute nodes attribute::* To get those that have the name attribute regardless of the value child::*[@Name] To find the last child child::*[position() = last()] Get the first next node following-sibling::*[position() = 1] To get all of the processing instructions /processing-instruction() To get all of the processing instructions that the name starts with "proccins_sheet" /processing-instruction()[starts-with(local-name(), "proccins_sheet")]To get all the processing instructions that the value starts with "Name="style-sheet" /processing-instruction()[starts-with(string(), 'Name="style-sheet')]
No comments:
Post a Comment