Upgrade to Pro — share decks privately, control downloads, hide ads and more …

XPATH WITH PHP AND JS

XPATH WITH PHP AND JS

PHPUG Cologne , 2014-08-08

Thomas Weinert

August 08, 2014
Tweet

More Decks by Thomas Weinert

Other Decks in Programming

Transcript

  1. 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 >
  2. 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 )
  3. 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 ) }
  4. 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
  5. 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
  6. 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 ' ) ;
  7. 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 >
  8. 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 >
  9. 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 "
  10. 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