Docstoc

Perl_22_XML

Document Sample
Perl_22_XML Powered By Docstoc
					                Chapter 22 – Extensible Markup
                      Language (XML)
        Outline
        22.1           Introduction
        22.2           Perl and XML
        22.3           Structuring Data
        22.4           XML Namespaces
        22.5           Document Type Definitions (DTD) and Schema
                       22.5.1 Document Type Definitions
                       22.5.2 W3C XML Schema
        22.6           Customized Markup Languages
                       22.6.1 MathML
                       22.6.2 WML
                       22.6.3 XBRL
                       22.6.4 ebXML
                       22.6.5 FpML
                       22.6.6 Other Markup Languages
        22.7           Document Object Model (DOM)
        22.8           XML::DOM
        22.9           DOM Methods

 2001 Prentice Hall, Inc. All rights reserved.
                Chapter 22 – Extensible Markup
                      Language (XML)
        Outline
        22.10          Simple API for XML (SAX)
        22.11          Parsing XML with XML::Parser
        22.12          Data::Dumper and XML
        22.13          Extensible Style Language (XSL)
        22.14          Generating XML and Perl
        22.15          Case Study: Message Forums with Perl and XML
                       22.6.1 Displaying the Forums using XML::Parser
                       22.6.2 Using XML::DOM to Add Forums and Messages
                       22.6.3 Alterations for Non-XSL Browsers
        22.16          Internet and World Wide Web Resources




 2001 Prentice Hall, Inc. All rights reserved.
                                            22.1 Introduction

      • XML
              –    Developed in 1996 by the WWW Consortium (W3C)
              –    Describes structured data
              –    Applications decide how to display the data
              –    Can create new markup languages to describe data types
              –    Written in any text editor that supports ASCII and Unicode
              –    All XML files end in .xml
              – Parsers are used to validate XML documents




 2001 Prentice Hall, Inc. All rights reserved.
                                            22.2 Perl and XML

      • XML support
              – Offered through a large number of modules
              – We focus on two:
                     • XML::Parser
                     • XML::DOM




 2001 Prentice Hall, Inc. All rights reserved.
                                      22.3 Structuring Data

      • XML
              – Allows programmers to describe their own tags
              – Provides enormous flexibility with data types




 2001 Prentice Hall, Inc. All rights reserved.
1    <?xml version = "1.0"?>                                                                Outline
2                                                       Optional XML declaration tells the
3    <!-- Fig. 22.1: article.xml                  -->   version the document conforms
                                                                                        article.xml
4    <!-- Article structured with XML -->
5
                                                      This is a comment in XML
6    <article>                                    The root element; every
7                                                 documents needs exactly one
8       <title>Simple XML</title>
9
                                                         The title of the document
10      <date>December 3, 2000</date>
11
12      <author>
13           <FirstName>Tem</FirstName>
14           <LastName>Nieto</LastName>
15      </author>
16
                                                                A summary tag with
17      <summary>XML is pretty easy.</summary>
                                                                summary data within it
18
19      <content>Once you have mastered HTML, XML is easily
20           learned. You must remember that XML is not for
21           displaying information but for managing information.
22      </content>
23
24 </article>
 2001 Prentice Hall, Inc. All rights reserved.
                                      22.3 Structuring Data




        Fig. 22.2 IE5 displaying article.xml. (part 1)


 2001 Prentice Hall, Inc. All rights reserved.
                                      22.3 Structuring Data




        Fig. 22.2 IE5 displaying article.xml. (part 2)


 2001 Prentice Hall, Inc. All rights reserved.
 1   <?xml version = "1.0"?>                                                       Outline
 2
 3   <!-- Fig. 22.3: letter.xml                   -->
 4   <!-- Business letter formatted with XML      -->                        letter.xml
 5
 6   <!DOCTYPE letter SYSTEM "letter.dtd">
 7
 8   <letter>
 9
10      <contact type = "from">
11         <name>John Doe</name>
12         <address1>123 Main St.</address1>
13         <address2></address2>
14         <city>Anytown</city>
15         <state>Anystate</state>
16         <zip>12345</zip>
17         <phone>555-1234</phone>
18         <flag gender = "M"/>         Flag gender is an empty element,
19      </contact>                      it is used to indicate the gender
20                                         of the current contact and does
21     <contact type = "to">               not mark up any text
22         <name>Joe Schmoe</name>
23         <address1>Box 12345</address1>
24         <address2>15 Any Ave.</address2>
25         <city>Othertown</city>
26         <state>Otherstate</state>
27         <zip>67890</zip>
28         <phone>555-4321</phone>
29         <flag gender = "M"/>
30     </contact>
 2001 Prentice Hall, Inc. All rights reserved.
31                                                                           Outline
32      <salutation>Dear Sir:</salutation>
33
                                                                       letter.xml
34      <paragraph>It is our privilege to inform you about our new
35           database managed with XML. This new system allows
36           you to reduce the load of your inventory list server by
37           having the client machine perform the work of sorting
38           and filtering the data.</paragraph>
39      <closing>Sincerely</closing>
40      <signature>Mr. Doe</signature>
41
42 </letter>




 2001 Prentice Hall, Inc. All rights reserved.
                                                        Outline

                                                  letter.xml




 2001 Prentice Hall, Inc. All rights reserved.
                                                        Outline

                                                  letter.xml




 2001 Prentice Hall, Inc. All rights reserved.
                                     22.4 XML Namespaces

      • Naming collisions
              – Two tags have the same name
                     • It is hard to tell the difference between the data in the tags
      • Namespaces
              – Used to prevent naming collisions
                     • </namespace:element>
              – Any name can be used as a namespace except xml
                     • xml is a reserved namespace
              – Author must provide a URI
                     • Uniform Resource Identifier
                     • Common practice is to use URLs for URIs



 2001 Prentice Hall, Inc. All rights reserved.
1    <?xml version = "1.0"?>                                                         Outline
2
3    <!-- Fig. 22.4 : namespace.xml -->
                                                                            namespace.xml
4    <!-- Namespaces                              -->
5
6    <directory xmlns:text = "urn:deitel:textInfo"
                                                                      Defines the URIs
7                   xmlns:image = "urn:deitel:imageInfo">
                                                                      of the document
8
9       <text:file filename = "book.xml">
10           <text:description>A book list</text:description>
11      </text:file>
12
13      <image:file filename = "funny.jpg">
14           <image:description>A funny picture</image:description>
15           <image:size width = "200" height = "100"/>
16      </image:file>
17
18 </directory>

                                       Applies the namespace
                                       prefix to the elements




 2001 Prentice Hall, Inc. All rights reserved.
1    <?xml version = "1.0"?>                                                                  Outline
2
3    <!-- Fig. 22.5 : defaultnamespace.xml -->
                                                                                      defaultnamespace.x
4    <!-- Using Default Namespaces                      -->
                                                                                      ml
5
6    <directory xmlns = "urn:deitel:textInfo"
7                   xmlns:image = "urn:deitel:imageInfo">               Defines the xmlns namespace
8
9       <file filename = "book.xml">
10           <description>A book list</description>
11      </file>
12                                                Child elements do
13      <image:file filename =             "funny.jpg">
                                                  not need the prefix
14           <image:description>A funny picture</image:description>
15           <image:size width = "200" height = "100"/>
16      </image:file>
17
18 </directory>




 2001 Prentice Hall, Inc. All rights reserved.
         22.5 Document Type Definitions (DTD) and
                        Schema
      • Documents for describing XML documents
              – Document Type Definitions (DTD)
              – Schemas




 2001 Prentice Hall, Inc. All rights reserved.
                     22.5.1 Document Type Definitions

      • DTD
              –    Optional but recommended for conformity
              –    Type check an XML document thus verifying validity
              –    EBNF grammar sets the structure of the document
              –    &
                     • Should use &amp
              – <
                     • Should use &lt
              – >
                     • Should use &gt




 2001 Prentice Hall, Inc. All rights reserved.
