The next statement sets the Global property on the regular-expression object to True, instructing the regular-expression engine to match all occurrences of the pattern rather than just a single occurrence. The Execute method returns a collection of matching patterns, and a message box displays the data for each PI. To run the code, assign the name badqueryflat.xml to the template from Listing 4 and save the template to the November XML virtual directory you created for the previous example. Then, save the VBScript code from Listing 3 to the same directory with the filename of RegExpSearch.vbs and run the script.
The SAX2 XML parser interface. Using the SAX2 XML parser interface is more efficient than using DOM for detecting errors because SAX2 doesn't generate an in-memory representation of the XML result. This characteristic also makes SAX2 suitable for large data sets. SAX2 is composed of a set of interfaces that supply callback functions to notify your program when the parser encounters different types of XML tokens (e.g., elements, comments, PIs). By using a callback function to catch PI tokens from the parser, your application can search for errors in an XML result.
Web Listing 1 shows a minimalist SAX2 content-handler class. The MyContent class derives from a base class that Microsoft supplies with its "JumpStart for Creating a SAX2 Application with C++" kit at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/xmlsdk30/htm/sax2c_jumpstart_jumpstart.asp?frame=true. The base class implements stub handler methods for all methods on the ISAXContentHandler interface. Using the base class lets you easily implement only the handler methods that you need to complete the task at hand. To detect SQL XML errors, you implement only the processingInstruction method, which prints a message if it encounters a SQL XML error PI.
Web Listing 2 shows the main program that instantiates the SAX2 parser, registers the MyContent handler for callbacks, and invokes the XML parser to parse the document from the URL passed as a command-line parameter. I copied the source code for the main program directly from the JumpStart kit. To run the program, you first need to compile the C++ project. You can download a complete C++ project, including an executable, from the SQL Server Magazine Web site at http://www.sqlmag.com (see "More on the Web" for download instructions). After you compile the program, navigate to the Debug directory beneath the project and run the following command line:
CPPSaxSample http://localhost/November/
template/badquery.xml
The SQL XML error PI data appears on the console.
You can use any of these three techniques to detect updategram errors. Let's look at an updategram example that incorporates a technique similar to the DOM technique I described earlier. Listing 5 shows an XML template that uses an XSLT style sheet instead of an XPath query to display error information.
The template consists of a root tag that contains an XSLT declaration and an updategram. Using the root tag ensures that the updategram's result is a well-formed XML document. The XSLT declaration specifies the style sheet that formats the result. The <updg:sync> block and its children define the updategram. Note that this updategram always generates an error because the author, Abraham Bennet, already exists in the authors table in the Pubs database. The mapping-schema attribute on the updg:sync tag refers to the mapping schema that Listing 6 shows. The mapping schema maps attributes from the author tag to the authors table in the Pubs database. When SQL Server executes the updategram, two outcomes are possible. If an error occurred, SQL Server replaces the updg:sync tag and all its content with the SQL XML error PI. If the updategram was successful, SQL Server removes the updg:sync tag. For details about updategram processing, see Michael Otey, "XML Updategrams," January 2001. After SQL Server processes the updg:sync tag, the XSLT style sheet processes the resulting XML document. In this example, the XSLT will format an HTML page to display either the error or a message that indicates that errors occurred.
To run the code, store the template under the filename update-authors.xml in the directory that you used in the earlier examples. To the same directory, store the mapping schema from Listing 6 and the XSLT style sheet from Listing 7 with the filenames update-authorsschema.xml and showErrors.xslt, respectively. Invoke the template by using the following URL from IE:
http://localhost/November/template/updateauthors.xml
Figure 2 shows the HTML result that the style sheet from Listing 7 produces. The HTML includes data from two error PIs that SQL Server generates. Note that to retrieve the data from the PI, the style sheet uses the xsl:value-of tag as well as the XPath expression that you used in Listing 2 to select SQL XML error PIs.
Detecting errors isn't difficult when you use SQL Server 2000's XML functionality. Just remember to choose your error-processing technique to match your application scenario.
End of Article
Prev. page
1
[2]
next page -->