DAX: Where Flowscript
     meets XSLT
     Lars Trieloff, Mindquarry
Lars Trieloff

•   Entrepreneur, Blogger,
    Open Source Coder

•   OSS Projects

    •   Apache Cocoon

    •   Mindquar...
What is DAX

• DAX means Declarative API for XML
• A way to process XML
• By expressing what parts of a document
  you wan...
DAX History
• Feb 2005: Kim Wolk writes XMLTO, a .NET
  library that transforms XML into objects
• March 2005: Ryan Cox po...
DAX Modules and
 Dependencies
      DAX
DAX Modules and
  Dependencies
           DAX




DAX-Java
DAX Modules and
  Dependencies
           DAX




DAX-Java




 dom4j
DAX Modules and
  Dependencies
             DAX



             DAX-
DAX-Java
           Javascript



 dom4j
DAX Modules and
  Dependencies
             DAX



             DAX-
DAX-Java
           Javascript



 dom4j       Rhino
DAX Modules and
  Dependencies
             DAX



             DAX-        DAX-
DAX-Java
           Javascript   Cocoon

...
DAX Modules and
  Dependencies
             DAX



             DAX-        DAX-
DAX-Java
           Javascript   Cocoon

...
DAX-Java
DAX-Java         How to use it
public class ElementCounter extends Transformer {
  Map elements = new Hashmap<String, Inte...
DAX-Java         How to use it
public class ElementCounter extends Transformer {
  Map elements = new Hashmap<String, Inte...
DAX-Java         How to use it
public class SourceCounter extends Transformer {
  Map sources = new Hashmap<String, Intege...
DAX-Java      How it works
 • Simple parsing algorithm:
  • traverse the DOM of the XML document
  • for each node, find an...
DAX-Java                   vs XSLT
                     DAX-Java                             XSLT

