Step by step answer.
To select elements with ancestor div[@id='content-area'] , which are the second children of their respective parents, use:
//div[@id='content-area']//div[2]
To select the second (in document order) div element using the ancestor div[@id='content-area'] :
(//div[@id='content-area']//div)[2]
Pay attention to the difference.
Then, to select the elements that are the second children of their respective parents, provided that they have the class "media-gallery-item", use:
//div[@id='content-area']//div[2][@class='media-gallery-item']
To select items that are the second of these children (their respective parents) that have the media-gallery-item class:
//div[@id='content-area']//div[@class='media-gallery-item'][2]
To select the second (in document order) div element with ancestor div[@id='content-area'] , providing it with a media-gallery-item class:
(//div[@id='content-area']//div)[2][@class='media-gallery-item']
To select the second (in document order) from the entire div element with the ancestor div[@id='content-area'] and the class media-gallery-item :
(//div[@id='content-area']//div)[@class='media-gallery-item'][2]
Special quotes suggested by @Alejandro:
The predicate filters a node-set off-axis to create a new node-set. For each node in the node-set to filter, PredicateExpr is evaluated using the node as the node context, with the number of nodes in the node-set as the size of the context and the proximity of the node in the node-set with respect to the axis, as the context position
http://w3.org/TR/xpath/#predicates
The proximity location of a node -set element relative to the axis is determined as the position of the node in the node set, ordered in the document order, if the axis is the front axis and ordered in the reverse document order if the axis is the reverse axis
http://w3.org/TR/xpath/#dt-proximity-position
The bottom line is that the position predicate works with respect to the axis. And you need parentheses to explicitly declare priority. Thus, when calculating the position, not the child axis will be considered, but the node-set after resolving the descendant-or-self axis.