一些常用的XPath语法

https://files.cnblogs.com/bear831204/XPathTest.zip

部分代码:

WriteElements("The root bookstore, this is the whole document:",
                "./bookstore");
            WriteElements("All <first-name> elements:",
                ".//first-name");
            WriteElements("First <author> inside each <book>:",
                ".//book/author[1]");
            WriteElements("Last <author> inside each <book>:",
                ".//book/author[last()]");
            WriteElements("First <author> from the entire set of <author> elements within <book> elements:",
                "(.//book/author)[1]");
            WriteElements("First <author> inside first <book>:",
                ".//book[1]/author[1]");
            WriteElements("All <author> elements that contain at least one <degree> element child and at least one <award> element child:",
                ".//author[degree and award]");
            WriteElements("All <author> elements that contain at least one <degree> or <award> element and at least one <publication> element:",
                ".//author[(degree or award) and publication]");
            WriteElements("The third <author> element that has a <first-name> child element:",
                "(.//author[first-name])[3]");
            WriteElements("All <book> elements whose style attribute value is equal to the specialty attribute value of the <bookstore> element at the root of the document:",
                ".//book[/bookstore/@specialty=@style]");
            WriteElements("All <degree> elements one or more levels deep in the <bookstore> element (arbitrary descendants):",
                "./bookstore//degree");
            WriteElements("All elements with the style attribute:",
                ".//*[@style]");
            WriteElements("First 2 <author> inside each <book>:",
                ".//book/author[position() < 3]");
            WriteElements("All <author> elements that contain at least one <last-name> element child with the value Bob:",
                ".//author[last-name = 'Bob']");
            WriteElements("All <author> elements that has a <last-name> child element with the value Bob and a <first-name> child element with the value Joe:",
                ".//author[last-name = 'Bob' and first-name = 'Joe']");
            WriteElements("All <author> elements that do no contain <award> child elements with the value Pulitzer, inculding the author doesn't have award and has award but the value is not Pulitzer:",
                ".//author[not(award = 'Pulitzer')]");
            WriteLog("The count of book element:" + "    (count(.//book))");
            WriteLog(_doc.XPathEvaluate("count(.//book)").ToString());
            WriteLog();
            WriteElements("All <author> elements that has a <first-name> child element with the value contains 'Ton':",
                ".//author[contains(first-name,'Ton')]");
            WriteElements("All elements whose name contains st",
                ".//*[contains(name(),'st')]");
            WriteElements("All first-name and last-name elements:",
                ".//*[name() = 'first-name' or name() = 'last-name']");
            WriteElements("All elements whose name starts with publica:",
                ".//*[starts-with(name(),'publica')]");
            WriteElements("All elements whose name starts with my:",
                ".//*[starts-with(name(),'my')]");
            WriteElements("All elements whose local name is author:",
                ".//*[local-name()='author']");
            WriteElements("All elements whose value is misery:",
                ".//*[.='misery']");
            WriteElements("All <author> elements whose value is Matthew Bob:",
                ".//author[. = 'Matthew Bob']");
            WriteElements("All author elements containing any child element whose value is Bob:",
                ".//author[* = 'Bob']");

原文地址:https://www.cnblogs.com/bear831204/p/2501628.html