Friday, July 16, 2010

XPath on .NET

  1. XPath defines a common syntax for accessing XML nodes through the XML DOM as well as from XML Stylesheet language tranformation
  2. System.Xml.XPath is the parent namespace for XPath functionality in .NET framework
  3. XPath expression => Run time evaluation engine => Against a data source (XML DOM)
  4. XPath expressions are always evaluated in the context of a node (context node similar to current directory)
  5. The result of execution is a context node set
  6. XML DOM (Select nodes within the context of the XMLDocument class)
  7. An XML Node Name is the Namespace + Local Name
  8. 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])
  1. Axis
    1. self
    2. child
    3. descendant
    4. ancestor
    5. following
    6. following-sibling
    7. preceding
    8. preceding-sibling
    9. attribute
  2. Node-test => Sequence of node names in the node-set
  3. 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