130 likes | 227 Views
Explore XPath language to locate data in XML documents through tree views and path syntax. Learn about steps, paths, operators, syntax rules, and practical examples. Discover how to process text values efficiently in XSLT.
E N D
XPath and trees Elena Pierazzo 2006-2007
XPath • A language to describe how to locate a part of an XML document • Used in many XML-based technologies and tools
XML documents: a tree view <p>Here is some <hi type="ital">highlighted</hi> text.</p> node link
Or a genealogical chart Parent of <p> Ancestor of <hi>, @type, text nodes Child of / Siblings, descendant of /
Other examples Let’s see some more examples
Paths and Steps one step’s path <xsl:template match=“section”> <xsl:template match=“section/title”> / step operator (= child of) two steps’ path
Abbreviated Syntax • / = child of E.g.: “section/title” Not the same as: /section/title • // = descendant of E.g.: “body//title” • * = any node E.g.: body/*/title • . = current node E.g.: .//para Not the same as //para
XPath at work <AUTHOR TYPE="main"> <NAME><FIRST>Professor Christopher</FIRST><LAST>Gantley</LAST></NAME> <AFFIL>University of Northumbria</AFFIL> <EMAIL>c.gantley@unnx.ac.uk</EMAIL></AUTHOR><AUTHOR> <NAME><FIRST>Megan</FIRST><LAST>Graham</LAST></NAME> <AFFIL>Institute of Image Research, University of Northumbria</AFFIL> <EMAIL>megan.graham@unnx.ac.uk</EMAIL></AUTHOR>
We want obtain: Gantley, Professor Christopher <xsl:template match="NAME"><Ii><xsl:apply-templates select="LAST"/>, <xsl:apply-templates select="FIRST"/></i></xsl:template> Children of the contextual node Contextual node
The select attribute • Has different meaning! • <xsl:template match=“node and descendant”> • <xsl:apply-templates select=“selected node”> • <xsl:value-of select=“text”> forthcoming!
Processing the text: value-of As it was: http://www.cch.kcl.ac.uk/legacy/teaching/av2001/xslt/class_mats/chap1/examples/eeyore.xsl How we can making it better: <xsl:template match="story"><html><head><title>Winnie-the-Pooh: <xsl:value-of select="/story/title"/></title></head><body><xsl:apply-templates/></body></html></xsl:template> Just text, not other nodes
More examples • XML document: chap 2 > examples > abst10.xml • "Finished" template: chap 2 > examples > abstrs.xsl