XPath Abbreviated Syntax

XPath Abbreviated Syntax

You can take advantage of a number of abbreviations in XPath syntax. Here are the rules:

  • self::node() can be abbreviated as .

  • parent::node() can be abbreviated as ..

  • child:: childname can be abbreviated as childname

  • attribute:: childname can be abbreviated as @ childname

  • /descendant-or-self::node()/ can be abbreviated as //

For example, the location path .//PLANET is short for self::node()/descendant-or-self::node()/child::PLANET . You can also abbreviate the predicate expression [position() = 3] as [3] , [position() = last()] as [last()] , and so on. Using the abbreviated syntax makes XPath location paths a lot easier to use. The following list includes some examples of location paths that use abbreviated syntax:

  • PLANET returns the <PLANET> element children of the context node.

  • * returns all element children of the context node.

  • text() returns all text node children of the context node.

  • @UNITS returns the UNITS attribute of the context node.

  • @* returns all the attributes of the context node.

  • PLANET[3] returns the third <PLANET> child of the context node.

  • PLANET[last()] returns the last <PLANET> child of the context node.

  • */PLANET returns all <PLANET> grandchildren of the context node.

  • /PLANETS/PLANET[3]/ NAME [2] returns the second <NAME> element of the third <PLANET> element of the <PLANETS> element.

  • //PLANET returns all the <PLANET> descendants of the document root.

  • PLANETS//PLANET returns the <PLANET> element descendants of the <PLANETS> element children of the context node.

  • //PLANET/NAME returns all the <NAME> elements that have a <PLANET> parent.

  • . returns the context node itself.

  • .//PLANET returns the <PLANET> element descendants of the context node.

  • .. returns the parent of the context node.

  • ../@UNITS returns the UNITS attribute of the parent of the context node.

  • .//.. returns all parents of a descendant of the context node, and the parent of the context node.

  • PLANET[NAME] returns the <PLANET> children of the context node that have <NAME> children.

  • PLANET[NAME="Venus"] returns the <PLANET> children of the context node that have <NAME> children with text equal to Venus.

  • PLANET[@UNITS = "days"] returns all <PLANET> children of the context node that have a UNITS attribute with value days.

  • PLANET[6][@UNITS = "days"] returns the sixth <PLANET> child of the context node, only if that child has a UNITS attribute with value days. Can also be written as PLANET[@UNITS = "days"][6] .

  • PLANET[@COLOR and @UNITS] returns all the <PLANET> children of the context node that have both a COLOR attribute and a UNITS attribute.

  • "//PLANET[not(.= preceding ::PLANET)]" selects all <PLANET> elements that do not have the same value as any preceding <PLANET> element.

  • *[1][self::NAME] matches any <NAME> element that is the first child of its parent.

  • *[position() lt; 5][@UNITS] matches the first five children of the context node that have a UNITS attribute.



Inside XSLT
Inside Xslt
ISBN: B0031W8M4K
EAN: N/A
Year: 2005
Pages: 196

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net