A Python SOAP Client
The SOAP Toolkit ships with a Calculator service. This service offers four different operations that it performs on two supplied parameters, much like a basic calculator. This example is similar in functionality to the VBScript sample client that ships with the Toolkit. The most significant difference between the two is that your client is implemented entirely in Python from scratch, not in VBScript. The calculator operations are add, subtract, muitipiy, and divide. If your SOAP Toolkit is...
Converting the flat file to XML
The code to convert the flat file to XML is primarily inside the FlatfileParser created earlier. Use the Python CGI API to grab the flat file from the query string Parse query string for flat file flatfile query.getvalue flatfile, 0 failure Conversion request not found or incorrectly formatted. print instatiate flat file parser amp display file ffp FlatfileParser print lt h1 gt Flat File lt h1 gt print lt p gt Flat file received lt p gt print lt p gt lt pre gt flatfile lt pre gt lt p gt After...
Business Process and Modeling
In ebXML, business process and information modeling is not mandatory. But if it is used, it is required to be UMM UN CEFACT Modeling Methodology . This is a modeling language that uses UML Universal Modeling Language , which is commonly used in object-oriented programming design. The UMM approach separates operational and functional views and relies on two view constructs the Business Operational View BOV and the Functional Service View FSV . The operational view covers such things as the...
Creating the CGI Script
The xslt.cgi script pulls the stylesheets together and coordinates the processing and updating of the XML on disk. While this application lets you edit and display XML in your browser, it only consists of a single CGI script and two XSL sheets. The source data that may constantly change is also stored on disk as XML. XSLT transformations can be done programmatically using the xml.xslt.processor.Processor class provided 4XSLT is installed, as shown earlier . When the CGI script launches, it...
MSXML Node Object
The Node object is the fundamental object of the DOM and of MSXML3.0. This interface supports the common methods used throughout this book when working with the DOM. Appends the supplied node to child NodeList for this element. cloneNode Creates a new node that is a complete copy of this particular node. Returns true if the node has children. insertBefore newChild, referenceChild Takes the node supplied as newChild and inserts it in this node's NodeList immediately prior to the supplied...
EntityResolver
This handler interface is used to allow the application to control how external entities are loaded. This can be used to support the use of an application-level cache or use one or more of the techniques for mapping public identifiers to system identifiers. The parser calls this method to convert the identifiers for an external entity into a system identifier to load. The systemId parameter is the URL provided in an entity declaration, and publicId is the public identifier, or None if there...
Performs validation against the document based on the declared DTD MSXML
This read write property determines whether synchronous or asynchronous document retrieval is used when downloading. This read-only property contains the doctype associated with the XML document. This read write property contains the XML document's root element. This read-only property contains the DOMImplementation object for the document instance. This read-only property contains a collection of all of the namespaces used in the document. This read write property represents the event handler...
Mixed Content
This content may contain character data. This is the most common arrangement in text documents lt news title XML from Outer Space gt This article describes XML transmissions from outer space. lt h1 gt Not a Meteor lt h1 gt lt para gt Contrary to earlier reports, the XML that has landed outer space is not a meteor. lt para gt In this example, elements and character data are mixed beneath the news element. Elements that have a mixed content model are not required to allow other elements as...
Baking Cookies for the Server
Any hungry server administrator may be disappointed to learn that the cookies your browser sends to his web site are electronic. Cookies are frequently delivered to web servers by browsers to indicate a special identification for your browser. Your browser keeps the cookie and returns it whenever the same web site or document is requested. This lets the web server personalize site content for you, or connect you with some specific data that may be held in a database, such as your profile...
Example B gentdpya script to print XML definitions
Generate HTML for terms and definitions directly from XML specification. XML source must come from standard input. from xml.sax import ContentHandler from xml.sax import make parser class XMLSpecHandler ContentHandler Class implements part of SAX API to pull term definitions out of the XML Specification source file. def startElement self, name, attrs if name termdef self.inTermDef 1 self.strTermDefContents print lt p gt lt b gt attrs.get 'term', lt b gt lt br gt def characters self, ch if...
Transforming XML with XSLT
The XML Stylesheet Language, or XSL, consists of two component specifications XSL Transformations XSLT and XSL Formatting Objects XSL-FO . The transformation language is used to translate XML documents from their original form to some other form, which may be XML, HTML, or anything else including plain text . XSLT is covered in more detail in Chapter 6. The XSL-FO specification describes specific presentational styling and is used to describe a formatted document that could be printed to a...
Using Pulldom
pulldom , which also may be imported from the xml.dom package, may be just the thing to save your life when faced with the task of taking a portion of a large XML document and creating a DOM instance of the subset for manipulation. pulldom essentially allows for the construction of selected portions of a DOM based on SAX events. The module uses minidom for the actual nodes it returns. pulldom seeks to be a middle ground between the DOM and SAX. pulldom wants to overcome the state-management the...
F Python XML Tools
The Python XML Tools collection is built on top of PyXML and features two GTK widgets XmlTree and XmlEditor. The Python XML Tools are available from http www.logilab.org xmltools . These packages are used to display XML files XmlTree , as well as edit them XmlEditor . XmlTree displays XML files in a tree-like form, familiar to those who've used file browsers on Windows, KDE, or GNOME. This structure takes the form of a GTK widget it's derived from the GtkCTree Widget. The API features several...
NodeList
The NodeList interface is a generic list containing the child nodes of the node, regardless of their specific subtype. In other words, Text nodes are in the list alongside Element and Processinginstruction nodes. This interface is not defined in terms of the Node interface. This read-only attribute indicates the number of nodes in the list. If the length is 10, then the actual indexes are 0 through 9. This method returns the node with the corresponding index in the NodeList. If the index is out...
ebXML Document Structure
The dialect portion of ebXML deals with message formats. The specification puts forth guidelines that must be used when creating ebXML documents. Simple structure is used in ebXML. The specification requires that element and attribute names follow a standard capitalization arrangement, similar to Java names. Using a scheme described as upper and lower camel-case, ebXML element and attribute names have the following capitalization format lt MyElementName myElementAttribute Additionally,...
Example A DTDHandler class implements DTDConsumer in xpHandlerspy
DTDHandler -- implements xmlproc's DTDConsumer Interface def init self,parser self.parser parser def new general entity self,name,val print General Entity Received , name def new external entity self,ent name,pub id,sys id,ndata print External Entity Received , ent_name def new element type self,elem name,elem cont print New Element Type Declaration , elem name, Content Model , elem_cont def new attribute self,elem,attr,a type,a decl,a def print New Attribute Declaration , attr Example 7-5 is...
XMLMessage class
Using the XMLMessage class is simple. Messages can either be created from an XML string, an XML document object, or loaded from a file. Once created, access functions allow you to get at specific parts of the message document more quickly. The methods getHeader and getBody allow you to quickly extract header or body data. The method setHeader and setBody allow you to manipulate an XML message before sending it to another system for processing. The whole message can be swapped in and out as...
XMLMessage code listing
Example 10-7 shows the complete listing of XMLMessage.py. Example 10-7. XMLMessage.py XMLMessage.py - a wrapper for message.xml documents from xml.dom.ext import PrettyPrint from xml.dom.ext.reader.Sax2 import FromXmlStream, FromXml XMLMessage encapsulates a message.xml document from class users. def init self self._dom self._xml setBody strXML - The supplied XML is used for the body of the XML message. xmlstr FromXml str lt message gt self._header strXML lt message gt return...
NamedNodeMap
This interface is similar to a NodeList, but its designed to allow the accessing of nodes from the list by name. Nodes are not guaranteed to be in any particular order within the map. This interface is not defined in terms of the Node interface. The number of nodes in the map. As with the NodeList, if length is 10 then actual indexes are 0 through 9. This method retrieves the node specified by the parameter name. The node is returned if found, or None is returned if not. getNamedItemNS...
E MSXML Support for XSLT
MSXML3.0 provides support for XSL transformations without any additional software. The parser features a transformNode method that usually accepts a stylesheet as a parameter in DOM form and returns the result of processing the current document with the supplied stylesheet. For example objXML objXSL strTransformedXML objXML.transformNode objXSL In the simplest case, as shown in the preceding code, two DOM instances are created. One DOM instance is needed to hold the source document, the other...
Example E booksxml
lt book name Python and XML gt lt section name Appendix E type Appendix gt lt chapterTitle gt Appendix E lt chapterTitle gt lt bodytext gt This appendix focuses on techniques for using lt bodytext gt lt section gt lt book gt Using MSXML, it's easy to take this document apart. But before you can work with MSXML, you have to import the correct library to access COM objects win32com.client . Additionally, for the call to Dispatch, you need the ProgID of the Microsoft XML parser. If you've...
Example productxml with a bad product element
lt xml version 1.0 gt lt DOCTYPE product lt ELEMENT name PCDATA gt lt ELEMENT price PCDATA gt lt ELEMENT product name, price gt lt name gt Bean Crusher lt name gt lt product gt Try out xvcmd.py the validator from your command line C gt python c python2 0 xmldoc demo xmlproc xvcmd.py product.xml xmlproc version 0.70 E product.xml 9 11 Element 'product' ended, but not finished Parse complete, 1 error s and 0 warning s As suspected, an error occurs. The problem is that in the DTD, we explicitly...
F Pyxie
The Pyxie package, developed by Sean McGrath, is available from http pyxie.sourceforge.net and is based around a line-oriented notation known as PYX. PYX and Pyxie are an alternative to the SAX and DOM, and is, according to its author, geared for pipeline processing, in which one application's output is fed as input to the next application. This idiom is common among Unix tools, but is also used on Windows, though it is not common there for end-user tools. Pyxie can parse an XML document into a...
F Online Resources
The Python XML community is centered around the Python XML Special Interest Group, or XML-SIG. The group has a web page at As with most Python SIGs, everything really happens on a mailing list. Information on the mailing list, including both links to the list archives and a subscription form, is available at the XML-SIG web page. The XML-SIG is not only responsible for maintaining the PyXML package used extensively in this book, but also the Python XML Topic Guide, containing overviews of...
CDATASection
This interface allows you to manipulate CDATASection nodes. Unlike Text nodes, the normalize method does not merge adjacent CDATASection nodes. CDATASection nodes are used to hold special characters that may be mistaken as XML by a parser. This interface represents the DTD of your XML document, or it is None if one does not exist. Its attributes are read-only, per the DOM specification, because the W3C is uncertain about how the different XML schema efforts will impact DTD usage. This attribute...
MSXML NodeList Object
The NodeList is commonly returned by DOM methods that return a collection or list of Nodes. The NodeList features some special methods and properties to make working the list easier. This method returns an item from the list at the given index zero-based, as Python sequence indexes . If there is no node at index, returns None. This method returns the next node in the list, based on the internal iterator. This method resets the internal iterator to zero.
E Setting Up MSXML
Fortunately, when using Python for your XML development, you don't have to give up MSXML3.0. The Microsoft parser is fully accessible from Python using Python's COM support. If you haven't already, you'll need to visit http www.python.org and install win32all.exe as a supplement to your Python installation. This Windows-specific package provides support for Microsoft's Component Object Model COM framework. The win32all.exe package must be installed prior to using COM objects. More details of...