1    <!-- Fig. 22.6: letter.dtd                   -->                                         Outline
2    <!-- DTD document for letter.xml             -->
3
                                                                                      letter.dtd
4    <!ELEMENT letter (contact+, salutation, paragraph+,
5                              closing, signature )>                      Defines the rules
6                                                                         of element letter
7    <!ELEMENT contact (name, address1, address2, city, state,
8                               zip, phone, flag)>
9    <!ATTLIST contact type CDATA #IMPLIED>
10
11 <!ELEMENT name (#PCDATA)>
                                                         Defines an attribute
                                                  The element can store
12 <!ELEMENT address1 (#PCDATA)>                  parsed character data
13 <!ELEMENT address2 (#PCDATA)>
14 <!ELEMENT city (#PCDATA)>
15 <!ELEMENT state (#PCDATA)>
16 <!ELEMENT zip (#PCDATA)>
17 <!ELEMENT phone (#PCDATA)>
                                                        Specifies that the flag
18 <!ELEMENT flag EMPTY>                                must be either M or F
19 <!ATTLIST flag gender (M | F) "M">
20
21 <!ELEMENT salutation (#PCDATA)>
22 <!ELEMENT closing (#PCDATA)>
23 <!ELEMENT paragraph (#PCDATA)>
24 <!ELEMENT signature (#PCDATA)>



 2001 Prentice Hall, Inc. All rights reserved.
                                 22.5.2 W3C XML Schema

      • Schemas
              –    Alternative to DTDs
              –    Also used to validate XML forms
              –    Use validating parsers
              –    Are expected to replace DTDs
              –    Two models
                     • W3C XML Schema
                     • Microsoft XML Schema




 2001 Prentice Hall, Inc. All rights reserved.
1    <?xml version = "1.0"?>
                                                                                   Outline
2
3 <!-- Fig. 22.7 : xml-schema.xsd                 -->
                                                                          schema.xsd
4    <!-- Example W3C XML Schema                  -->
5
6    <xsd:schema xmlns:xsd = "http://www.w3.org/2000/10/XMLSchema">
7       <xsd:element name = "message" type = "xsd:string"/>
8
9       <xsd:element name = "greeting" type = "greetingType"/>
10
11      <xsd:complexType name = "greetingType" content = "mixed">
12           <xsd:element ref = "message"/>
13      </xsd:complexType>
14
15      <xsd:element name = "myMessage" type = "myMessageType"/>
16
17      <xsd:complexType name = "myMessageType">
18           <xsd:element ref = "greeting" minOccurs = "0"
                                                                 Creates an optional
                                                                 greeting element followed
19               maxOccurs = "1"/>
                                                                 by message elements
20           <xsd:element ref = "message" minOccurs = "1"
21               maxOccurs = "unbounded"/>
22    </xsd:complexType>
23 </xsd:schema>

 2001 Prentice Hall, Inc. All rights reserved.
 1   <?xml version = "1.0"?>                                                     Outline
 2
 3   <!-- Fig. 22.8 : intro3.xml                  -->
 4   <!-- Introduction to W3C XML Schema          -->
                                                                           intro3.xml
 5
 6   <myMessage
 7      xmlns:xsd = "http://www.w3.org/2000/10/XMLSchema-instance"
 8      xsd:noNamespaceSchemaLocation = "xml-schema.xsd">
 9
10      <greeting>Welcome to W3C XML Schema!</greeting>   Uses the XML
11      <message>This is a message.</message>             schema just created
12      <message>This is another message.</message>
13
14   </myMessage>




 2001 Prentice Hall, Inc. All rights reserved.
                                                              Outline

                                                        intro3.xml




               (Courtesy of Icon Information Systems)




 2001 Prentice Hall, Inc. All rights reserved.
                 22.6 Customized Markup Languages

      • Custom Languages
              – XML allows programmers to create their own tags to
                precisely describe data how they want




 2001 Prentice Hall, Inc. All rights reserved.
                                                  22.6.1 MathML

      • MathML
              – Developed by W3C
              – Used to show mathematical expressions and notations
              – W3C’s Amaya browser
                     • Can use the MathML
                     • Also used for HTML/XML editing




 2001 Prentice Hall, Inc. All rights reserved.
 1   <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">               Outline
 2   <html>
 3
 4   <!-- Fig. 22.9 mathml.html         -->                                 mathml.html
 5   <!-- Calculus example using MathML -->
 6
 7   <body>
                                  Embeds the file directly
 8
 9   <MATH>                       into an HTML file with
10      <mrow>                    the MATH element
11          <msubsup>
12             <mo>&Integral;</mo>        Allows author to properly
13             <mn>0</mn>                 group elements together
14             <mrow>
15                <mn>1</mn>
16                <mo>-</mo>
17                <mi>y</mi>
18             </mrow>
19          </msubsup>
20                                                 Is used to specify
21         <msqrt>                                 multiplication without
22              <mrow>
                                                   showing a symbol
23                   <mn>4</mn>
24                   <mo>&InvisibleTimes;</mo>
25                   <msup>
26                        <mi>x</mi>
27                        <mn>2</mn>
28                   </msup>
29                   <mo>+</mo>
30                   <mi>y</mi>
31              </mrow>
32         </msqrt>
 2001 Prentice Hall, Inc. All rights reserved.
33                                                                                 Outline
34       <mo>&delta;</mo>                         Creates the delta symbol
35       <mi>x</mi>
36    </mrow>                                                                mathml.html
37 </MATH>
38 </body>
39 </html>




 2001 Prentice Hall, Inc. All rights reserved.
                                                  22.6.2 WML

      • Wireless Markup Language (WML)
              – Allows text portions of Web pages to be displayed on
                wireless devices
              – Uses WAP to deliver content
                     • Wireless Application Protocol




 2001 Prentice Hall, Inc. All rights reserved.
                                                  22.6.3 XBRL

      • Extensible Business Reporting Language (XBRL)
              – Creation, exchange, and validation of financial information
              – Follows the principals of of GAAP and meets the standards
                of several committees
              – Improves accessing information efficiency




 2001 Prentice Hall, Inc. All rights reserved.
                                                  22.6.4 ebXML

      • Electronic Business XML (ebXML)
              – Develop and standardize a structure to organize business
                data
              – Goal is to reduce the cost of electronic trading for small and
                medium businesses




 2001 Prentice Hall, Inc. All rights reserved.
                                                  22.6.5 FpML

      • Financial Products Markup Language (FpML)
              – Standard to exchange financial information
              – Main use
                     • Platform independent data exchange




 2001 Prentice Hall, Inc. All rights reserved.
                        22.6.6 Other Markup Languages

      • Other languages
              – Hundreds exist
              – Everyday more are being developed




 2001 Prentice Hall, Inc. All rights reserved.
                  22.7 Document Object Model (DOM)

      • DOM
              – Uses the XML tree structure in memory to display the data
                effectively to the user
              – Each element acts as a node in the tree
              – Each node has properties, methods, and events
                     • Properties
                         – Used to get name or value
                     • Methods
                         – Used to add or delete nodes
                     • Events
                         – Generate code based on clicking or changes




 2001 Prentice Hall, Inc. All rights reserved.
                  22.7 Document Object Model (DOM)
                Ma rkup La ng ua g e       De sc rip tio n
                Chemical Markup            CML was developed by Peter Murray-Rust. It is used by chemists to
                Language (CML)             interchange descriptions of molecules, formulas and other chemical
                                           data. CML documents can be parsed and rendered by the Jumbo
                                           browser. Visit the CML home page at www.xml-cml.org.

                VoiceXML                   VoiceXML was developed by the VoiceXML forum founded by AT&T,
                                           IBM, Lucent and Motorola. It provides interactive voice communication
                                           between humans and computers through a telephone, PDA (Personal
                                           Digital Assistant) or desktop computer. VoiceXML documents can be
                                           parsed using the VoiceXML SDK developed by IBM. Visit
                                           www.voicexml.org for more information on VoiceXML.
                Synchronous                SMIL is used for multimedia presentations. It was primarily developed
                Multimedia Integration     by the W3C with contributions from other companies. Visit
                Language (SMIL )           www.w3.org/AudioVideo for more on SMIL.
                Vector Markup              VML marks up graphics information. It was developed by Microsoft,
                Language (VML)             Hewlett-Packard and other companies. For more information on VML,
                                           visit www.w3.org/TR/NOTE-VML.
                Product Data Markup        PDML is a markup language developed for product data interchange
                Language (PDML)            among businesses and government agencies. For more information on
                                           PDML, visit www.pdml.org.
                Commerce XML               cXML is a markup language that provides a protocol for business
                (cXML)                     transactions on the Internet. For more information on cXML, visit
                                           www.cxml.org/home.


        Fig 22.10…continued on next slide


 2001 Prentice Hall, Inc. All rights reserved.
                  22.7 Document Object Model (DOM)

          XMI (XML Metadata             XMI is used for metadata interchange between modelling
          Interchange)                  applications/tools that are based on UML (Unified Modelling
                                        Language) and metadata repositories like MOF (Meta Object Facility).
                                        XMI is a specification submitted to the OMG (Object Management
                                        Group) by IBM, Oracle, Unisys and others. Visit www.omg.org for
                                        more information.
          Trading Partner               tpaML is an XML-based markup language developed by IBM that
          Agreement Markup              defines an electronic trading partner agreement (TPA) document. A
          Language (tpaML)              TPA contains information about the organizations involved in the
                                        contract, the business protocols (e.g., cXML), etc. For more information
                                        on tpaML, visit www-
                                        4.ibm.com/software/developer/library/tpaml.html.
          Small to Medium               SMBXML was developed for small to medium sized business
          Business XML                  transactions. For more information on SMBXML, visit
          (SMBXML)                      www.smbxml.org.

          Financial XML                 FinXML is an XML based framework developed by the Financial
          (FinXML)                      consortium that provides a standard format for exchanging financial data
                                        between financial institutions. For more information on FinXML, visit
                                        www.finxml.org.
          Financial Information         FixML was developed by a consortium of over 20 financial firms and is
          Exchange Markup               a standard for data exchange between financial institutions. For more
          Language (FixML)              information on FixML visit www.fixprotocol.org.
         Fig. 22.10      Va rio us m a rkup la ng ua g e s d e rive d fro m XML.


 2001 Prentice Hall, Inc. All rights reserved.
                                                  22.8 XML::DOM

      • XML::DOM
              – Level 1 recommendations
                     • Changes that must be made in order to meet standards
              – Level 2 recommendations
                     • Changes that should be made to better the standardization
              – Node
                     • Represents an XML element
              – NodeList
                     • Array of nodes accessible by index number
              – NameNodeMap
                     • A hash of nodes, each accessible by name




 2001 Prentice Hall, Inc. All rights reserved.
                                                  22.8 XML::DOM



                                                     article


                  title                 date         author        summary   content


                                           firstName           lastName




        Fig. 22.11 Tree structure for article.xml.


 2001 Prentice Hall, Inc. All rights reserved.
 1   #!/usr/bin/perl                                                                     Outline
 2   # Fig. 22.12: contacts.pl
 3   # Using the XML::DOM module to manipulate an XML Document.
 4                                                                                 contacts.pl
 5   use warnings;
 6   use strict;
 7   use XML::DOM;
 8
 9   open( CONTACTS, "+<contact.xml" )
10      or die ( "Error opening contact.xml" );
11                                                          Creates a new parser
12   my $parser = new XML::DOM::Parser;
13   my $document = $parser->parse( \*CONTACTS );
14
15   printlist( $document );
16   instructions();
                                                Uses the parse method
17   my $choice = 6;                    Displaysto retrieve the document
                                                 the document
18
19   while ( $choice ne 'q' ) {
20      print( "\n? " );                          Gets the right choice
21      chomp( $choice = <STDIN> );               from the user and calls
22                                                the appropriate function
23     if ( $choice eq 'a' ) {
24         addContact( $document );
25     }
26     elsif ( $choice eq 'l' ) {
27         printlist( $document );
28     }
29     elsif ( $choice eq 'i' ) {
30         instructions( $document );
31     }
 2001 Prentice Hall, Inc. All rights reserved.
32       elsif ( $choice ne 'q' ) {
33          print( "$choice is not an option." .
                                                                                                Outline
34                 " Enter i for instructions.\n");
35       }                                                                                contacts.pl
36   }                                            At the end of the program the file is
37
                                                  reset to the beginning and then
38   seek( CONTACTS, 0, 0 );
39   truncate( CONTACTS, 0 );                     truncated
40   $document->print( \*CONTACTS );
41
42   sub printlist                   Creates the format the program
43   {                               uses to display the list of names
44      my $document = shift;
45      my $root =
46            $document->getElementsByTagName("contacts")->item(0);
47       my $contactList = $root->getChildNodes();
48                                                                Loops through each element
49       print( "Your contact list: \n" );                        and prints out the name
50
51       for my $i ( 1 .. $contactList->getLength - 1 ) {
52          my $contact = $contactList->item( $i );
53          next unless ( $contact->getNodeName eq 'contact' );
54
55           my $first = $contact->getElementsByTagName("FirstName");
56           my $text =
57                 $first->item( 0 )->getChildAtIndex( 0 )->getData();
58
59           my $lastN = $contact->getElementsByTagName( "LastName" );
60           my $last =
61                 $lastN->item( 0 )->getChildAtIndex( 0 )->getData();
62
63           print( "$text $last\n" );
64       }
 2001 Prentice Hall, Inc. All rights reserved.
65 }                                                                                        Outline
66
67 sub addContact
                                                                                   contacts.pl
68 {
69      my $document = shift;
70      my $root =                                        Gets the root of the content
71               $document->getElementsByTagName("contacts")->item(0);
72
73      print( "Enter the name of the person you wish to add: " );
74      chomp( my $name = <STDIN> );
75      my ( $first, $last ) = split( / /, $name );
76
77      my $firstNode = $document->createElement( "FirstName" );
78      $firstNode->addText( $first );
79
                                                   Adds the text node to its new element
80      my $lastNode = $document->createElement( "LastName" );
81      $lastNode->addText( $last );
                                                  Creates a last name element and
82
                                                  adds the node to its element
83      my $contact = $document->createElement( "contact" );
84      $contact->appendChild( $firstNode );
85      $contact->appendChild( $lastNode );
                                                       Adds the nodes to the list and
86
                                                       then attaches the list to the root
87      $root->appendChild( $contact );
88 }
89


 2001 Prentice Hall, Inc. All rights reserved.
90 sub instructions                                           Outline
91 {
92      print( "\n\n",                                  contacts.pl
93               "Enter 'a' to add a contact.\n",
94               "Enter 'l' to list the contacts.\n",
95               "Enter 'i' for instructions.\n",
96               "Enter 'q' to quit.\n" );
97 }




 2001 Prentice Hall, Inc. All rights reserved.
Your contact list:                                                  Outline
John Black
Sue Green
Bob Red                                                       contacts.pl
Mary Blue
Mike White
Jane Brown
Bill Gray


Enter   'a'   to add a contact.
Enter   'l'   to list the contacts.
Enter   'i'   for instructions.
Enter   'q'   to quit.
? a
Enter   the name of the person you wish to add: Bart Fuscha

? l
Your contact list:
John Black
Sue Green
Bob Red
Mary Blue
Mike White
Jane Brown
Bill Gray
Bart Fuscha

? q




 2001 Prentice Hall, Inc. All rights reserved.
 1   <?xml version = "1.0"?>                            Outline
 2
 3   <!-- Fig. 27.13: contact.xml -->
 4   <!-- A contact list          -->             contact.xml
 5
 6   <contacts>
 7
 8      <contact>
 9         <LastName>Black</LastName>
10         <FirstName>John</FirstName>
11      </contact>
12
13      <contact>
14         <LastName>Green</LastName>
15         <FirstName>Sue</FirstName>
16      </contact>
17
18      <contact>
19         <LastName>Red</LastName>
20         <FirstName>Bob</FirstName>
21      </contact>
22
23      <contact>
24         <LastName>Blue</LastName>
25         <FirstName>Mary</FirstName>
26      </contact>
27
28     <contact>
29         <LastName>White</LastName>
30         <FirstName>Mike</FirstName>
31     </contact>
 2001 Prentice Hall, Inc. All rights reserved.
32                                                      Outline
33      <contact>
34         <LastName>Brown</LastName>
35         <FirstName>Jane</FirstName>
                                                  contact.xml
36      </contact>
37
38      <contact>
39         <LastName>Gray</LastName>
40         <FirstName>Bill</FirstName>
41      </contact>
42




 2001 Prentice Hall, Inc. All rights reserved.
                                          22.9 DOM Methods

      • Key methods
              – Listed in the following tables
                     • Node methods (22.14)
                     • NodeList methods (22.15)
                     • NameNodeMap methods (22.16)
                     • Document methods (22.17)
                     • Element methods (22.18)
                     • Attr methods (22.19)
                     • Text methods (22.20)




 2001 Prentice Hall, Inc. All rights reserved.
                                          22.9 DOM Methods


         Method                             Description

         getNodeType                        Returns an integer representing the node type (see the
                                            Module Documentation for this list).
         getNodeName                        Returns the name of the node. If the node does not have
                                            a name, a string consisting of # followed by the type of
                                            the node is returned.
         getNodeValue                       Returns a string or undefined depending on the node
                                            type.
         getParentNode                      Returns the parent node.
         getChildNodes                      Returns a NodeList (Fig. 22.15) with all the children of
                                            the node. When called in list context, it returns a
                                            regular Perl list of the children.
         getFirstChild                      Returns the first child in the NodeList.
         getLastChild                       Returns the last child in the NodeList.
         getPreviousSibling                 Returns the node preceding this node, or undefined.
         getNextSibling                     Returns the node following this node, or undefined.
         getAttributes                      Returns a NamedNodeMap (Fig. 22.16) containing the
                                            attributes for this node.


        Fig. 22.14 Some Node object methods (continued on next slide)


 2001 Prentice Hall, Inc. All rights reserved.
                                          22.9 DOM Methods
            Method                          Description

            insertBefore                    Inserts the node passed as the first argument
                                            before the existing node passed as the second
                                            argument. If the new node is already in the tree,
                                            it is removed before insertion. The same behavior
                                            is true for other methods that add nodes.
            replaceChild                    Replaces the second argument node with the first
                                            argument node.
            removeChild                     Removes the child node passed to it.
            appendChild                     Appends the node passed to it to the list of child
                                            nodes.
            getElementsByTagName            Returns a NodeList of all nodes in the subtree with
                                            the name specified as the first argument ordered as
                                            they would be encountered in a preorder traversal.
                                            An optional second argument specifies either the
                                            direct child nodes (0), or any descendant (1).
            print                           Prints the entire subtree to the filehandle object
                                            passed to it. Other versions of print include
                                            printToFile and
                                            printToFileHandle.
            getChildAtIndex                 Returns the child node at the specified index in
                                            the child list.
            addText                         Appends the string passed to it to the last Node if
                                            it is a Text node, otherwise creates a new Text
                                            node for the string and adds it to the end of the
                                            child list.
            isAncestor                      Returns true if the node passed is a parent of the
                                            node, or is the node itself.

 2001 Prentice Hall, Inc. All rights reserved.
                                          22.9 DOM Methods




           Method                 Description

           item                   Passed an index number, will return the
                                  element at that index. Indices range
                                  from 0 to length – 1
           getLength Returns the total number of nodes in
                     the list.




        Fig. 22.15 Some NodeList methods


 2001 Prentice Hall, Inc. All rights reserved.
                                          22.9 DOM Methods

           Method                                 Description

           getNamedItem                           Returns a node in the NamedNodeMap
                                                  with the specified name or undefined
                                                  if there were none.
           setNamedItem                           Stores a node passed to it in the
                                                  NamedNodeMap. Two nodes with the
                                                  same name cannot be stored in the
                                                  same NamedNodeMap.
           removeNamedItem                        Removes a specified node from the
                                                  NamedNodeMap.
           getLength                              Returns the total number of nodes in
                                                  the NamedNodeMap.
           getValues                              Returns a NodeList containing all
                                                  the nodes in the NamedNodeMap.

        Fig. 22.16 Some NamedNodeMap methods


 2001 Prentice Hall, Inc. All rights reserved.
                                          22.9 DOM Methods

          Method                                  Description

          getDocumentElement                      Returns the root node of the
                                                  document tree.
          createElement                           Creates and returns an element
                                                  with the specified tag name.
          createAttribute                         Creates and returns an attribute
                                                  with the specified name and value.
          createTextNode                          Creates and returns a text node
                                                  that contains the specified text.
          createComment                           Creates a comment to hold the
                                                  specified text.



        Fig. 22.17 Some Document methods


 2001 Prentice Hall, Inc. All rights reserved.
                                          22.9 DOM Methods

              Method                              Description

              getTagName                          Returns the name of the element.
              setTagName                          Changes the name of the element to
                                                  the specified name.
              getAttribute                        Returns the value of the specified
                                                  attribute.
              setAttribute                        Changes the value of the attribute
                                                  passed as the first argument the
                                                  value passed as the second
                                                  argument.
              removeAttribute                     Removes the specified attribute.
              getAttributeNode                    Returns the specified attribute
                                                  node.
              setAttributeNode                    Adds a new attribute node with the
                                                  specified name.


        Fig. 22.18 Some Element methods


 2001 Prentice Hall, Inc. All rights reserved.
                                          22.9 DOM Methods



      Method                   Description

      getValue                 Returns the specified attribute’s
                               value.
      setValue                 Changes the value of the attribute to
                               the specified value.
      getName                  Returns the name of the attribute.




        Fig. 22.19 Some Attr methods


 2001 Prentice Hall, Inc. All rights reserved.
                                          22.9 DOM Methods



          Method                    Description

          getData                   Returns the data contained in the node
                                    (text or comment).
          setData                   Sets the node’s data.
          getLength                 Returns the number of characters
                                    contained in the node.




        Fig. 22.20 Some Text and Comment methods


 2001 Prentice Hall, Inc. All rights reserved.
                        22.10 Simple API for XML (SAX)

      • SAX
              – Alternative to parsing XML documents
              – Uses to an event-based model
              – Can be used to read XML documents
                     • Generate a DOM tree
              – But since DOM trees are easier to manipulate, SAX is
                generally used for reading rather than editing
              – Supports a variety of programming languages




 2001 Prentice Hall, Inc. All rights reserved.
               22.11 Parsing XML with XML::Parser

      • XML::Parser
              – Allows user to choose which model to use
                     • SAX
                     • DOM
              – Event handlers are called based on markup reached
                     • Start tag calls a Start handle




 2001 Prentice Hall, Inc. All rights reserved.
 1   #!/usr/bin/perl                                                                   Outline
 2   # Fig. 22.21: taginfo.pl
 3   # SAX example.
 4
                                                                               taginfo.pl
 5   use warnings;
 6   use strict;
 7   use XML::Parser;
 8
 9   my ( $file, $tag ) = @ARGV;
10
11   my $parser = new XML::Parser( ErrorContext => 2 );
12
13   $parser->setHandlers( Char => \&charHandler,
14                         Start => \&startHandler,               Sets the event handlers
15                         End   => \&endHandler );               for the program
16
17   my $depth = 0;
18   $parser->parsefile( $file );                 Each handler is sent an Expat
19                                                object that contains information
20   sub startHandler                             about the state of the parser
21   {
22      my ( $expat, $element, @attributes ) = @_;
23      return if ( !$element );
24      if ( $element eq $tag ) {
25         print( "\n", ' ' x $depth,
26            "<$tag> starts on line ",$expat->current_line(),"\n");
27           $depth += 3;
28
 2001 Prentice Hall, Inc. All rights reserved.
29           print( ' ' x $depth, "Parent tag: ",$expat->current_element(),                          Outline
30               "\n", ' ' x $depth, "Depth: ", $expat->depth(), "\n",
31               ' ' x $depth, "Attributes: @attributes\n" );
32       }
                                                                                           taginfo.pl
33       elsif ( $expat->current_element() eq $tag ) {
34          print( ' ' x $depth, "Child: $element\n" );
35       }                                                                 Prints the information if
36   }                                                                     the tag is of that specified
37
38   sub endHandler
39   {
40      my ( $expat, $element ) = @_;
41                                                            Prints out the element and the current
42       if ( $element eq $tag ) {
43          $depth -= 3;
                                                              line that the end element is on
44
45           print( ' ' x $depth, "<\\$element> ends on line ",
46              $expat->current_line(), "\n\n" );
47       }
48   }
49
50   sub charHandler
                                                  Called whenever normal text is encountered
51   {
52      my ( $expat, $data ) = @_;
53                                                                       Removes any formatting
54       if ( $expat->current_element() eq $tag ) {
                                                          text or             extra white spaces
55          $data =~ s/^\s+//;
56          $data =~ s/\s+$//;
57          print( ' ' x $depth, "Data: $data\n" ) if ( $data );
58       }
59   }
 2001 Prentice Hall, Inc. All rights reserved.
<box> starts on line 8                                  Outline
   Parent tag: boxlist
   Depth: 1
   Attributes: size big                           taginfo.pl
   Data: This is the big box.

   <box> starts on line 11
      Parent tag: box
      Depth: 2
      Attributes: size medium
      Data: Medium sized box
      Child: item
      Child: thing
   <\box> ends on line 15

   Child: parcel

   <box> starts on line 18
      Parent tag: box
      Depth: 2
      Attributes: type small
      Data: smaller stuff

        <box> starts on line 20
           Parent tag: box
           Depth: 3
           Attributes: type tiny
           Data: tiny stuff
        <\box> ends on line 20

   <\box> ends on line 21

<\box> ends on line 23

 2001 Prentice Hall, Inc. All rights reserved.
 1   <?xml version = "1.0"?>                              Outline
 2
 3   <!-- Fig. 22.22: boxes.xml       -->
 4   <!-- Article structured with XML -->
                                                    boxes.xml
 5
 6   <boxlist>
 7
 8     <box size = "big">
 9        This is the big box.
10
11         <box size = "medium">
12            Medium sized box
13            <item>Some stuff</item>
14            <thing>More stuff</thing>
15         </box>
16
17         <parcel></parcel>
18         <box type = "small">
19            smaller stuff
20            <box type = "tiny">tiny stuff</box>
21         </box>
22
23     </box>
24
25   </boxlist>




 2001 Prentice Hall, Inc. All rights reserved.
               22.11 Parsing XML with XML::Parser

          Handler                   Arguments     Description

          Init                      Expat         Called just before the
                                                  parsing of a document starts.
          Final                     Expat         Called after parsing has
                                                  finished.
          Start                     Expat,        Called when a starting XML
                                    Element,      tag is encountered.
                                    Attributes
          End                       Expat,        Called when a closing XML tag
                                    Element       is encountered.
          Char                      Expat, Data   Called when non-markup is
                                                  encountered.
          Comment                   Expat, Data   Called when a comment is
                                                  encountered.



        Fig. 22.23 Some XML::Parser events


 2001 Prentice Hall, Inc. All rights reserved.
               22.11 Parsing XML with XML::Parser


          Method                              Used for

          current_line                        Returns the current line number.
          current_column                      Returns the current column number of the
                                              parse.
          current_byte                        Returns how many bytes have been parsed.
          depth                               Returns the size of the context list.
          element_index                       Returns how many elements have been
                                              parsed.
          finish                              Unsets all handlers. The document is still
                                              parsed, but no handlers are called.




        Fig. 22.24 Some Expat methods


 2001 Prentice Hall, Inc. All rights reserved.
                          22.12 Data::Dumper and XML

      • The Data::Dumper
              – Used to quickly and easily create a DOM tree




 2001 Prentice Hall, Inc. All rights reserved.
1    #!/usr/bin/perl                                                                                    Outline
2    # Fig. 22.25: tree.pl
3    # Using Data::Dumper to print the results of tree-based parsing.
                                                                                             tree.pl
4
5    use warnings;
6    use strict;
7    use Data::Dumper;                            Uses the dumper
8    use XML::Parser;
9
10 my $xml =<<"DONE";
11 <letter><contact type = "from"><name>John</name>
12      <phone>555-1234</phone></contact>
13      <salutation format = "formal">Dear Sir:</salutation>
14 </letter>
15 DONE                                               Creates a new parser and sets it’s style to Tree
16
17 my $parser = new XML::Parser(Style=>'Tree', ErrorContext => 2);

18
                                                                    Returns the tree and
19 my $tree = $parser->parse( $xml );
                                                                    stores it in $tree
20
21 print( Data::Dumper->Dump( [ $tree ] ) );
                                                                    Uses the dumper to convert the
                                                                    tree into a string and display it



 2001 Prentice Hall, Inc. All rights reserved.
                                                        Outline
$VAR1 = [
              'letter',
              [                                   tree.pl
                {},
                'contact',
                [
                  {
                     'type' => 'from'
                  },
                  'name',
                  [
                     {},
                     0,
                     'John'
                  ],
                  0,
                  '
   ',
                    'phone',
                    [
                      {},
                      0,
                      '555-1234'
                    ]
                 ],
                 0,
                 '


Continued…



 2001 Prentice Hall, Inc. All rights reserved.
    ',                                                  Outline
                    'salutation',
                    [
                       {                          tree.pl
                          'format' => 'formal'
                       },
                       0,
                       'Dear Sir:'
                    ],
                    0,
                    '
'
                ]
           ];




 2001 Prentice Hall, Inc. All rights reserved.
              22.13 Extensible Style Language (XSL)

      • XSL
              – Defines how an XML document’s data is rendered
              – Provides rules for displaying or organizing data
              – Defines how the document is converted




 2001 Prentice Hall, Inc. All rights reserved.
 1   <?xml version = "1.0"?>
 2   <?xml:stylesheet type = "text/xsl" href = "elements.xsl"?>
                                                                                Outline
 3   <!-- Fig. 22.26 : games.xml -->
 4   <!-- Sports Database        -->                                      games.xml
 5
                                                  Specifies a reference
 6   <sports>
 7
                                                  to a XSL stylesheet
 8      <game id = "783">
 9         <name>Cricket</name>
10
11         <para>
12            More popular among commonwealth nations.
13         </para>
14      </game>
15
16      <game id = "239">
17         <name>Baseball</name>
18
19         <para>
20            More popular in America.
21         </para>
22      </game>
23
24      <game id = "418">
25         <name>Soccer (Football)</name>
26
27         <para>
28            Most popular sport in the world.
29         </para>
30      </game>
31
32   </sports>
 2001 Prentice Hall, Inc. All rights reserved.
                                                        Outline

                                                  games.xml




 2001 Prentice Hall, Inc. All rights reserved.
1    <?xml version = "1.0"?>                                                                         Outline
2
3    <!-- reference XSL stylesheet URI                   -->
                                                                                               elements.xsl
4    <xsl:stylesheet xmlns:xsl = "http://www.w3.org/TR/WD-xsl">
5
6    <!-- Fig. 22.27 : elements.xsl                      -->
7    <!-- A simple XSLT transformation                   -->
                                                                     Stylesheet start tag;
8
                                                                     begins every stylesheet
9    <xsl:template match = "/">
10
11     <html>                                     Selects the root element
12                                                of an XML document
13        <title>Sports</title>
14
15        <body>
16
17            <table border = "1" bgcolor = "cyan">
18
19            <thead>
20               <tr>
21                  <th>ID</th>
22                  <th>Sport</th>
23                  <th>Information</th>
24               </tr>
25            </thead>
26
 2001 Prentice Hall, Inc. All rights reserved.
27              <!-- insert each name and para element value into -->              Outline
28              <!-- a table row.                                 -->
29              <xsl:for-each select = "sports/game">             Searches through the tree
                                                                             elements.xsl
30                  <tr>                                          looking for game elements
31                     <td><xsl:value-of select = "@id"/></td>
32                     <td><xsl:value-of select = "name"/></td>
33                     <td><xsl:value-of select = "para"/></td>
34                  </tr>
35              </xsl:for-each>
36
37              </table>
38
39         </body>
40
41     </html>
42 </xsl:template>
43
44 </xsl:stylesheet>




 2001 Prentice Hall, Inc. All rights reserved.
                            22.14 Generating XML in Perl

      • Creating XML
              – Use print or write functions to print out XML




 2001 Prentice Hall, Inc. All rights reserved.
 1   #!/perl/bin/perl                                                                          Outline
 2   # Fig. 22.28: xmlgenerator.pl
 3   # Converting a comma separated list data source to XML.
 4                                                                                       xmlgenerator.pl
 5   use warnings;
 6   use strict;
 7
 8   print <<'HEADER';
 9   Content-Type: text/xml                          Prints out the header information
10
11   <?xml version = "1.0"?>
12   <?xml:stylesheet type = "text/xsl" href = "/contact_list.xsl"?>
13   HEADER
14
15   print( "<contacts>\n\n" );
16
17   open( NAMES, "names.txt" ) or die ("Error opening names.txt");
18
19   while ( <NAMES> ) {                          Opens the file names.txt
20      chomp;
21
22      # escape any characters not allowed in XML content.
23      s/&/&amp;/;
24      s/</&lt;/;
                                    Replaces any characters that
25      s/>/&gt;/;
26      s/"/&quot;/;                are not allowed with their
27      s/’/&apos;/;                appropriate escape characters
28
29      my ( $last, $first ) = split( /, / );
30
 2001 Prentice Hall, Inc. All rights reserved.
31      print( "         <contact>\n",                                                            Outline
32                 "         <LastName>$last</LastName>\n",
33                 "         <FirstName>$first</FirstName>\n",
                                                                                            xmlgenerator.pl
34                 "     </contact>\n\n" );
35 }
                                                          Prints out the contacts by last
36
                                                          name, first name format
37 close( NAMES );
38
39 print( "</contacts>\n" );
                                              Closes the file




 2001 Prentice Hall, Inc. All rights reserved.
            22.15 Case Study: Message Forums with
                         Perl and XML
      • Message forums
              – Virtual bulletin boards
                     • Discussion groups
                     • General comments
                     • Question and answer




 2001 Prentice Hall, Inc. All rights reserved.
            22.15 Case Study: Message Forums with
                         Perl and XML
                     File Name                    Description

                     forums.xml                   XML document listing all available
                                                  forums and their filenames.
                     default.pl                   Main page, providing navigational
                                                  links to the forums.
                     template.xml                 Template for a message forum XML
                                                  document.
                     addForum.pl                  Adds a forum.
                     feedback.xml                 Sample message forum.
                     formatting.xsl Document for transforming message
                                    forums into HTML.
                     addPost.pl     Adds a message to a forum.
                     invalid.html                 Used to display an error message.
                     site.css                     Stylesheet for formatting HTML
                                                  documents.



        Fig. 22.29 Message forum documents


 2001 Prentice Hall, Inc. All rights reserved.
                 22.15.1 Displaying the Forums using
                            XML::Parser
      • XML::Parser
              – Documents used to display and organize a message forum




 2001 Prentice Hall, Inc. All rights reserved.
                 22.15.1 Displaying the Forums using
                            XML::Parser



                                default.pl                         forums.xml


                                                   addForum.pl

                                feedback.xml


                                                  formatting.xsl

                                   addPost.pl




        Fig. 22.30 Key interactions between message forum documents


 2001 Prentice Hall, Inc. All rights reserved.
1   <?xml version = "1.0"?>                                                  Outline
2   <!-- Fig. 22.31: feedback.xml -->
3
4   <?xml:stylesheet type="text/xsl" href="../XML/formatting.xsl"?>   feedback.xml
 5
 6 <forum file = "feedback.xml">            References the formatting.xsl   stylesheet
 7    <name>Feedback</name>
 8
 9    <message timestamp = "Mon Nov 27 13:18:15 2000">
10       <user>Emily</user>
11       <title>Nice forums!</title>
12       <text>These forums are great! Well done, all.</text>
13    </message>
14
15 </forum>




 2001 Prentice Hall, Inc. All rights reserved.
 1   <?xml version = "1.0"?>                                              Outline
 2   <!-- Fig. 22.32: forums.xml -->
 3
 4   <?xml:stylesheet type = "text/xsl" href = "formatting.xsl"?>   forums.xml
 5
 6   <forums>
 7
 8      <forum filename = "feedback.xml">
 9         <name>Feedback</name>
10      </forum>
11
12   </forums>




 2001 Prentice Hall, Inc. All rights reserved.
 1   #!perl                                                                          Outline
 2   # Fig. 22.33: default.pl
 3   # Default page for XML forums
 4                                                                         default.pl
 5   use   warnings;
 6   use   strict;
 7   use   CGI qw( :standard );
 8   use   XML::Parser;
 9   use   Fcntl qw( :flock );
10
11   my ( $parser, @files, @forums, @items );
12
13   open( XML, "+<../htdocs/XML/forums.xml" ) or
14      die( "Could not open: $!" );                     Specifies a shared lock
15   flock( XML, LOCK_SH );
16   $parser = new XML::Parser( Handlers => { Start => \&startTag,
17       Char => \&text       } );
18   $parser->parse( \*XML );                           Creates a new parser to
19   close( XML );                                      handle event based objects
20
21   print( header, start_html( -title => "Deitel Message Forums",
22      -style => { -src => "/XML/site.css" } ) );
23
24   print( h1( "Deitel Message Forums" ) );               Begins HTML output
25
26   @items = map { a( { -href => "/XML/$files[ $_ ]" },
27      $forums[ $_ ] ) } ( 0 .. $#files );
28                                                         Creates an array of links to
29   print( p( strong( "Available Forums" ),               forum management options
30      ul( li( \@items ) ) ) );
31
 2001 Prentice Hall, Inc. All rights reserved.
32   @items = ( a( { -href => "addforum.pl" }, "Add a Forum" ),                                      Outline
33              "Delete a Forum", "Modify a Forum" );
34
35   print( p( strong( "Forum Management" ), ul( li( \@items ) ) ),                           default.pl
36         end_html );
37                                                            Check to see if it is a forum element,
38   sub startTag
                                               and if so push it onto the               array of files
39   {
40     my ( $expat, $element, %attributes ) = @_;
41      push(@files,$attributes{"filename"}) if $element eq "forum";
42   }
43
                                            Check to see if the text was found in a name,
44   sub text                               if so it is pushed onto the list of forum names
45   {
46      my ( $expat, $string ) = @_;
47      push( @forums, $string ) if $expat->in_element( "name" );
48   }




 2001 Prentice Hall, Inc. All rights reserved.
                                                        Outline

                                                  default.pl




           Opening a page
           after a forum
           called books
           has be created




 2001 Prentice Hall, Inc. All rights reserved.
       22.15.2 Using XML::DOM to Add Forums and
                       Messages
      • XML::DOM
              – Scripts and documents used to add forums and messages
              – Done in accordance with the W3C DOM recommendation




 2001 Prentice Hall, Inc. All rights reserved.
 1   #!perl                                                                                      Outline
 2   # Fig. 22.34: addForum.pl
 3   # Adds a forum to the list
 4                                                                                    addForum.pl
 5   use   warnings;
 6   use   strict;
 7   use   CGI qw( :standard );
 8   use   XML::DOM;
 9   use   Fcntl qw( :flock :DEFAULT );           param is originally
10                                                empty so line 53 executes
11   if ( param ) {
12      my ( $parser, $document, $forums, $forum, $name );
13
14         my ( $file, $newfile) = ("forums.xml", param( "filename" ));
15         $newfile =~ /^\w+\.xml$/ or die( "Not a valid file: $!" );
16
17      sysopen(NEW,"../htdocs/XML/$newfile", O_WRONLY|O_EXCL|O_CREAT)
18            or die( "Could not create: $!" );
19         open( FORUMS, "+<../htdocs/XML/$file" ) or                  Opens the forums file
20            die( "Could not open: $!" );                             for reading and writing
21         flock( FORUMS, LOCK_EX );
22
23         $parser = new XML::DOM::Parser;
24         $document = $parser->parse( \*FORUMS );                      Accesses the root
25         $forums = $document->getDocumentElement;                     element forums
26
27         $forum = $document->createElement( "forum" );
28         $forum->setAttribute( "filename", $newfile );
29         $forums->insertBefore( $forum, $forums->getFirstChild );
30
 2001 Prentice Hall, Inc. All rights reserved.
31      $name = $document->createElement( "name" );                                         Outline
32      $name->addText( param( "name" ) );
33      $forum->appendChild( $name );
                                                                                    addForum.pl
34
                                                   Adds the child to the forum
35      seek( FORUMS, 0, 0 );
36      truncate( FORUMS, 0 );
37      $document->print( \*FORUMS );
                                                    Updates the forum
38      close( FORUMS );
                                                    with the new data
39
40      $document = $parser->parsefile("../htdocs/XML/template.xml");
41      $forum = $document->getDocumentElement;
42      $forum->setAttribute( "file", $newfile );
43
44      $name = $document->createElement( "name" );
45      $name->addText( param( "name" ) );
46      $forum->appendChild( $name );
                                                              Adds another child to forum
47
48      $document->print( \*NEW );
49      close( NEW );
50      print( redirect( "default.pl" ) );
51 }




  2001 Prentice Hall, Inc. All rights reserved.
52 else {                                                                         Outline
53    print( header, start_html( -title => "Add a forum",
54        -style => { -src => "/XML/site.css" } ) );
55    print( start_form(),
                                                                         addForum.pl
                                                     Outputs a form
56        "Forum Name", br(),
57        textfield( -name => "name", -size => 40 ), br(),
                                                                      Prompts the user for a
58        "Forum File Name", br(),
59        textfield( -name => "filename", -size => 40 ), br(),
                                                                      forum name and
60        submit( -name => "submit", value => "Submit" ),             filename for the XML
61        reset(), end_form(),                                        document to be created
62        a({-href=>"/cgi-bin/default.pl"},"Return to Main Page"),
63           end_html() );
64 }




 2001 Prentice Hall, Inc. All rights reserved.
            22.15.3 Alterations for Non-XSL Browser

      • Alteration
              – Use XML stylesheets to display XML documents
      • XSL templates
              – Define a rule that is applied to a given element




 2001 Prentice Hall, Inc. All rights reserved.
1   <?xml version = "1.0"?>                                                                      Outline
2
3   <!-- Fig. 22.35 : template.xml -->
4
                                                                                           template.xml
5   <?xml:stylesheet type="text/xsl"href="../XML/formatting.xsl"?>
6 <forum>
7 </forum>


                                                  A template used to generate new forums




 2001 Prentice Hall, Inc. All rights reserved.
 1   #!perl                                                                           Outline
 2   # Fig. 22.36: addPost.pl
 3   # Adds a posting to a forum
 4
                                                                           addPost.pl
 5   use   warnings;
 6   use   strict;
 7   use   CGI qw( :standard );
 8   use   XML::DOM;
 9   use   Fcntl qw( :flock );
10
11   if ( param( "submit" ) ) {
12      my ( $parser, $document, $forum, $message, $element );
13
14         my $file = param( "file" );
15         $file =~ /^\w+\.xml$/ or die( "Not a valid file: $!" );
16         open( FORUM, "+<../htdocs/XML/$file" )
17            or die( "Could not open: $!" );
18         flock( FORUM, LOCK_EX );                        The XSL template     matches
19                                                          the forum element
20         $parser = new XML::DOM::Parser;
21         $document = $parser->parse( \*FORUM );             Retrieves the file’s
22         $forum = $document->getDocumentElement;            element information and
23                                                            stores it in $forum
24         $message = $document->createElement( "message" );
25         $message->setAttribute( "timestamp", scalar( localtime ) );
26         $forum->appendChild( $message );
27


 2001 Prentice Hall, Inc. All rights reserved.
28    foreach ( qw( user title text ) ) {
29       $element = $document->createElement( $_ );
                                                                                   Outline
30       $element->addText( param( $_ ) );
31       $message->appendChild( $element );                                  addPost.pl
                                                        Creates a tree by appending
32    }
33
                                                        each element to the root and
34    seek( FORUM, 0, 0 );                              attaching text to each element
35    truncate( FORUM, 0 );
36    $document->printToFileHandle( \*FORUM );
37    close( FORUM );
38    print( redirect( "/XML/$file" ) );
39 }
40 elsif ( param ) {
41     my $file = param( "file" );
42     print( header, start_html( -title => "Add a posting",
43         -style => { -src => "/XML/site.css" } ) );
                                                                 Creates 3 text fields and a
44     print( start_form(),                                      submit button for the user to
45           "User", br(),                                       enter and then submit their data
46           textfield( -name => "user", -size => 40 ), br(),
47           "Message Title", br(),
48           textfield( -name => "title", -size => 40 ), br(),
49           "Message Text", br(),
50           textarea( -name => "text", -cols => 40, -rows => 5 ),
51           br(), hidden( -name => "file", -value => $file ),
52           submit( -name => "submit", -value => "Submit" ),
53           reset(), end_form(),
54           a( { -href => "/XML/$file" }, "Return to Forum" ),
55               end_html() );
56 }
57 else {
58     print( redirect( "error.html" ) );
59 }
 2001 Prentice Hall, Inc. All rights reserved.
                                                        Outline

                                                  addPost.pl




 2001 Prentice Hall, Inc. All rights reserved.
1    <?xml version = "1.0"?>                                                            Outline
2
3    <!-- Fig. 22.37 : formatting.xsl -->
4                                                                               formatting.xsl
5    <xsl:stylesheet xmlns:xsl = "http://www.w3.org/TR/WD-xsl">
6
7      <xsl:template match = "*|@*|text()|cdata()|comment()|pi()">
8         <xsl:copy><xsl:apply-templates
9             select = "*|@*|text()|cdata()|comment()|pi()"/></xsl:copy>
10     </xsl:template>
11
12     <xsl:template match = "/">                            Creates the XSL template
13        <html>
                                                             for formatting and
14        <xsl:apply-templates select = "*"/>
15        </html>
                                                             displaying a forum
16     </xsl:template>
17
18     <xsl:template match = "forum">
19        <head>
20           <title><xsl:value-of select = "name"/></title>
21           <link rel = "stylesheet" type = "text/css"
22               href = "../XML/site.css"/>
23        </head>
24
25         <body>
26              <table width = "100%" cellspacing = "0"
27                   cellpadding = "2">
28                   <tr>
29                        <td class = "forumTitle">
30                             <xsl:value-of select = "name"/>
 2001 Prentice Hall, Inc. All rights reserved.
31                     </td>                                                          Outline
32                  </tr>
33               </table>
34                                                                              formatting.xsl
35               <br/>
36               <xsl:apply-templates select = "message"/>
37               <br/>
38
39               <center>
40                  <a>
41                    <xsl:attribute name = "HREF">../cgi-bin/
42                        addPost.pl?file=<xsl:value-of select = "@file"/>
43
44                      </xsl:attribute>
45                      Post a Message
46                    </a>
47                    <br/>
48                    <br/>
49                    <a href="../cgi-bin/default.pl">Return to Main Page</a>
50               </center>
51
52         </body>
53      </xsl:template>
54
55      <xsl:template match = "message">
56         <table width = "100%" cellspacing = "0"
57            cellpadding = "2">
58            <tr>
59
 2001 Prentice Hall, Inc. All rights reserved.
60                    <td class = "msgTitle">                                Outline
61                        <xsl:value-of select = "title"/>
62                    </td>
                                                                       formatting.xsl
63
64               </tr>
65
66               <tr>
67                    <td class = "msgInfo">
68                        by
69                        <em><xsl:value-of select = "user"/></em>
70                        at
71                        <span class = "date">
72                             <xsl:value-of select = "@timestamp"/>
73                        </span>
74                    </td>
75               </tr>
76
77               <tr>
78                    <td class = "msgText">
79                        <xsl:apply-templates select = "text"/>
80                    </td>
81               </tr>
82
83           </table>
84      </xsl:template>
85
86 </xsl:stylesheet>
 2001 Prentice Hall, Inc. All rights reserved.
 1   #!perl                                                                                    Outline
 2   # Fig. 22.38: forum.pl
 3   # Display forum postings for non-XSL browser
 4                                                                                    forum.pl
 5   use   warnings;
 6   use   strict;
 7   use   CGI qw( :standard *center *table );
 8   use   XML::Parser;
 9   use   Fcntl qw( :flock );
10
11   print( redirect( "error.html" ) ) if not param();                    Displays the error.html page
12                                                                        if no parameters are passed
13   my ( $file, $parser, %info );
14
15   $file = param( "file" );
16   $file =~ /^\w+\.xml$/ or die( "Not a valid file: $!" );
17   open( FORUM, "../htdocs/XML/$file" ) or
                                                                               Checks to see if the file
18      die( "Could not open: $!" );
                                                 Opens the file                is a valid XML file
19   flock( FORUM, LOCK_SH );
20                                                                               Creates handlers set to
21   $parser = new XML::Parser(             Style     => "Subs",                 subroutines with names
22                                          Handlers => { Char => \&text } ); derived from the
23   $parser->parse( \*FORUM );                                                  corresponding tag names,
24   close( FORUM );
                                                                                 subroutines for closing tags
25
26   sub forum
                                                                                 are followed by an underscore
                                                When a <forum> tag is found
27   {
                                                the forum subroutine is called
28      print( header );
29   }
                                                to output the HTTP header
30
 2001 Prentice Hall, Inc. All rights reserved.
31   sub forum_                        The forum_ subroutine is called               Outline
32   {                                 whenever a </forum> tag is found
33      print( br, start_center,
34         a( { -href => "/cgi-bin/addPost.pl?file=$file" },
                                                                            forum.pl
35              "Post a Message" ),
36         br, br,
37         a( { -href => "/cgi-bin/default.pl" },
38              "Return to Main Page" ),
39         end_center, end_html );
40   }
41
42   sub name_
43   {
                                                          Creates the basics of the page for
44      print( start_html( -title => $info{ "name" },
                                                          the user by making a table
45         -style => { -src => "/XML/site.css" } ),
46         start_table( { -width       => "100%",
47                        -cellspacing => "0",
48                        -cellpadding => "2"     } ),
49
50           Tr( td( { -class => "forumTitle" }, $info{ "name" } ) ),
51           end_table, br );
52   }
53
54   sub message
55   {
56      my ( $expat, $element, %attributes ) = @_;
57      $info{ "date" } = $attributes{ "timestamp" };
58   }
59
 2001 Prentice Hall, Inc. All rights reserved.
60 sub message_                                                                                     Outline
61 {
62      print( start_table( { -width                      => "100%",        Generates the code to insert a name
                                                                                           forum.pl
63           -cellspacing => "0",                                           and the message that the user left
64           -cellpadding => "2"                  } ),
65           Tr( [ td( { -class             => "msgTitle" }, $info{ "title" } ),
66               td( { -class           => "msgInfo"      },
67                     " by " . em( $info{ "user" } ) . " at " .
68                     span( { -class => "date" }, $info{ "date" } ) ),
69               td( { -class           => "msgText"      }, $info{ "text" } ) ] ),
70           end_table );
71 }
72                                                Places the string into $info, which will
73 sub text                                       eventually be placed into the info hash
74 {
75      my ( $expat, $string ) = @_;
76      $info{ $expat->current_element } = $string;
77 }




 2001 Prentice Hall, Inc. All rights reserved.
                                                                                                       Outline

                                                                                                 forum.pl




 Netscape Communicator browser window ©1999 Netscape Communications Corporation. Used
 with permission. Netscape Communications has not authorized, sponsored, endorsed, or approved
 this publication and is not responsible for its content.




 2001 Prentice Hall, Inc. All rights reserved.

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:1
posted:2/10/2012
language:
pages:97