Stein XML 2.1 XML a first course Part 2 Yaakov J. Stein Chief Scientist RAD Data Communications.

Slides:



Advertisements
Similar presentations
XML-XSL Introduction SHIJU RAJAN SHIJU RAJAN Outline Brief Overview Brief Overview What is XML? What is XML? Well Formed XML Well Formed XML Tag Name.
Advertisements

The Document Object Model
JavaScript and the DOM Les Carr COMP3001 Les Carr COMP3001.
1 XSLT – eXtensible Stylesheet Language Transformations Modified Slides from Dr. Sagiv.
Document Object Model. Lecture 18 The Document Object Model (DOM) is not a programming language It is an object-oriented model of web documents Each.
Document Object Model (DOM): An Abstract Data Structure for XML data Alex Dekhtyar Department of Computer Science University of Kentucky.
XML DOM and SAX Parsers By Omar RABI. Introduction to parsers  The word parser comes from compilers  In a compiler, a parser is the module that reads.
XP 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial 10.
Document Object Model (DOM): An Abstract Data Structure for XML data Alex Dekhtyar Department of Computer Science CSC 560: Management of XML Data.
Tutorial 11 Creating XML Document
XML Technologies and Applications Rajshekhar Sunderraman Department of Computer Science Georgia State University Atlanta, GA 30302
September 15, 2003Houssam Haitof1 XSL Transformation Houssam Haitof.
MC 365 – Software Engineering Presented by: John Ristuccia Shawn Posts Ndi Sampson XSLT Introduction BCi.
Technical Track Session XML Techie Tools Tim Bornholt.
Processing of structured documents Spring 2003, Part 5 Helena Ahonen-Myka.
DHTML. What is DHTML?  DHTML is the combination of several built-in browser features in fourth generation browsers that enable a web page to be more.
4.1 JavaScript Introduction
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
1 XML Data Management 4. Domain Object Model Werner Nutt.
HTML DOM.  The HTML DOM defines a standard way for accessing and manipulating HTML documents.  The DOM presents an HTML document as a tree- structure.
JS: Document Object Model (DOM)
XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks.
5 Processing XML Parsing XML documents  Document Object Model (DOM)  Simple API for XML (SAX) Class generation Overview.
XP 1 CREATING AN XML DOCUMENT. XP 2 INTRODUCING XML XML stands for Extensible Markup Language. A markup language specifies the structure and content of.
XP New Perspectives on XML, 2 nd Edition Tutorial 10 1 WORKING WITH THE DOCUMENT OBJECT MODEL TUTORIAL 10.
Extensible Markup and Beyond
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
JavaScript & DOM Client-side scripting. JavaScript JavaScript is a tool to automate client side (which is implemented using HTML so far) JavaSript syntax.
DOM and JavaScript Aryo Pinandito.
CSS Class 7 Add JavaScript to your page Add event handlers Validate a form Open a new window Hide and show elements Swap images Debug JavaScript.
SEG3210 DHTML Tutorial. DHTML DHTML is a combination of technologies used to create dynamic and interactive Web sites. –HTML - For creating text and image.
1 CIS336 Website design, implementation and management (also Semester 2 of CIS219, CIS221 and IT226) Lecture 6 XSLT (Based on Møller and Schwartzbach,
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
Advanced Java Session 9 New York University School of Continuing and Professional Studies.
Parsing with DOM using MSXML Kanda Runapongsa Dept. of Computer Engineering Khon Kaen University.
DOM Robin Burke ECT 360. Outline XHTML in Schema JavaScript DOM (MSXML) Loading/Parsing Transforming parameter passing DOM operations extracting data.
The XML Document Object Model (DOM) Aug’10 – Dec ’10.
Lesson13. JavaScript JavaScript is an interpreted language, designed to function within a web browser. It can also be used on the server.
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
5.2 DOM (Document Object Model). 2 Motto: To write it, it took three months; to conceive it three minutes; to collect the data in it — all my life. —F.
1 Chapter 10: XML What is XML What is XML Basic Components of XML Basic Components of XML XPath XPath XQuery XQuery.
1 XSLT An Introduction. 2 XSLT XSLT (extensible Stylesheet Language:Transformations) is a language primarily designed for transforming the structure of.
ECA 228 Internet/Intranet Design I XSLT Example. ECA 228 Internet/Intranet Design I 2 CSS Limitations cannot modify content cannot insert additional text.
CITA 330 Section 6 XSLT. Transforming XML Documents to XHTML Documents XSLT is an XML dialect which is declared under namespace "
XSLT Kanda Runapongsa Dept. of Computer Engineering Khon Kaen University.
1 Introduction  Extensible Markup Language (XML) –Uses tags to describe the structure of a document –Simplifies the process of sharing information –Extensible.
Introduction to XML This presentation covers introductory features of XML. What XML is and what it is not? What does it do? Put different related technologies.
C# and Windows Programming XML Processing. 2 Contents Markup XML DTDs XML Parsers DOM.
1 Dr Alexiei Dingli XML Technologies SAX and DOM.
SDPLNotes 3.2: DOM1 3.2 Document Object Model (DOM) n How to provide uniform access to structured documents in diverse applications (parsers, browsers,
Introduction to the Document Object Model Eugenia Fernandez IUPUI.
XSLT. XSLT stands for Extensible Stylesheet Language Transformations XSLT is used to transform XML documents into other kinds of documents. XSLT can produce.
XML DOM.
XP New Perspectives on XML, 2 nd Edition Tutorial 7 1 TUTORIAL 7 CREATING A COMPUTATIONAL STYLESHEET.
Making dynamic pages with javascript Lecture 1. Java script java versus javascript Javascript is a scripting language that will allow you to add real.
COS 125 DAY 20. Agenda Assignment 8 not corrected yet Assignment 9 posted  Due April 16 New course time line Discussion on Scripts 
7-Mar-16 Simple API XML.  SAX and DOM are standards for XML parsers-- program APIs to read and interpret XML files  DOM is a W3C standard  SAX is an.
Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Chapter 7 Representing Web Data:
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
XML Schema – XSLT Week 8 Web site:
XML Notes taken from w3schools. What is XML? XML stands for EXtensible Markup Language. XML was designed to store and transport data. XML was designed.
Rendering XML Documents ©NIITeXtensible Markup Language/Lesson 5/Slide 1 of 46 Objectives In this session, you will learn to: * Define rendering * Identify.
21-Jun-16 Document Object Model DOM. SAX and DOM SAX and DOM are standards for XML parsers-- program APIs to read and interpret XML files DOM is a W3C.
 2001 Prentice Hall, Inc. All rights reserved. Outline 1 JavaScript.
THE DOM.
DHTML.
In this session, you will learn to:
Unit 4 Representing Web Data: XML
Introduction to the Document Object Model
DOM Robin Burke ECT 360.
Presentation transcript:

Stein XML 2.1 XML a first course Part 2 Yaakov J. Stein Chief Scientist RAD Data Communications

Stein XML 2.2 Course Objectives XML what and why? Well-formed XML –Displaying XML in IE Valid XML and DTDs Parsing XML using JavaScript Processing XML using XSL

Stein XML 2.3 XML Parsing XML using JavaScript

Stein XML 2.4 XML Parsers All XML parsers MUST check for well-formed input Some XML parsers are validating, others nonvalidating There are two XML parser “philosophies” Event driven parsers (SAX) –Fast and small memory footprint –Output parsing results on-the-fly –Application must store information it needs –Can use stack to track hierarchy Tree parsers (DOM) –Slow and large memory footprint –Build full tree first, then user can traverse tree –Exploit “Object Oriented” languages

Stein XML 2.5 SAX Simple API for XML (present version SAX 2.0) Not developed by W3C BUT de-facto standard Versions for Java (Apache Xerces parser), C++, VB, Python, Perl some ContentHandler methods (callbacks) void setDocumentLocator (Locator locator) supplies application with event location void startDocument() throws SAXException receive notification of XML beginning void endDocument() throws SAXException receive notification of XML end void startElement (…) throws SAXException receive notification of element start tag void endElement (…) throws SAXException receive notification of element end tag void characters (…) throws SAXException receive notification of text void ignorableWhitespace(…) throws SAXException receive notification of space Example to be or not to be startElement quote characters “to be” startElement bold characters “or” endElement bold characters “not to be” endElement quote

Stein XML 2.6 Document Object Model DOM - API that provides access to XML/HTML document structure - Enables reading, deleting, changing, adding elements/attributes There is a good match between XML and tree hierarchy and object oriented programming vehicles vehicles.airplanes vehicles.motor_vehicles vehicles.motor_vehicles.trucks vehicles.motor_vehicles.cars vehicles.bicycles vehicles airplanes motor_vehicles bicycles truckscars

Stein XML 2.7 Nodes The basic unit in the DOM tree is the Node object Nodes that are not null also implement more specialized interfaces Node properties nodeName (readonly String) nodeType (readonly unsigned short) nodeValue (String) attributes (readonly NamedNodeMap) parentNode (readonly Node) childNodes (readonly NodeList) firstChild (readonly Node) lastChild (readonly Node) previousSibling (readonly Node) nextSibling (readonly Node) ownerDocument (readonly Document) prefix (String) localName (readonly String) namespaceURI (readonly String) Node methods boolean hasChildNodes() Node cloneNode(…) Node appendChild(…) Node removeChild(…) Node replaceChild(…) Node insertBefore(…) void normalize() boolean hasAttributes() boolean isSupported(…)

Stein XML 2.8 Node Types The W3C DOM defines the following types (as constants in the Node object - but IE doesn’t implement) constant’s name nodeName nodeValue data type 1.ELEMENT_NODE tag’s name null Element 2.ATTRIBUTE_NODE attribute’s name value Attr 3.TEXT_NODE #text text Text 4.CDATA_SECTION_NODE #cdata_section text CDATASection 5.ENTITY_REFERENCE_NODE referenced name null EntityRerence 6.ENTITY_NODE entity’s name null Entity 7.PROCESSING_INSTRUCTION_NODE PI’s target rest of PI ProcessingInstruction 8.COMMENT_NODE #comment text Comment 9.DOCUMENT_NODE #document null Document 10.DOCUMENT_TYPE_NODE dtd name null DocumentType 11.DOCUMENT_FRAGMENT_NODE #document-fragment null DocumentFragment 12.NOTATION_NODE notation’s name null Notation

Stein XML 2.9 Elements Element nodes have the following properties and methods (for full list see W3C site) Property tagName (readonly String) Methods boolean hasAttribute(String name) String getAttribute(String name) void setAttribute(String name, String value) Attr getAttributeNode (String name) Attr setAttributeNode(Attr newAttr) void removeAttribute(String name) Attr removeAttributeNode(Attr oldAttr) NodeList getElementsbyTagName(String name)

Stein XML 2.10 Attributes Attr nodes have the following properties (no methods) Properties name (readonly String) ownerElement (readonly Element) specified (readonly boolean) value (String)

Stein XML 2.11 NodeList and NamedNodeMap NodeList is an array of nodes Node.childNodes Property length (readonly unsigned long) Method Node item (unsigned long index) nl.item(k) is the same as nl[k] NamedNodeMap is a collection of Nodes indexed by names Property Node.Attributes length (readonly unsigned long) Methods Node item(unsigned Long index) Node getNamedItem(name) Node setNamedItem(…) Node removeNamedItem(name)

Stein XML 2.12 Character Data CharacterData nodes are the father of text and comment nodes text nodes are the father of CDATASection nodes Properties data (String) length (readonly unsigned long) Methods appendData() deleteData() insertData() replaceData() substringData() Node CharacterData TextComment CDATASection

Stein XML 2.13 Document Document nodes are needed to start everything Properties documentElement (readonly Element) root element of xml Doctype (readonly DocumentType) dtd Methods Element createElement(name) Attr createAttribute(name) Text createTextNode(…) Comment createComment(…) createEntityReference(…) createCDATASection(…) createProcessingInstruction(…) createDocumentFragment(…) Element getElementById(id) NodeList getElementsByTagName(name) createNodeIterator(…) createTreeWalker(…)

Stein XML 2.14 Parsing with JavaScript There are DOM interfaces for many (object oriented) languages –Java –JavaScript, ECMAScript, Jscript –C++ –VBScript It is easier to use a scripting language –Many required features are pre-programmed –Interpreted, not compiled –Platform independent JavaScript runs only inside a browser JavaScript is easier that Java which is easier than C++ (kids use it!) JavaScript is FUN (kids use it!)

Stein XML 2.15 How to use JavaScript Use JavaScript by placing script tags in HTML document internal javascript code or URL You can place SCRIPT tag anywhere, in HEAD or in BODY It is recommended to hide scripts from older non-scripting browsers <!-- HTML COMMENT // JAVASCRIPT COMMENT --> This page requires a modern browser!

Stein XML 2.16 Quick overview of JavaScript ECMAscript, see ECMA-262 Object oriented (object has properties, methods and events) Loosely typed (string (default), numbers, boolean) functions with arguments (not checked even for number) optional return value var declares local scope new allocates object don’t need ; Operators (numbers,strings) - * / % (mod) >= == != ~ (bit negation) ! && || ?: (conditional), NaN infinity Flow if if/else while for (c-like) for/in continue break return with Math PI E SQRT2 abs ceil floor round max min sqrt pow eval sin cos tan acos asin atan exp log random Date WeekDay DayFromTime DaysInYear etc.etc.etc.

Stein XML 2.17 Javascript Events EVENTS Onclick Mouse click Ondblclick Mouse double click onmouseover Mouse enters an element onmouseout Mouse leaves an element onmousemove Mouse moves onmousedown Mouse button is pressed onmouseup Mouse button is released onkeypress Visible character is pressed onkeydown Key is pressed onkeyup Key is released onload Document has finished loading onblur Element loses the focus onfocus Element gains the focus

Stein XML 2.18 Javascript Example function hi() { with (hello.style) { posLeft=event.clientX; posTop=event.clientY; } } function flying() { with (fly.style) { if (posLeft<300) { posLeft+=5; posTop+=5; } else { posLeft=10; posTop=10; } } setTimeout('flying()',10); } Hello World! I'm Flying!!! DHTML

Stein XML 2.19 XML Islands What happens when we define an XML island inside an HTML file ? XML Island Demo Nothing happens - the XML is in the DOM, but the browser doesn’t know what to do! (When we directly display an XML file HTML uses a default XSL) We have to manually extract from the XML DOM and insert it into the browser window as HTML!

Stein XML 2.20 An IE specific-feature XML islands are Microsoft-specific, and Microsoft supplies some non-standard ways of retrieving info XML island Demo printout Hello world! printout Hello world!

Stein XML 2.21 Javascript to the rescue Using javascript we can access the XML DOM in a standard way! Hello world! XML DOM Demo alert(hellodata.xml) document.write(hellodata.xml) alert displays the DOM object write displays the text (suppresses tags)

Stein XML 2.22 Let’s try a more interesting file! ASM-20 products/family/asm-20/asm-20.htm 4-wire D1 synchronous unmanaged V Try alert and document.write !!!

Stein XML 2.23 Javascript Access to DOM What happens when we walk through the DOM tree? // main section of DOM (DTD after xsl please!) document.writeln("The document has " + modemdata.childNodes.length + " sections. ") for (n=0;n<modemdata.childNodes.length;n++) { document.writeln( " " + n + " " + " nodeType=" + modemdata.childNodes(n).nodeType + " nodeName=" + modemdata.childNodes(n).nodeName + " nodeValue=" + modemdata.childNodes(n).nodeValue + " " ) } The document has 5 sections. 0 nodeType=7 nodeName=xml nodeValue=version="1.0“ 1 nodeType=7 nodeName=xml-stylesheet nodeValue=type="text/xsl" href="modems.xsl“ 2 nodeType=8 nodeName=#comment nodeValue= modems.xml 3 nodeType=10 nodeName=modems nodeValue=null 4 nodeType=1 nodeName=modems nodeValue=null the XML tree

Stein XML 2.24 Let’s walk through the real tree! // first get the XML root node var rootnode = modemdata.documentElement // var rootnode = modemdata.childNodes(modemdata.childNodes.length-1) var nmodems = rootnode.childNodes.length document.writeln(" The root is " + rootnode.nodeName + " " + " and it has " + nmodems + " child nodes. ") // now traverse XML tree for (n=0;n<nmodems;n++) { // find the modem var thismodem = rootnode.childNodes(n) document.writeln(" "+ n + ". " + thismodem.nodeName+" ") numfields = thismodem.childNodes.length // print all the child nodes for this modem for (i=0;i<numfields;i++) { document.writeln( " " + i + " " + " " + thismodem.childNodes(i).nodeType + " " + thismodem.childNodes(i).nodeName + " " + thismodem.childNodes(i).text + " ") }

Stein XML 2.25 And the answer is … The root is modems and it has 7 child nodes. 0. copper 0 1 name ASM webpage products/family/asm-20/asm-20.htm 2 1 medium 4-wire 3 1 linecode D1 4 1 sync synchronous 5 1 management unmanaged 6 1 minrate maxrate maxrange interfaces V.24 RS-232 V.35 V.36 X.21 …

Stein XML 2.26 More generally There are more levels and we have to recursively walk through the tree function parseChildren(node) var x = node.childNodes var n = x.length if (n>0) { for (var I=0; I<n; I++) {... parseChildren( x(i) ) } There will usually be attributes (etc) as well We often want to jump to specific nodes, etc We may want to append, delete, change nodes and persist the changes EXERCISE TIME!! See NodeIterator and TreeWalker

Stein XML 2.27 XML Processing XML using XSL

Stein XML 2.28 Stylesheets Stylesheets are commonplace in presentation tools They enable customization, standardization of documents A stylesheets is usually a set of rules describing how different elements are to be displayed For example look of headers font face and size effects (underline, bullets) Use of color Cascaded Style Sheets are used to changes HTML defaults SGML had D ocument S tyle and S emantics S pecification L anguage Based on Scheme (LISP variant) Influenced XSLT’s philosophy, but not its syntax

Stein XML 2.29 CSS We can add style to XML using CSS - just like HTML book {display:block} article {display:block} talk {display:block} title {display:block; background:red; color:yellow; font-size:20pt;} author {color:blue; font-size:20pt;} <?xml-stylesheet type="text/css" href="biblio3.css"?>... But such style is very limited Treatment of tags is not environment dependent Can hide tags (display:none) but can’t sort or filter them CSS is not a full programming language CSS is not XML-based and not extensible

Stein XML 2.30 XSL One can process with procedural languages (e.g. javascript) But instead one can use an XML-based pattern matching language –First step of compilation is XML –Declaritive languages are more suitable for transformation applications XSL eXtensible Stylesheet Language XSL has 2 components XSLT and XSLFO Both are XML applications (can be verified using DTD) XSLT has 2 versions NEW VERSION (MSXML3, IE6?) OLD VERSION (IE5+) XSLT is supported by IE5+ XMLSPY Apache’s Xalan Saxon XP Sablotron Unicorn Xesalt

Stein XML 2.31 XSL Transformations If we are already processing the XML file (XML in XML out) we can do a lot more! Examples: Change tag names (e.g. … to … ) Change attributes to child elements or vice-versa Manipulate fields (including numeric computation) Reorder elements Change entire hierarchical structure Filter elements or SELECT records Hence there are two equivalent opening tags for “embedded” XSL for “standalone” XSLT not in IE XML format conversion

Stein XML 2.32 XSLT Processing XSLT Inputs 2 XML files: XML and XSL Outputs 1 XML file (can be HTML for display) XSLT supports recursion and iteration (it relies on an XML DOM parser) XSLT supports XPath (although IE support is minimal) XSLT supports internationalization (languages) Unfortunately, present-day XSLT processors are limited require tree in memory and are hence limited in database size (write SAX programs for large applications) are relatively slow Processing features: template matching commands value commands extract fields standard programming constructs (e.g. basic math, loops, conditionals) special features (e.g. filtering, sorting) noncommands are passed to output

Stein XML 2.33 Simple XSL Example Bibliography Digital Signal Processing... Y. Stein Critical Temperature... Y. Stein Storage Capacity for Neural Network Models Y. Stein <?xml-stylesheet type="text/css" href="biblio3.css"?>... Bibliography

Stein XML 2.34 template match The heart of XSLT is template matching (triggering) The xsl:template element with the match attribute is used... Put here whatever you want to do! Actually the match attribute’s value is not merely a nodename it is a complex expression matching any of the children of the current node We must always start processing by matching to the document node which is nicknamed / (WARNING - this is NOT the XML root!)...

Stein XML 2.35 Recursion and Iteration At every moment there is a current node We will need to match the current node’s children We can do this by recursion Or by iteration (looping)... When recursing XSL should perform default actions on all the child nodes, but IE doesn’t

Stein XML 2.36 value-of select The explicit value of a node is obtained using where as usual nodename is actually an expression For the current node’s value use “.” Example :

Stein XML 2.37 XPath expressions The expression in match and select attributes are in XPath XPath expressions are NOT XML syntax Here are some XPath goodies / like in directories is both the “top” and hierarchy divider * attribute // any number of intervening levels type() e.g. text(), comment() nodes of a particular type Test brackets [xxx] only nodes with child or attribute which match Examples

Stein XML 2.38 Sorting The for-each element has several ordering options Sorting is specified using the order-by attribute By default ordering is lexicographical (unless explicitly number) and ascending (use - for descending) Multiple keys can be specified (separate by ; ) <xsl:for-each select="copper|fiber" order-by="number(minrate); -interfaces">... There is also a command not implemented by IE Also, you can count with (position in current node)

Stein XML 2.39 Default (IE) XSL... <!DOCTYPE (View source for full doctype... )>

Stein XML 2.40 XSLing on-the-fly By defining two XML islands one for the XML and one for the XSL We can process the XML before displaying it function load() { var result = xmli.transformNode(xsli.documentElement); fakeDiv.innerHTML = result; }

Stein XML 2.41 XML and XSL and Javascript! XSL is great - but it has NO GUI !!!!!! Javascript is great - but it is tedious to use Idea: process XML with XSL use HTML buttons, forms, etc. events trigger Javascript functions Javascript changes XSL in DOM XSL retransforms XML to HTML

Stein XML 2.42 XSML+XSL+JS Example function load() { var result = xmli.transformNode(xsli.documentElement); fakeDiv.innerHTML = result; } function change(value) { // parse XSL and make changes load() } 0 1

Stein XML 2.43 A Example modems.xml -->... modems.xsl find.html... function load()... function selectmedium(key)... function selectman(key)... function inputrate(key)... function inputrange(key)......