XPath Co je to?
XPath je jazyk, který slouží k vyhledávání/výběru elementů pomocí XSLT identifikaci elementů/částí dokumentu pomocí XPointer transformace elementů (XSLT) aj.
Uzly, které se rozlišují: kořenový uzel uzel s elementy uzel s textem uzel s atributy uzel s komentáři uzel se zpracovacími instrukcemi uzel se jmennými prostory
Ukázkový XML soubor <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="people.xsl"?> <!DOCTYPE people [ <!ATTLIST homepage xlink:type CDATA #FIXED "simple" xmlns:xlink CDATA #FIXED "http://www.w3.org/1999/xlink"> <!ATTLIST person id ID #IMPLIED> ]> <people> <person born="1912" died="1954" id="p342"> <name> <first_name>alan</first_name> <last_name>turing</last_name> </name> <!-- Did the word computer scientist exist in Turing's day? --> <profession>computer scientist</profession> <profession>mathematician</profession> <profession>cryptographer</profession> <homepage xlink:href="http://www.turing.org.uk/"/> </person> <person born="1918" died="1988" id="p4567"> <name> <first_name>richard</first_name> <middle_initial>m</middle_initial> <last_name>feynman</last_name> </name> <profession>physicist</profession> <hobby>playing the bongoes</hobby> </person> </people>
Stromová struktura dokumentu
Lokační cesty podstatný výraz XPath min. jeden lokační krok identifikující množinu uzlů v dokumentu Jednoduchý příklad pro kořenový uzel / : <xsl:template match="/"> <html><xsl:apply-templates/></html>
Lokační kroky pro subelementy <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:template match="people"> <xsl:apply-templates select="person"/> <xsl:template match="person"> <xsl:value-of select="name"/> </xsl:stylesheet>
Lokační kroky pro atributy <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:template match="/"> <html> <xsl:apply-templates select="people"/> </html> <xsl:template match="people"> <table> <xsl:apply-templates select="person"/> </table> <xsl:template match="person"> <tr> <td><xsl:value-of select="name"/></td> <td><xsl:value-of select="@born"/></td> <td><xsl:value-of select="@died"/></td> </tr> </xsl:stylesheet> <html> <table> <tr> <td> Alan Turing </td> <td>1912</td> <td>1954</td> </tr> <tr> <td> Richard M Feynman </td> <td>1918</td> <td>1988</td> </tr> </table> </html>
Další lokační kroky comment() text() processing-instruction() <xsl:template match="comment( )"> <i>comment Deleted</i>
Další lokační kroky comment() text() processing-instruction() <xsl:template match="comment( )"> <i>comment Deleted</i> processing-instruction( ) processing-instruction('xml-stylesheet')
Zástupné výrazy * node() @* uzly s elementy: <xsl:template match="*"> <xsl:apply-templates select="*"/> všechny uzly (s elementy, textem, atributy, zprac. instr., jmennými prostory i komentáři) : node() výpis všech atributů elementu person do tagu: <xsl:template match="person"> <attributes><xsl:apply-templates select="@*"/></attributes>
Slučování vzorů pomocí @id @xlink:type * @* všechny uzly s elementy a atributy uzly s textem, komentáři a zprac. instrukcemi <xsl:template match="first_name last_name profession hobby"> <xsl:value-of select="text( )"/>
Složené lokační cesty (1) @název_atributu / comment() text() node() processing-instruction(). je aktuální kontextový uzel // jsou následníci kontext. uzlu
Složené lokační cesty (2) //@id/.. //middle_initial/../first_name <xsl:template match="comment()"> <span class="comment"><i><xsl:value-of select="."></i></span> <xsl:template match="name"> <strong><xsl:value-of select="."></strong> /people/person/name/first_name /people/person/name/first_name/text() <first_name>alan</first_name> <first_name>richard</first_name> Alan Richard
Predikáty pozor! <xsl:template match="//profession[.='physicist']"> //person [profession="physicist"] <,>,=,... //person[@born<=1976] pozor! <xsl:apply-templates select="//person[@born<=1976]"/> and, or //person[@born<=1920 and @born>=1910] /people/person[@born < 1950]/name[first_name = "Alan"]
Nezkrácené lokační cesty osa potomků rodičů sebe sama atributů následníků osa předchůdců osa následujících/předchozích sourozenců osa dalších/předchozích uzlů osa jmenných prostorů osa následníků osa předchůdců včetně sebe sama people/person/@id child::people/child::person/attribute::id
Dále podporujeme výrazy: 3.141529 2+2 'Rosalind Franklin' true( ) 32.5 < 76.2E-21 position()=last( ) atd.
Vychytávky (1) <xsl:value-of select="@id div 10"/> <xsl:template match="profession"> <xsl:if test=".='computer scientist' or.='physicist'"> <xsl:value-of select="."/> </xsl:if> <xsl:template match="profession"> <xsl:choose> <xsl:when test=".='computer scientist'"> <i><xsl:value-of select="."/></i> </xsl:when> <xsl:otherwise> <xsl:value-of select="."/> </xsl:otherwise> </xsl:choose>
Vychytávky (2) Funkce XPath vrací hodnotu: logickou číslo množinu uzlů řetězec
Některé funkce round(číslo) not(@id>400) sum(množina_uzlů) starts-with(last_name,'t') <xsl:apply-templates select="name[starts-with(last_name, 'T')]"/> concat( Zač, át, ek věty ) normalize-space( Řetězec plný mezer ) aj.
XPath nástroje Javy xerces v2: org.apache.xerces.impl.xpath.xpath extends java.lang.object java v5: interface XPath