|
|
|
|||||
|
|
||||||
|
|
Usage From command line runscript inputFileNAme outputFileName scriptFileName From the API The bsh.Transformer class is a helper class to invoke a transformation from within Java Applications. Create a new
Transformer object, and call transform(): Document in = ..//
input document Dependencies RAV Script is an extension of Beanshell, hence the syntax is a
superset of Beanshell. Beanshell has syntax very similar to Java, but if you
want to learn more, Visit www.beanshell.org
to learn more. RAV Script uses JDOM for the XML document model. It is not necessary
to be a JDOM expert to use RAV Script, but for advanced users, visit www.jdom.org to learn about JDOM. Scripting Transformations The script should have one call to root(), to initialize the output
document. The rest of the script is all about adding children to this
document. The script has two context variables, $input and $output, which are
fundamental for how RAV Script works. These variables need not be referred
directly in the script, as they are used behind the scenes. $input is initialized with the root element of the input document, and
changes when the processing context changes. The processing context changes
inside a foreach(String xpath) statement (the syntax is explained later).
Inside the foreach statement loop, the $input context variable takes the
value of each XML node matched by the XPath string. The idea is similar to
match() in XSLT. $output has to be initialized by calling root(Element r) once within
the script. This sets a JDOM Element as the initial parent. After the call,
addelem statements are used to add children to the parent. If there is a
block defined with the addelem statement, the $output context variable takes
the value of the element just added, and becomes the new parent. So within
the block, any use of addelem will result in adding children to the new
parent (not the root). Changes to BeanShell Unlike Java or Beanshell, RAVScript makes local variables available to
called methods which are declared in the script. (For those familiar with
Beanshell, the NameSpace of the method is a child of calling context's
Namespace). So you can declare methods and call them within the main script,
and $input and $output will be propogated behind the scenes. This is similar
in principle to apply-templates in XSLT. $input can be accessed anywhere in the script, and $output can be
accessed anywhere after the call to root(). However this is not necessary for
most purposes. Extensions to BeanShell syntax This syntax is merely a shortcut for creating Elements. So
although they can appear to be empty, you can add children to them later.
Note the nested children in the 'z' Element. The syntax is
quite flexible. You could get the same results as above with: Note that this is not pure XML, but designed to look as close to XML
as possible. You can also specify a NameSpace for Elements or Attributes. NameSpace ns = Namespace.getNamespace("ns","http://xxx/xxx"); 2. foreach When foreach is used in the above example, the XPath expression
“x” is applied on $input, and the results are assigned to $input
in each iteration. It is also possible to match nodes in documents (or elements) other
than the input document. This effectively enables multiple input documents,
something not possible with XSLT (at least with version 1.0). z = <z> <x>
"some more text" </x> <x> "foo" </x>
</z>; foreach ("x" in z) // Xpath applied on the
variable z, not on $input In the final variation, foreach can assign the result of foreach to a
variable other than $input z = <z> <x>
"some more text" </x> <x> "foo" </x>
</z>; foreach foo ("x" in z) // Xpath applied on the
variable z, result assigned to foo 3. addelem In the above example, addelem adds children to $output. It is also
possible to add children to other elements using the following syntax: y = <y> “the root” </y>; addelem <x> to y; 4. root 5. Helper functions: text, attribute, evaluate,
valueOf, copy, compare |
|||||
|
template provided by: |
Copyright © 2002 ravana.com. All rights reserved |