            @Path(quot;...
DAX-Javascript
DAX-Javascript    Why?
• XSLT is fine for transforming XML
• but no side-effects possible
• no access to external data mode...
DAX-Javascript       Background
   • Map most important XSLT concepts to
      Javascript concepts
          XSLT         ...
DAX-Javascript    How to use it
<xsl:template match=quot;fooquot;>
  <bar>
    <xsl:comment>example code uses foo</xsl:com...
DAX-Javascript     How to use it
<xsl:template match=quot;node()|@*quot;>
  <xsl:copy>
    <xsl:apply-templates select=quo...
DAX-Javascript   How it works
  • Uses Rhino Javascript Engine
   • full access to Java object model
   • allows side-effe...
DAX-Cocoon
DAX-Cocoon        How to use it

<map:components>
  <map:transformers>
    <map:transformer name=quot;daxquot;
      src=q...
DAX-Cocoon        How to use it
<map:match pattern=quot;/resource/*quot;>
  <map:select type=quot;request-methodquot;>
   ...
DAX-Cocoon        How to use it
var resourcemanager =
  cocoon.getComponent(quot;resourcemanagerquot;);

Stylesheet.templa...
DAX-Cocoon          How it works
 •   Implemented as a Cocoon Transformer
 •   Pull in quot;cocoonquot; object as Flowscri...
DAX-Cocoon           Open Questions

 •   Caching
     •   We do not know if a transformation has
         non-cacheable s...
How to go on?
•   Read more

    •   http://www.asciiarmor.com/2005/03/03/introducing-
        dax-declarative-api-for-xml...
Thank you very much
              lars@trieloff.net




 For more information, see my weblog at
 http://weblogs.goshaky.co...
Upcoming SlideShare
Loading in...5
×

Dax Declarative Api For Xml

2,478

Published on

DAX is a declarative API for processing XML in Java or Javascript that integrates into Cocoon and re-uses important concepts of XPath and XSLT.

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
2,478
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
64
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Dax Declarative Api For Xml

  1. 1. DAX: Where Flowscript meets XSLT Lars Trieloff, Mindquarry
  2. 2. Lars Trieloff • Entrepreneur, Blogger, Open Source Coder • OSS Projects • Apache Cocoon • Mindquarry • Goshaky • DAX
  3. 3. What is DAX • DAX means Declarative API for XML • A way to process XML • By expressing what parts of a document you want to process • Based on Java, Javascript and Cocoon
  4. 4. DAX History • Feb 2005: Kim Wolk writes XMLTO, a .NET library that transforms XML into objects • March 2005: Ryan Cox ports it to Java 5, using Annotations and dom4j‘s Transformer API • 2006 to 2007: DAX is used in production at Mindquarry, adopted to Cocoon
  5. 5. DAX Modules and Dependencies DAX
  6. 6. DAX Modules and Dependencies DAX DAX-Java
  7. 7. DAX Modules and Dependencies DAX DAX-Java dom4j
  8. 8. DAX Modules and Dependencies DAX DAX- DAX-Java Javascript dom4j
  9. 9. DAX Modules and Dependencies DAX DAX- DAX-Java Javascript dom4j Rhino
  10. 10. DAX Modules and Dependencies DAX DAX- DAX- DAX-Java Javascript Cocoon dom4j Rhino
  11. 11. DAX Modules and Dependencies DAX DAX- DAX- DAX-Java Javascript Cocoon dom4j Rhino Cocoon
  12. 12. DAX-Java
  13. 13. DAX-Java How to use it public class ElementCounter extends Transformer { Map elements = new Hashmap<String, Integer>(); public void processElement(Node context) { String name = context.getName(); if (elements.hasKey(name)) { elements.put(name, elements.get(name) + 1); } else { elements.put(name, 1); } } }
  14. 14. DAX-Java How to use it public class ElementCounter extends Transformer { Map elements = new Hashmap<String, Integer>(); @Path(quot;*quot;) //select all elements public void processElement(Node context) { String name = context.getName(); if (elements.hasKey(name)) { elements.put(name, elements.get(name) + 1); } else { elements.put(name, 1); } } }
  15. 15. DAX-Java How to use it public class SourceCounter extends Transformer { Map sources = new Hashmap<String, Integer>(); @Path(quot;img[@src]quot;) //select all elements public void processElement(Node context) { String name = this.valueOf(quot;@srcquot;); if (elements.hasKey(name)) { elements.put(name, elements.get(name) + 1); } else { elements.put(name, 1); } } }
  16. 16. DAX-Java How it works • Simple parsing algorithm: • traverse the DOM of the XML document • for each node, find an annotated method • with matching XPath • execute this method • Just like XSLT's templates
  17. 17. DAX-Java vs XSLT DAX-Java XSLT @Path(quot;/foo/barquot;) Templates public void bar(Node context) <xsl:template match=quot;/foo/barquot;> Value-Of valueOf(quot;@barquot;) <xsl:value-of select=quot;@barquot; /> Apply- applyTemplates() <xsl:apply-templates /> Templates
  18. 18. DAX-Javascript
  19. 19. DAX-Javascript Why? • XSLT is fine for transforming XML • but no side-effects possible • no access to external data model Input XSLT Output ? Model
  20. 20. DAX-Javascript Background • Map most important XSLT concepts to Javascript concepts XSLT Javascript <xsl:stylesheet> Stylesheet object template function of the Stylesheet <xsl:template> object applyTemplate function of the <xsl:apply-templates/> Stylesheet object copy function of the Stylesheet object <xsl:copy/> (with inlined body function)
  21. 21. DAX-Javascript How to use it <xsl:template match=quot;fooquot;> <bar> <xsl:comment>example code uses foo</xsl:comment> <xsl:apply-templates /> </bar> </xsl:template/> Stylesheet.template({match:quot;fooquot;}, function(node) { this.element(quot;barquot;, function(node) { this.comment(quot;example code uses fooquot;); this.applyTemplates(); }); });
  22. 22. DAX-Javascript How to use it <xsl:template match=quot;node()|@*quot;> <xsl:copy> <xsl:apply-templates select=quot;node()|@*quot; /> </xsl:copy> </xsl:template/> Stylesheet.template({match:quot;node()|@*quot;}, function (node) { this.copy(function(node) { this.applyTemplates({select:quot;node()|@*quot;}) }); });
  23. 23. DAX-Javascript How it works • Uses Rhino Javascript Engine • full access to Java object model • allows side-effects when transforming XML • Parses the incoming XML stream • Finds and fires matching functions
  24. 24. DAX-Cocoon
  25. 25. DAX-Cocoon How to use it <map:components> <map:transformers> <map:transformer name=quot;daxquot; src=quot;dax.cocoon.DAXTransformerquot; /> </map:transformers> </map:components>
  26. 26. DAX-Cocoon How to use it <map:match pattern=quot;/resource/*quot;> <map:select type=quot;request-methodquot;> <map:generate type=quot;streamquot; /> <map:when test=quot;PUTquot;> <map:transform type=quot;daxquot; src=quot;dax/res.jsquot;> <map:parameter name=quot;resquot; value=quot;{1}quot; /> </map:transform> </map:when> </map:select> </map:match>
  27. 27. DAX-Cocoon How to use it var resourcemanager = cocoon.getComponent(quot;resourcemanagerquot;); Stylesheet.template({match:quot;delquot;}, function(node) { var that = this; this.copy(function(node) { if (that.valueOf(quot;.quot;)==cocoon.parameters.res) { resourcemanager.delete(that.valueOf(quot;@nodequot;)) } this.applyTemplates({select:quot;node()|@*quot;}) }); });
  28. 28. DAX-Cocoon How it works • Implemented as a Cocoon Transformer • Pull in quot;cocoonquot; object as Flowscript does • Usage Scenario: REST Application • validate using DAX (e.g. by checking database) • transform using DAX (e.g by triggering actions) • save using DAX (e.g. by changing model)
  29. 29. DAX-Cocoon Open Questions • Caching • We do not know if a transformation has non-cacheable side-effects • Mixing DAX and XSLT • perhaps E4X is a way to conveniently embed XSLT • Not all XSLT concepts implemented (sorting)
  30. 30. How to go on? • Read more • http://www.asciiarmor.com/2005/03/03/introducing- dax-declarative-api-for-xml/ • https://www.mindquarry.org/wiki/dax/ • http://www.codeconsult.ch/bertrand/archives/ 000802.html • Download • http://releases.mindquarry.org/dax/ • (Maven artifacts available)
  31. 31. Thank you very much lars@trieloff.net For more information, see my weblog at http://weblogs.goshaky.com/weblog/lars
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×