XML is Dead. Long Live XML! (IPC2013SE 2013-06-04)

XML is Dead. Long Live XML! (IPC2013SE 2013-06-04)

Presentation given at International PHP Conference 2013 Spring Edition in Berlin, Germany.

D6ccd6409910643d05ddaea3b2cd6f13?s=128

David Zuelke

June 04, 2013
Tweet

Transcript

  1. XML IS DEAD. LONG LIVE XML!

  2. David Zülke

  3. David Zuelke

  4. None
  5. http://en.wikipedia.org/wiki/File:München_Panorama.JPG

  6. Lead Architect

  7. None
  8. @dzuelke

  9. TODAY'S MENU • XML (and where it took a wrong

    turn) • HTML 5 (and why it makes me a little bit sad) • JSON (and why it's not as universally useful as you may think) • YAML (and why it makes my eyes bleed)
  10. but first

  11. I'd like to hear from you, dear audience

  12. what do you love, hate, adore or loathe with a

    passion?
  13. XML eXtensible Markup Language

  14. "extensible" mostly refers to the Namespaces concept and the document

    model
  15. READING AND WRITING • Push Parsing, uses events to notify

    app during parsing • Example: SAX • Pull Parsing, allows app to retrieve or skip elements • Example: PHP's ext/xmlreader • DOM, constructs an object representation of the tree • Example: DOM (duh) Use XMLReader::expand()
  16. VALIDATION • XML Schema (validates the grammar of a document)

    • RELAX NG (like XML Schema, just full of win) • Except it's so awesome it sucks at reporting errors :( • Schematron (validates the semantics of a document) • DTD (RRRRRRRAAAAAAAAAAAAAAAAAGGGGEEEE)
  17. OTHER GOODIES • XPath and XQuery for querying documents •

    XPath is an expression syntax, XQuery a full query language • XSLT to transform documents to XML, HTML, text, ... • Makes heavy use of XPath • XIncludes • Allow re-use of content within and across documents <3
  18. but most importantly

  19. XML’s document model is built for extensibility

  20. <?xml  version="1.0"  encoding="utf-­‐8"?> <products  xmlns="http://acme.com/shop/products">    <product  id="123">    

       <name>Bacon</name>        <price>5.99</price>    </product> </products>
  21. //p:product[1]/p:price[1]  =>  “5.99” “p” bound to “http:/ /acme.com/shop/products”

  22. <?xml  version="1.0"  encoding="utf-­‐8"?> <products  xmlns="http://acme.com/shop/products">    <product  id="123">    

       <name>Bacon</name>        <price>5.99</price>        OMNOMNOM  Bacon    </product> </products>
  23. <?xml  version="1.0"  encoding="utf-­‐8"?> <products  xmlns="http://acme.com/shop/products">    <product  id="123">    

       <name>Bacon</name>        <price>5.99</price>    </product> </products>
  24. <?xml  version="1.0"  encoding="utf-­‐8"?> <products  xmlns="http://acme.com/shop/products">    <product  id="123">    

       <name>Bacon</name>        <price  currency="USD">5.99</price>    </product> </products>
  25. //p:product[1]/p:price[1]  =>  “5.99”

  26. <?xml  version="1.0"  encoding="utf-­‐8"?> <products  xmlns="http://acme.com/shop/products">    <product  id="123">    

       <name>Bacon</name>        <price  currency="USD">5.99</price>        <price  currency="EUR">4.49</price>    </product> </products>
  27. //p:product[1]/p:price[1]  =>  “5.99”

  28. <?xml  version="1.0"  encoding="utf-­‐8"?> <products  xmlns="http://acme.com/shop/products">    <product  id="123">    

       <name  xml:lang="en">Bacon</name>        <name  xml:lang="de">Speck</name>        <price  currency="USD">5.99</price>        <price  currency="EUR">4.49</price>    </product> </products>
  29. <?xml  version="1.0"  encoding="utf-­‐8"?> <products  xmlns="http://acme.com/shop/products">    <product  id="123">    

       <name  xml:lang="en">Bacon</name>        <name  xml:lang="de">Speck</name>        <price>5.99</price>        <link  rel="category"  href="..."  />    </product> </products>
  30. XML: doing it right

  31. <?xml  version="1.0"  encoding="utf-­‐8"  standalone="yes"?> <search  xmlns="urn:com.lovefilm.api.search">    <total_results>6</total_results>    <items_per_page>2</items_per_page>

       <start_index>1</start_index>    <link  href="http://openapi.lovefilm.com/catalog/games?start_index=1&amp;items_per_page=2&amp;term=old"                rel="self"  title="self"/>    <link  href="http://openapi.lovefilm.com/catalog/games?start_index=3&amp;items_per_page=2&amp;term=old"                rel="next"  title="next"/>    <link  href="http://openapi.lovefilm.com/catalog/games?start_index=5&amp;items_per_page=2&amp;term=old"                rel="last"  title="last"/>    <catalog_title>        <can_rent>true</can_rent>        <release_date>2003-­‐09-­‐12</release_date>        <title  full="Star  Wars:  Knights  of  the  Old  Republic"  clean="Star  Wars:  Knights  of  the  Old  Republic"/>        <id>http://openapi.lovefilm.com/catalog/title/59643</id>        <adult>false</adult>        <number_of_ratings>574</number_of_ratings>        <rating>4</rating>        <category  scheme="http://openapi.lovefilm.com/categories/catalog"  term="games"/>        <category  scheme="http://openapi.lovefilm.com/categories/format"  term="Xbox"/>        <category  scheme="http://openapi.lovefilm.com/categories/genres"  term="Adventure"/>        <category  scheme="http://openapi.lovefilm.com/categories/genres"  term="Role-­‐playing"/>        <category  scheme="http://openapi.lovefilm.com/categories/certificates/bbfc"  term="TBC"/>        <link  href="http://openapi.lovefilm.com/catalog/title/59643/synopsis"                    rel="http://schemas.lovefilm.com/synopsis"  title="synopsis"/>        <link  href="http://openapi.lovefilm.com/catalog/title/59643/reviews"                    rel="http://schemas.lovefilm.com/reviews"  title="reviews"/>        <link  href="http://www.lovefilm.com/product/59643-­‐Star-­‐Wars-­‐Knights-­‐of-­‐the-­‐Old-­‐Republic.html?cid=LFAPI"                    rel="alternate"  title="web  page"/>    </catalog_title> </search>
  32. which brings me to...

  33. HATEOAS AND HYPERMEDIA The Main Ingredient For Your REST API

  34. XML: doing it wrong

  35. <?xml  version="1.0"  encoding="UTF-­‐8"?> <!DOCTYPE  plist  PUBLIC  "-­‐//Apple  Computer//DTD  PLIST  1.0//EN"

       "http://www.apple.com/DTDs/PropertyList-­‐1.0.dtd"> <plist  version="1.0"> <dict>        <key>DefaultProfileChoices</key>        <dict>                <key>AC  Power</key>                <integer>2</integer>                <key>Battery  Power</key>                <integer>1</integer>                <key>UPS  Power</key>                <integer>1</integer>        </dict>        <key>SystemProfileDefaults</key>        <array> <!-­‐-­‐  Profile  0  -­‐-­‐>                <dict> <!-­‐-­‐  AC  Power  -­‐  unused  -­‐-­‐>                        <key>AC  Power</key>                        <dict>                            <!-­‐-­‐  ...  -­‐-­‐>                        </dict>                </dict>        </array> </dict> </plist>
  36. but please do not serialize objects to XML (I'm looking

    at you, SOAP)
  37. HTML 5 Such a mess :(

  38. HTML 5 • Neither SGML nor XML • so browser

    vendors need to write new (inefficient) parsers • Does not have a DTD anymore • how could it, given how it's not SGML... • Completely idiotic stuff like data-* attributes • And they're even proud of it...
  39. <li  class="user"          data-­‐name="John  Resig"  data-­‐city="Boston"  

           data-­‐lang="js"  data-­‐food="Bacon">    <b>John  says:</b>  <span>Hello,  how  are  you?</span> </li>
  40. but that's already possible in XML!

  41. <li  class="user"          xmlns:myapp="http://myapp.com/v1"      

       myapp:name="John  Resig"  myapp:city="Boston"          myapp:lang="js"  myapp:food="Bacon">    <b>John  says:</b>  <span>Hello,  how  are  you?</span> </li>
  42. JSON JavaScript Object Notation

  43. JSON KEY FEATURES • Quite efficient, simple, universally supported in

    many languages • Very good for serializing objects • Built-in types, do not need explicit declaration • No schemas • No mixed content
  44. win: store arbitrary keys and values

  45. {        "_id"  :  "bc2a41170621c326ec68382f846d5764",      

     "_rev"  :  "2612672603",        "item"  :  "orange",        "prices"  :  {                "Fresh  Mart"  :  1.99,                "Price  Max"  :  3.19,                "Citrus  Circus"  :  1.09        } }
  46. fail: use it as query language for your “web scale”

    database
  47. {  foo:  "bar"  } {  x  :  3,  y  :

     "foo"  } {  $or  :  [  {  a  :  1  }  ,  {  b  :  2  }  ]  } {  j  :  {  $ne:  3  },  k  :  {  $gt:  10  }  } {  x  :  3  },  {  z  :  1  } {  j  :  {  $in  :  [2,4,6]  }  } {  "author.name"  :  "joe"  } {  "addresses.city"  :  "London"  }
  48. EVOLVABILITY Because Things Change Over Time!

  49. <?xml  version="1.0"  encoding="utf-­‐8"?> <product  xmlns="urn:com.acme.prods"  xmlns:atom="http://www.w3.org/2005/xlink">    <id>1234</id>    <name>Red

     Stapler</name>    <atom:link  rel="payment"  type="application/com.acme.shop+xml"                          href="http://acme.com/products/1234/payment"/>    <price>3.14</price> </product> {    id:  1234,    name:  "Red  Stapler",    links:  [        {            rel:  "payment",            type:  "application/vnd.com.acme.shop+json",            href:  "http://acme.com/products/1234/payment"        }    ],    price:  3.14 } XML VERSUS JSON
  50. <?xml  version="1.0"  encoding="utf-­‐8"?> <product  xmlns="urn:com.acme.prods"  xmlns:atom="http://www.w3.org/2005/xlink">    <id>1234</id>    <name>Red

     Stapler</name>    <atom:link  rel="payment"  type="application/com.acme.shop+xml"                          href="http://acme.com/products/1234/payment"/>    <price  currency="EUR">3.14</price> </product> {    id:  1234,    name:  "Red  Stapler",    links:  [        {            rel:  "payment",            type:  "application/vnd.com.acme.shop+json",            href:  "http://acme.com/products/1234/payment"        }    ],    price:  {        amount:  3.14,        currency:  "EUR"    } } XML VERSUS JSON Content (“node value”) still the same Float becomes object, stuff breaks
  51. YAML Yet Another Markup Language

  52. YAML Yet Another Markup Language YAML Ain't Markup Language

  53. recursive acronyms are cool

  54. YAML also was cool at some point

  55. a simple markup language, perfect for configuration files

  56. autoload:    #  plugins    plugins_lib:        name:

                         plugins  lib        path:                      %SF_PLUGINS_DIR%/*/lib        recursive:            true        exclude:                []            plugins_module_lib:        name:                      plugins  module  lib        path:                      %SF_PLUGINS_DIR%/*/modules/*/lib        prefix:                  2        recursive:            true        exclude:                []    #  project    project:        name:                      project        path:                      %SF_LIB_DIR%        recursive:            true        exclude:                [model,  symfony,  deprecated]
  57. but then...

  58. foo:  |          -­‐-­‐-­‐      

     foo:  bar        -­‐-­‐-­‐        yo:  baz bar:  |          fooness         clipped:  |        This  has  one  newline. same  as  "clipped"  above:  "This  has  one  newline.\n" stripped:  |-­‐        This  has  no  newline. same  as  "stripped"  above:  "This  has  no  newline." kept:  |+        This  has  four  newlines. same  as  "kept"  above:  "This  has  four  newlines.\n\n\n\n" -­‐  &showell  Steve -­‐  Clark   -­‐  Brian   -­‐  Oren   -­‐  *showell
  59. YAML DRAWBACKS • Makes your eyes bleed • No schemas

    or other forms of validation • Forget a newline somewhere and the parser might look at you, helpless, like a deer in headlights • No mixed content
  60. CONCLUSION • XML for hypermedia, documents, your new REST API

    • JSON for serializing objects and storing them in e.g. CouchDB • YAML for your configuration files
  61. !e End

  62. Questions?

  63. THANK YOU! This was http://joind.in/8809 by @dzuelke Questions? E-Mail: dz@europeanmedia.com