XPath/CSS Equivalents
CSS Equivalents
edit:disabled Equivalent
//*[@disabled]
represented :disabled
:checked Equivalent
//*[@checked]
represents :checked
:selected Equivalent
//*[@selected]
represents :selected
:text Equivalent
//*[@type="text"]
represents :text
:contains Equivalent
//*[contains(text(),"you")]
represents :contains("you")
:only-of-type query
//p[contains(@me,"you")]
represents p[me*="you"]
Starts with Equivalent
//p[starts-with(@me,"you")]
represents p[me^="you"]
Contains Equivalent
//p[starts-with(@me,concat("you",'-'))]
represents p[me|="you"]
Ends with Equivalent
//p[substring(@me,string-length(@me)-2)="you"]
represents p[me$="you"]
Like Equivalent
//p[contains(concat(" ",@me, " ")," you ")]
represents p[me~="you"]
Negation Attribute Equivalent
//p[@me!="you"]
represents p[me!="you"]
ID Equivalent
//p[@id="me"]
represents p#me
:not Equivalent
//p[not(@id="me")]
represents p:not(#me)
Class Equivalent
//p[contains(concat(" ", @class, " "), " me ")]
represents p.me
Descendant Equivalent
//div//p
represents div p
Child Equivalent
//div/p
represents div > p
Adjacent Sibling Equivalent
//h1/following-sibling::div
represents h1 + div
General Sibling Equivalent
//h1/following-sibling::*[count(div)]
represents h1 ~ div
CSS3 / jQuery Equivalents
edit:nth-last-child(n) query
//*[count(child::node() ) > 0]
represents hasChildNodes
:root query
/*[1]
represents :root
:first-child query
descendant::*[1]
represents :first-child
:last-child query
//*[last()]
represents :last-child
:nth-last-child(n) query
//*[count(*)=1]
represents :only-child
:empty query
//*[count(*) = 0]
represents :empty
:nth-child(n) query
//*[position() mod n = 1]
represents :nth-child(n)
:nth-child(odd) query
//*[(position() mod 2)=1]
represents :nth-child(odd)
:nth-child(even) query
//*[(position() mod 2)=0]
represents :nth-child(even)
:nth-last-child(n) query
//*[(count() - position()) mod n = 1]
represents :nth-last-child(n)
:nth-of-type(n) query
//p[n]
represents :nth-of-type(n)
:nth-last-of-type(n) query
//p[(count() - position()) mod n = 1]
represents :nth-last-of-type(n)
:first-of-type query
descendant::p[1]
represents :first-of-type
:last-of-type query
//p[last()]
represents :last-of-type
:only-of-type query
//p[count(*)=1]
represents :only-of-type
-moz-any/-webkit-any query
[local-name()='h1' or local-name()='h4']/node()
represents -moz-any(h1,h4) *
DOM Equivalents
edit//mytag
represents getElementsByTagName
//*[@class=$myclass]
represents getElementsByClassName
//*
represents childNodes
preceding-sibling::*[1]
represents previousSibling
following-sibling::*[1]
represents nextSibling
./following-sibling::*
represents generalSibling
Conditional Logic Equivalents
edit-A predicate is like a SQL where clause
-A pipe is like a SQL union clause
-An axis is like a SQL t1.col = t2.col
-Use a predicate with a boolean variable check as an if statement
-Use a pipe with a tagname search as a range checker
//h2 | //h3 | //h4
-Use a pipe with a negation predicate variable check as an else statement
//var[1] | //var[not //var]
-Use a repeating axis to skip levels in the tree to retrieve nodes at every other branch
child::*/child::*
-Use variables to store individual checks within complex and/or conditional tests
-Use variables within loops to store iteration dependent variables and separate the logic from the output
-Use string-length to test the existence of functions
-Use separate tests like (a and c) or (b and c) instead of nested conditions like ((a or b) and c)
-Use string(.), local-name(.),string-length(concat(., '') ), number(.), and boolean(.)[boolean(.)] to test node values, names and existence
-Use //node()[local-name(.) = $myvar] to test for the existence of form values
-Use //node()[local-name(.) = $myvar][boolean(node())] to skip empty form values
-Always skip empty nodes when debugging
-Use local-name(.)[boolean(.)] to test for empty tags in the context node
-Use boolean(@*[not aa or not bb]) to filter known attributes
-Use {boolean((string$myvar))} to test interpolated variables
-Use boolean(following::*[1] or following::. or following::self::*) to test closing tag failures
-Use count(//*[1 | last() = 1]) to count nodes with only one child
-Use (table1 | table2)[col=val]/* to do a join
-Use *[not(@*)] to return nodes that have no attributes
-Use number(.) - number(.) to suppress number values
-Use substring(., 0, string-length(.) ) to suppress string values
-Use substring('0', 1, not($myvar) ) to set an undefined variable to zero
-Use normalize-whitespace($myvar) to remove tab characters in lists
-Use translate($myvar, $ABCvar, $abcvar) with variables storing A-Z and a-z to ignore case for node queries
-Use string-length instead of contains when checking for list position to make logic data independent
-Use use-attribute-sets to share boilerplate arguments with multiple elements, such as table or list rows
-Use * or self::* whenever selecting to return a nodeset or single node
-Use string(.) to get a node from an XML file called with the document function
-Use different delimiters between each list item to make substring-before/after logic more readable
-Use string concatenation of node/class names with counter/node values to generate ID attributes
-Use a variable to store the previous index value with a comma to make substring-after work with recursion
-Use a predicate with the boolean value of the node as a guard operator
smyvar[$myvar]
-Use XSL nodes to store local values and test XML nodes in predicates with a pipe to emulate a default operator $dynamic[$var] | $default[not($var)]
-Use the child axis instead of slash for grouping child nodes
child::(boy | girl)
-Avoid (*) because it walks the tree before testing child nodes
-Use node() instead of . when searching all elements using //
-Use a predicate check of the generate-id of the node itself and a node variable to do intersect and except set operations
-Use concat with a node and a dummy param to check for node existence
-Use string(number(.))=NaN to check existence of numeric node values
-Use not($a=$b) instead of $a !=$b when comparing variables that contain more than one node
-Use newlines after parentheses to avoid leaving one open ended
-Use predicates to check if form field names exist in the XML doc
-Use qname to get the namespace binding of a tag
-Use id() to get generate-id values instead of variable interpolation