XPATH WITH PHP AND JS

XPATH WITH PHP AND JS

PHPUG Cologne , 2014-08-08

3f2fb8bbcd44609346e1cc0c06d0a39b?s=128

Thomas Weinert

August 08, 2014
Tweet

Transcript

  1. XPATH WITH PHP AND JS @ThomasWeinert

  2. XML Wellformed XML Nodes Location Paths Expressions PHP JavaScript Axes

    Namespaces
  3. XML Syntax for data structures/formats Human readable Machine readable Mixable

    (Namespaces)
  4. WELLFORMED XML < ? x m l v e r

    s i o n = " 1 . 0 " e n c o d i n g = " u t f - 8 " ? > < s u g g e s t i o n s > < s u g g e s t i o n > J S O N < / s u g g e s t i o n > < s u g g e s t i o n > X M L < / s u g g e s t i o n > < s u g g e s t i o n > Y A M L < / s u g g e s t i o n > < / s u g g e s t i o n s >
  5. NODES Element Text Processing instruction Attributes Comment CDataSection Document

  6. WHITESPACES

  7. LOCATION PATHS //suggestion /suggestions/suggestion

  8. EXPRESSIONS //suggestion count(/suggestions/suggestion) string(/suggestions/suggestion[starts-with(., 'X')])

  9. PHP - SCALAR $ d o m = n e

    w D O M D o c u m e n t ( ) ; $ d o m - > l o a d X m l ( $ x m l ) ; $ x p a t h = n e w D O M X p a t h ( $ d o m ) ; v a r _ d u m p ( $ x p a t h - > e v a l u a t e ( ' c o u n t ( / s u g g e s t i o n s / s u g g e s t i o n ) ' ) ) ; f l o a t ( 3 )
  10. PHP - NODES $ d o m = n e

    w D O M D o c u m e n t ( ) ; $ d o m - > l o a d X m l ( $ x m l ) ; $ x p a t h = n e w D O M X p a t h ( $ d o m ) ; v a r _ d u m p ( $ x p a t h - > e v a l u a t e ( ' / / s u g g e s t i o n ' ) ) ; o b j e c t ( D O M N o d e L i s t ) # 6 ( 1 ) { [ " l e n g t h " ] = > i n t ( 3 ) }
  11. JAVASCRIPT - SCALAR c o n s o l e

    . l o g ( d o m . e v a l u a t e ( ' c o u n t ( / s u g g e s t i o n s / s u g g e s t i o n ) ' , d o m , n u l l , X P a t h R e s u l t . A N Y _ T Y P E , n u l l ) . n u m b e r V a l u e ) ; 3
  12. JAVASCRIPT - NODES v a r n o d e

    s = d o m . e v a l u a t e ( ' / / s u g g e s t i o n ' , d o m , { } , X P a t h R e s u l t . A N Y _ T Y P E , n u l l ) ; v a r n o d e ; w h i l e ( n o d e = n o d e s . i t e r a t e N e x t ( ) ) { c o n s o l e . l o g ( n o d e . t e x t C o n t e n t ) ; } J S O N X M L Y A M L
  13. JAVASCRIPT - CREATING A DOM The 'document' object XMLHttpRequest.responseXml DOMParser

    v a r p a r s e r = n e w D O M P a r s e r ( ) ; v a r d o m = p a r s e r . p a r s e F r o m S t r i n g ( x m l , ' t e x t / x m l ' ) ;
  14. AXES

  15. SELF::

  16. CHILD::

  17. DESCENDANT::

  18. DESCENDANT-OR-SELF::

  19. PARENT::

  20. ANCESTOR::

  21. ANCESTOR-OR-SELF::

  22. PRECEDING-SIBLING::

  23. FOLLOWING-SIBLING::

  24. PRECEDING::

  25. FOLLOWING::

  26. AXES - EXTENDED /suggestions/suggestion /child::suggestions/child::suggestion //suggestion /descendant::suggestion

  27. AXES - EXAMPLES //p[ancestor::footer] count(preceding-sibling::li)

  28. XML WITH NAMESPACES < ? x m l v e

    r s i o n = " 1 . 0 " e n c o d i n g = " U T F - 8 " ? > < a t o m : f e e d x m l n s : a t o m = " h t t p : / / w w w . w 3 . o r g / 2 0 0 5 / A t o m " x m l n s = " h t t p : / / w w w . w 3 . o r g / 1 9 9 9 / x h t m l " > < a t o m : e n t r y > < a t o m : t i t l e > A t o m - P o w e r e d R o b o t s R u n A m o k < / a t o m : t i t l e > < a t o m : s u m m a r y t y p e = " t e x t / x h t m l " > < p > S o m e t e x t . < / p > < / a t o m : s u m m a r y > < / a t o m : e n t r y > < / a t o m : f e e d >
  29. XML WITH NAMESPACES - EXPANDED < ? x m l

    v e r s i o n = " 1 . 0 " e n c o d i n g = " U T F - 8 " ? > < { h t t p : / / w w w . w 3 . o r g / 2 0 0 5 / A t o m } : f e e d > < { h t t p : / / w w w . w 3 . o r g / 2 0 0 5 / A t o m } : e n t r y > < { h t t p : / / w w w . w 3 . o r g / 2 0 0 5 / A t o m } : t i t l e > A t o m - P o w e r e d R o b o t s R u n A m o k < / { h t t p : / / w w w . w 3 . o r g / 2 0 0 5 / A t o m } : t i t l e > < { h t t p : / / w w w . w 3 . o r g / 2 0 0 5 / A t o m } : s u m m a r y t y p e = " t e x t / x h t m l " > < { h t t p : / / w w w . w 3 . o r g / 1 9 9 9 / x h t m l } : p > S o m e t e x t . < / p > < / { h t t p : / / w w w . w 3 . o r g / 2 0 0 5 / A t o m } : s u m m a r y > < / { h t t p : / / w w w . w 3 . o r g / 2 0 0 5 / A t o m } : e n t r y > < / { h t t p : / / w w w . w 3 . o r g / 2 0 0 5 / A t o m } : f e e d >
  30. PHP $ d o m = n e w D

    O M D o c u m e n t ( ) ; $ d o m - > l o a d X m l ( $ x m l ) ; $ x p a t h = n e w D O M X p a t h ( $ d o m ) ; $ x p a t h - > r e g i s t e r N a m e s p a c e ( ' a t o m ' , ' h t t p : / / w w w . w 3 . o r g / 2 0 0 5 / A t o m ' ) ; v a r _ d u m p ( $ x p a t h - > e v a l u a t e ( ' s t r i n g ( / / a t o m : e n t r y / a t o m : t i t l e ) ' ) ) ; s t r i n g ( 2 8 ) " A t o m - P o w e r e d R o b o t s R u n A m o k "
  31. JAVASCRIPT v a r x m l n s =

    { n a m e s p a c e s : { ' a t o m ' : ' h t t p : / / w w w . w 3 . o r g / 2 0 0 5 / A t o m ' } , l o o k u p N a m e s p a c e U R I : f u n c t i o n ( p r e f i x ) { i f ( p r e f i x = = ' ' ) r e t u r n n u l l ; r e t u r n t h i s . n a m e s p a c e s [ p r e f i x ] | | n u l l ; } } ; e x p r = ' s t r i n g ( / / a t o m : e n t r y / a t o m : t i t l e ) ' ; c o n s o l e . l o g ( d o m . e v a l u a t e ( e x p r , d o m , x m l n s , X P a t h R e s u l t . A N Y _ T Y P E , n u l l ) . s t r i n g V a l u e ) ; A t o m - P o w e r e d R o b o t s R u n A m o k
  32. THE END THOMAS.WEINERT.INFO