Web Programming Unit-V

Embed Size (px)

Citation preview

  • 8/6/2019 Web Programming Unit-V

    1/14

    1

    http://www.saif4u.webs.com

    B.COM COMPUTER III YEAR - NOTES

    WEB PROGRAMMING

    INDEX UNIT - V

    S.no: Title Page no:

    1.Extensible Markup Language (XML) Introduction

    02 - 04

    2. Creating XML Documents 05 - 05

    3. XML Style Sheets 06 - 07

    4. Hyperlinks in XML Documents 08 - 09

    5. XML Document Object Model 10 - 11

    6. XML Query Language 12 - 14

  • 8/6/2019 Web Programming Unit-V

    2/14

    2

    http://www.saif4u.webs.com

    Extensible Markup Language (XML) Introduction

    Extensible Markup Language (XML) is a set of rules for encoding documents

    inmachine-readable form. It is defined in the XML 1.0 Specification[4] produced by

    the W3C, and several other related specifications, all gratisopen standards.[5]

    The design goals of XML emphasize simplicity, generality, and usability over

    theInternet.[6] It is a textual data format with strong support via Unicode for the

    languages of the world. Although the design of XML focuses on documents, it is widely

    used for the representation of arbitrary data structures, for example in web services.

    Many application programming interfaces (APIs) have been developed that software

    developers use to process XML data, and several schema systems exist to aid in the

    definition of XML-based languages.

    As of 2009, hundreds of XML-based languages have beendeveloped,[7] including RSS,Atom, SOAP, and XHTML. XML-based formats have

    become the default for most office-productivity tools, including Microsoft Office (Office

    Open XML), OpenOffice.org(OpenDocument), and Apple's iWork.[8]

    XML was designed to transport and store data.

    HTML was designed to display data.

    What is XML?

    XML stands for EXtensible Markup Language XML is a markup language much like HTML XML was designed to carry data, not to display data XML tags are not predefined. You must define your own tags XML is designed to be self-descriptive XML is a W3C Recommendation

    The Difference Between XML and HTML

    XML is not a replacement for HTML.

    XML and HTML were designed with different goals:

    XML was designed to transport and store data, with focus on what data is HTML was designed to display data, with focus on how data looks

    HTML is about displaying information, while XML is about carrying information.

  • 8/6/2019 Web Programming Unit-V

    3/14

    3

    http://www.saif4u.webs.com

    XML Does Not DO Anything

    Maybe it is a little hard to understand, but XML does not DO anything. XML was createdto structure, store, and transport information.

    The following example is a note to Tove, from Jani, stored as XML:

    ToveJaniReminderDon't forget me this weekend!

    The note above is quite self descriptive. It has sender and receiver information, it alsohas a heading and a message body.

    But still, this XML document does not DO anything. It is just information wrapped intags. Someone must write a piece of software to send, receive or display it.

    With XML You Invent Your Own Tags

    The tags in the example above (like and ) are not defined in any XMLstandard. These tags are "invented" by the author of the XML document.

    That is because the XML language has no predefined tags.

    The tags used in HTML are predefined. HTML documents can only use tags defined inthe HTML standard (like

    , , etc.).

    XML allows the author to define his/her own tags and his/her own document structure.

    XML is Not a Replacement for HTML

    XML is a complement to HTML.

    It is important to understand that XML is not a replacement for HTML. In most webapplications, XML is used to transport data, while HTML is used to format and displaythe data.

    My best description of XML is this:

    XML is a software- and hardware-independent tool for carrying information.

  • 8/6/2019 Web Programming Unit-V

    4/14

    4

    http://www.saif4u.webs.com

    XML is a W3C Recommendation

    XML became a W3C Recommendation February 10, 1998.

    XML is Everywhere

    XML is now as important for the Web as HTML was to the foundation of the Web.

    XML is the most common tool for data transmissions between all sorts of applications.

  • 8/6/2019 Web Programming Unit-V

    5/14

    5

    http://www.saif4u.webs.com

    Creating XML Documents

    XML stands for eXtensible Markup Language.

    XML is designed to transport and store data.

    XML is important to know, and very easy to learn.

    XML Document Example

    ToveJaniReminderDon't forget me this weekend!

    Using a Text Editor

    1. Select a text editor such as Notepad or WordPad.2. Create the three-line prologue to ensure the document references the correct

    DTD and XSL stylesheet.3. Review the DTD to determine the required structure of the document. The

    opening tag of the root element must be placed at the beginning of thedocument. In the apartment example, Directory is the root element.

    4. Following the root element, all of the elements and their children must be

    incorporated. These elements can be nested within each other, as seen in theapartment example where the City element contains the Apartment element. Inturn, the Apartment element contains the elements comprising apartment_infoand the Unit element, which contains the elements comprising unit_info. Toreplicate this structure within the XML document, the opening tags for the Cityand Apartment elements are included first.

    5. Once all of the apartments and their associated units have been created, theclosing tags for the City and Directory elements must be included at the end ofthe document.

    6. The XML document should be saved and placed into a Web accessible directory.The directory should also contain the DTD and XSL stylesheets referenced by

    the document

  • 8/6/2019 Web Programming Unit-V

    6/14

    6

    http://www.saif4u.webs.com

    XML Style Sheets

    Displaying XML with CSS

    It is possible to use CSS to format an XML document.

    Below is an example of how to use a CSS style sheet to format an XML document:

    Take a look at this XML file: The CD catalog

    Then look at this style sheet: The CSS file

    Finally, view: The CD catalog formatted with the CSS file

    Displaying XML with XSLT

    XSLT is the recommended style sheet language of XML.

    XSLT (eXtensible Stylesheet Language Transformations) is far more sophisticated thanCSS.

    XSLT can be used to transform XML into HTML, before it is displayed by a browser:

    Display XML with XSLT

    XML XSLTBelgian Waffles$5.95two of our famous Belgian Waffles with plentyof real maple syrup650

    Strawberry Belgian Waffles$7.95light Belgian waffles covered with strawberriesand whipped cream900

  • 8/6/2019 Web Programming Unit-V

    7/14

    7

    http://www.saif4u.webs.com

    Berry-Berry Belgian Waffles$8.95light Belgian waffles covered with anassortment of fresh berries and whipped cream900

    French Toast$4.50thick slices made from our homemadesourdough bread600Homestyle Breakfast$6.95

    two eggs, bacon or sausage, toast, and ourever-popular hash browns950

    weight:bold">- (calories per serving)

    OUTPUT

  • 8/6/2019 Web Programming Unit-V

    8/14

    8

    http://www.saif4u.webs.com

    Hyperlinks in XML DocumentsXLink defines a standard way of creating hyperlinks in XML documents.

    XPointer allows the hyperlinks to point to more specific parts in the XML document.

    What is XLink? XLink is short for XML Linking Language XLink is used to create hyperlinks in XML documents Any element in an XML document can behave as a link XLink supports simple links (like HTML) and extended

    links (for linking multiple resources together) With XLink, the links can be defined outside the linked

    files XLink is a W3C Recommendation

    XLink Syntax

    In HTML, we know (and all the browsers know!) that the element defines ahyperlink. However, this is not how it works with XML. In XML documents, you can usewhatever element names you want - therefore it is impossible for browsers to predictwhat hyperlink elements will be called in XML documents.

    The solution for creating links in XML documents was to put a marker on elements thatshould act as hyperlinks.

    Below is a simple example of how to use XLink to create links in an XML document:

    Visit W3Schools

    Visit W3C

    To get access to the XLink attributes and features we must declare the XLinknamespace at the top of the document.

    The XLink namespace is: "http://www.w3.org/1999/xlink".

  • 8/6/2019 Web Programming Unit-V

    9/14

    9

    http://www.saif4u.webs.com

    The xlink:type and the xlink:href attributes in the elements define that thetype and href attributes come from the xlink namespace.

    The xlink:type="simple" creates a simple, two-ended link (means "click from here to gothere"). We will look at multi-ended (multidirectional) links later.

    The XML Example Document

    Look at the following XML document, "bookstore.xml", that represents a few books:

    As his fifth year at Hogwarts School of Witchcraft andWizardry approaches, 15-year-old Harry Potter is.......

    XQuery Kick Start delivers a concise introductionto the XQuery standard.......

  • 8/6/2019 Web Programming Unit-V

    10/14

    10

    http://www.saif4u.webs.com

    XML Document Object Model

    The Document Object Model (DOM) is a cross-platform and language-independentconvention for representing and interactingwith objects in HTML, XHTML and XML documents.[1] Aspects of the DOM (such as its

    "Elements") may be addressed and manipulated within the syntax of the programminglanguage in use. The public interface of a DOM is specified in its applicationprogramming interface (API).

    What is the DOM?

    The DOM is a W3C (World Wide Web Consortium) standard.

    The DOM defines a standard for accessing documents like XML and HTML:

    "The W3C Document Object Model (DOM) is a platform and language-neutral interfacethat allows programs and scripts to dynamically access and update the content,structure, and style of a document."

    The DOM is separated into 3 different parts / levels:

    Core DOM - standard model for any structured document XML DOM - standard model for XML documents

    HTML DOM - standard model for HTML documents

    The DOM defines the objects and properties of all document elements, andthe methods (interface) to access them.

  • 8/6/2019 Web Programming Unit-V

    11/14

    11

    http://www.saif4u.webs.com

    What is the HTML DOM?The HTML DOM defines the objects and properties of all HTML elements, andthe methods(interface) to access them.

    What is the XML DOM? The XML DOM is:

    A standard object model for XML A standard programming interface for XML Platform- and language-independent A W3C standard

    The XML DOM defines the objects and properties of all XML elements, andthe methods (interface) to access them.

    In other words: The XML DOM is a standard for how to get, change, add, or deleteXML elements.

  • 8/6/2019 Web Programming Unit-V

    12/14

    12

    http://www.saif4u.webs.com

    XML Query Language

    XQuery is a query and functional programming language that is designed to query

    collections of XML data.

    XQuery 1.0 was developed by the XML Query working group of the W3C. The work wasclosely coordinated with the development of XSLT 2.0 by the XSL Working Group; the

    two groups shared responsibility for XPath 2.0, which is a subset of XQuery 1.0. XQuery

    1.0 became a W3C Recommendation on January 23, 2007.

    "The mission of the XML Query project is to provide flexible query facilities to extract

    data from real and virtual documents on the World Wide Web, therefore finally providing

    the needed interaction between the Web world and the database world. Ultimately,

    collections of XML files will be accessed like databases".[1]

    What is XQuery?

    XQuery is the language for querying XML data XQuery for XML is like SQL for databases XQuery is built on XPath expressions XQuery is supported by all major databases XQuery is a W3C Recommendation

    XQuery is About Querying XML XQuery is a language for finding and extracting elements and attributes from XMLdocuments.

    Here is an example of a question that XQuery could solve:

    "Select all CD records with a price less than $10 from the CD collection stored in theXML document called cd_catalog.xml"

    XQuery and XPath XQuery 1.0 and XPath 2.0 share the same data model and support the same functionsand operators. If you have already studied XPath you will have no problems withunderstanding XQuery.

  • 8/6/2019 Web Programming Unit-V

    13/14

    13

    http://www.saif4u.webs.com

    XQuery - Examples of Use XQuery can be used to:

    Extract information to use in a Web Service Generate summary reports Transform XML data to XHTML Search Web documents for relevant information

    XQuery Basic Syntax Rules

    Some basic syntax rules:

    XQuery is case-sensitive

    XQuery elements, attributes, and variables must be valid XML names An XQuery string value can be in single or double quotes An XQuery variable is defined with a $ followed by a name, e.g. $bookstore XQuery comments are delimited by (: and :), e.g. (: XQuery Comment :)

    XQuery Conditional Expressions "If-Then-Else" expressions are allowed in XQuery.

    Look at the following example:

    for $x in doc("books.xml")/bookstore/bookreturn if ($x/@category="CHILDREN")then {data($x/title)}else {data($x/title)}

    Notes on the "if-then-else" syntax: parentheses around the if expression arerequired. else is required, but it can be just else ().

    The result of the example above will be:

    Everyday ItalianHarry PotterLearning XMLXQuery Kick Start

  • 8/6/2019 Web Programming Unit-V

    14/14

    14

    http://www.saif4u.webs.com

    XQuery Comparisons In XQuery there are two ways of comparing values.

    1. General comparisons: =, !=, =

    2. Value comparisons: eq, ne, lt, le, gt, ge

    The difference between the two comparison methods are shown below.

    The following expression returns true if any q attributes have a value greater than 10:

    $bookstore//book/@q > 10

    The following expression returns true if there is only one q attribute returned by theexpression, and its value is greater than 10. If more than one q is returned, an erroroccurs:

    $bookstore//book/@q gt 10

    Examples

    The sample XQuery code below lists the unique speakers in each act of Shakespeare'splay Hamlet, encoded in hamlet.xml

    {for $act in doc("hamlet.xml")//ACTlet $speakers := distinct-values($act//SPEAKER)return

    { string($act/TITLE) }

    { for $speaker in $speakersreturn { $speaker }

    }

    }