Creating Output Scopes

[Previous] [Next]

All output from an OmniMark program goes to OmniMark's built-in current output. You do not usually need to explicitly state where you want output to go; everything goes to current output. You can change the destination of current output within a rule.

 element duck local stream bar open bar as file "Bar.txt" using output as bar do output "quack quack quack %c" done 

This rule temporarily changes the current output to the file Bar.txt. Any output that occurs in the do...done block following using output as goes to the new destination. Once the block is finished, current output reverts to its original destination. Note, however, that the output statement contains the parse continuation operator (%c). Thus the new output destination is in effect for all processing that occurs as part of the parsing of the duck element. This becomes the current output scope for all the output statements occurring in any of the markup rules fired as a result of parsing the duck element.

To understand how creating an output scope works, consider the XML file Duck.xml. This file contains a valid schema and the following markup:

 <line> A <player>Duck</player> goes up to the <player>bartender</player> </line> 

Now consider the following OmniMark program:

 global stream words global stream players process open words as file "words.txt" open players as file "players.txt" using output as words do xml-parse instance scan file "duck.xml" output"%c" done element line output "%c" element player using output as players do output "%c " done 

Running this program will leave you with two files: words.txt will contain " A goes up to the ", and people.txt will contain "Duck bartender ". Look closely at this code to make sure you understand which output destination is in effect in the element line rule. Do you understand why the output ended up in the files it did? If you are comfortable with this, you know most of what you need to know about how OmniMark handles output. If you are still having trouble here, don't worry. This hierarchical programming model is new to most programmers. Because an XML document is a hierarchical object, this model is critical to processing XML documents. Just remember that output happens in a hierarchical scope. As you use OmniMark or any hierarchical programming language, you will start to see how the process works.

You can slightly shorten the last rule of the previous program by using put as shorthand for the using output as block:

 element player put players "%c " 

Here put establishes the stream people as the current output scope and outputs the string "%c " to the current output scope. Thus, this output scope will again be in effect for all rules fired as a result of "%c".

OmniMark's current output is a powerful mechanism that simplifies code by eliminating the need to always state where output is going. Once you've set the destination of the current output, all output goes to that destination unless you explicitly send it elsewhere. Note that this means that an output statement can execute in a different current output scope depending on the context in which it occurs. The ability to separate the creation of output from the establishment of output destination is a powerful and flexible feature of OmniMark programming.

Current output has the additional feature of being able to have more than one destination at a time. The following code will place "A duck walks into a I've been barred!" in both the file outfile.txt and in the variable my-buffer:

 global stream my-file global stream my-buffer process open my-file as file "outfile.txt" output-to my-file submit "A duck walks into a bar" find "bar" open my-buffer as buffer using output as my-buffer and #current-output do output "I've been barred!" done 

The keyword #current-output stands for all destinations of current output. You can use #current-output to add a new destination to the output destinations currently active.



XML and SOAP Programming for BizTalk Servers
XML and SOAP Programming for BizTalk(TM) Servers (DV-MPS Programming)
ISBN: 0735611262
EAN: 2147483647
Year: 2000
Pages: 150

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net