XPath
XPath is a non-XML language used to identify particular parts of XML documents. XPath lets you write expressions that refer to the document’s firstperson
element, the seventh child element of the third person
element, the ID
attribute of the first person
element whose contents are the string “Fred Jones,” all xml-stylesheet
processing instructions in the document’s prolog, and so forth. XPath indicates nodes by position, relative position, type, content, and several other criteria. XSLT uses XPath expressions to match and select particular elements in the input document for copying into the output document or further processing. XPointer uses XPath expressions to identify the particular point in or part of an XML document that an XLink links to.
XPath expressions can also represent numbers, strings, or Booleans, so XSLT stylesheets carry out simple arithmetic for numbering and cross-referencing figures, tables, and equations. String manipulation in XPath lets XSLT perform tasks like making the title of a chapter uppercase in a headline, but mixed case in a reference in the body text.
XPath Functions
XPath provides many functions you may find useful in predicates or in raw expressions. All of these functions are discussed in Chapter 19, XPath Reference. For example, the position( )
function returns the current node’s position in the context node list as a number. This XSLT template uses the position( )
function to calculate the number of the person
being processed, relative to other nodes in the context node list:
<xsl:template match="person">
Person <xsl:value-of select="position( )"/>,
<xsl:value-of select="name"/>
</xsl:template>
Each XPath function returns one of these four types:
- boolean
- number
- node set
- string
XML Nodes
To find nodes in an XML file you can use XPath expressions. Method XmlNode.SelectNodesreturns a list of nodes selected by the XPath string. Method XmlNode.SelectSingleNode findsthe first node that matches the XPath string.
Suppose we have this XML file.
[XML]<Names> <Name> <FirstName>John</FirstName> <LastName>Smith</LastName> </Name> <Name> <FirstName>James</FirstName> <LastName>White</LastName> </Name> </Names>
To get all <Name> nodes use XPath expression /Names/Name
. The first slash means that the <Names> node must be a root node. SelectNodes method returns collection XmlNodeListwhich will contain the <Name> nodes. To get value of sub node <FirstName> you can simply index XmlNode with the node name: xmlNode["FirstName"].InnerText
. See the example below.
XmlDocument xml = new XmlDocument(); xml.LoadXml(myXmlString); // suppose that myXmlString contains "<Names>...</Names>" XmlNodeList xnList = xml.SelectNodes("/Names/Name"); foreach (XmlNode xn in xnList) { string firstName = xn["FirstName"].InnerText; string lastName = xn["LastName"].InnerText; Console.WriteLine("Name: {0} {1}", firstName, lastName); }
The output is:
Name: John Smith Name: James White