XML-XSH
view release on metacpan or search on metacpan
=item See also:
open_command close_command print_enc_command files_command
=back
=head2 close
=over 4
=item Usage:
close [B<id>]
=item Description:
Close the document identified by B<id>, removing its parse-tree from memory (note also that all nodes belonging to the document are removed from all nodelists they appear in). If B<id> is omitted, the command closes the current document.
=back
=head2 copy
=over 4
=item Usage:
copy B<xpath> B<location> B<xpath>
=item Aliases:
cp
=item Description:
Copies nodes matching the first B<xpath> to the destinations determined by the B<location> directive relative to the second B<xpath>. If more than one node matches the first B<xpath> than it is copied to the position relative to the corresponding nod...
The possible values for B<location> are: after, before, into, replace and cause copying the source nodes after, before, into (as the last child-node). the destination nodes. If replace B<location> is used, the source node is copied before the destina...
Some kind of type conversion is used when the types of the source and destination nodes are not equal. Thus, text, cdata, comment or processing instruction node data prepend, append or replace value of a destination attribute when copied before,after...
Attributes may be copied after, before or into some other attribute to append, prepend or replace the destination attribute value. They may also replace the destination attribute completely (both its name and value). To copy an attribute from one ele...
Elements may be copied into other elements (which results in appending the child-list of the destination element), or before, after or instead (replace) other nodes of any type except attributes.
Example: Replace living-thing elements in the document b with the coresponding creature elements of the document a.
xsh> copy a://creature replace b://living-thing
=back
=head2 count
=over 4
=item Usage:
count B<xpath>
=item Aliases:
print_value get
=item Description:
Calculate a given B<xpath> expression. If the result is a node-list, return number of nodes in the node-list. If the B<xpath> results in a boolean, numeric or literal value, return the value.
=back
=head2 create
=over 4
=item Usage:
create B<id> B<expression>
=item Aliases:
=over 4
=item Usage:
xcopy B<xpath> B<location> B<xpath>
=item Aliases:
xcp
=item Description:
xcopy is similar to B<copy>, but copies *all* nodes matching the first B<xpath> to *all* destinations determined by the B<location> directive relative to the second B<xpath>. See B<copy> for detailed description of B<xcopy> arguments.
Example: Copy all middle-earth creatures within the document a into every world of the document b.
xsh> xcopy a:/middle-earth/creature into b://world
=back
=head2 xinsert
=over 4
=item Usage:
xinsert B<node-type> B<expression> [namespace B<expression>] B<location> B<xpath>
=item Aliases:
xadd
=item Description:
Use the B<expression> to create a new node of a given B<node-type> in the B<location> relative to the given B<xpath>.
For element nodes, the format of the B<expression> should look like "E<lt>element-name att-name='attvalue' ...E<gt>". The B<E<lt>> and B<E<gt>> characters are optional. If no attributes are used, the expression may simply consist the element name. No...
Attribute nodes use the following syntax: "att-name='attvalue' [...]".
For the other types of nodes (text, cdata, comments) the expression should contain the node's literal content. Again, it is necessary to quote all whitespace and special characters as in any expression argument.
The B<location> argument should be one of: B<after>, B<before>, B<into>, B<replace>, B<append> or B<prepend>. See documentation of the B<location> argument type for more detail.
The namespace B<expression> is only valid for elements and attributes and must evaluate to the namespace URI. In that case, the element or attribute name must have a prefix. The created node is associated with a given namespace.
Example: Append a new Hobbit element to the list of middle-earth creatures and name him Bilbo.
xsh> xadd element "<creature race='hobbit' manner='good'>" \
into /middle-earth/creatures
xsh> xadd attribute "name='Bilbo'" \
into /middle-earth/creatures/creature[@race='hobbit'][last()]
=item See also:
insert_command move_command xmove_command
=back
=head2 xmove
=over 4
=item Usage:
xmove B<xpath> B<location> B<xpath>
=item Aliases:
xmv
=item Description:
Like B<xcopy>, except that B<xmove> removes the source nodes after a succesfull copy. Remember that the moved nodes are actually different nodes from the original ones (which may not be obvious when moving nodes within a single document into location...
See B<xcopy> for more details on how the copies of the moved nodes are created.
The following example demonstrates how B<xcopy> can be used to get rid of HTML B<E<lt>fontE<gt>> elements while preserving their content. As an exercise, try to find out why simple B<foreach E<sol>E<sol>font { xmove node() replace . }> would not work...
Example: Get rid of all E<lt>fontE<gt> tags
while //font[1] {
foreach //font[1] {
xmove ./node() replace .;
echo foo "bar" # prints: foo bar
echo foo"bar" # prints: foobar
echo foo'"bar"' # prints: foo"bar"
echo foo"'b\\a\"r'" # prints: foo'b\a"r'
$a="bar"
echo foo$a # prints: foobar
echo foo\$a # prints: foo$a
echo '$a' # prints: '$a'
echo "'$a'" # prints: 'bar'
echo "${{//middle-earth/creatures}}" # prints: 10
echo '${{//middle-earth/creatures}}' # prints: ${{//middle-earth/creatures}}
echo ${{//creature[1]/@name}} # !!! prints: 1
echo ${(//creature[1]/@name)} # prints: Bilbo
echo ${{{ join(",",split(//,$a)) }}} # prints: b,a,r
There is one more special type of expressions, so called ``here-documents'' following syntax of similar constructs in Bash and Perl. Following a B<E<lt>E<lt>> you specify a string to terminate the quoted material, and all lines following the current ...
Example:
$a="bar"
echo foo <<END baz;
xx $a yy
END
# prints foo xx bar yy baz
echo foo <<"END" baz;
xx $a yy
END
# same as above
echo foo <<'END' baz;
xx $a yy
END
# prints foo xx $a yy baz
=item B<filename>
An B<expression> which interpolates to a valid file name or URL.
=item B<id>
An identifier, that is, a string beginning with a letter or underscore, and containing letters, underscores, and digits.
=item B<location>
One of: B<after>, B<before>, B<into>, B<append>, B<prepend>, B<replace>.
NOTE: XSH 1.6 introduces two new values for location argument: B<append> and B<prepend> and slighlty changes behavior of B<after> and B<before>!
This argument is required by all commands that insert nodes to a document in some way to a destination described by an XPath expression. The meaning of the values listed above is supposed be obvious in most cases, however the exact semantics for loca...
B<afterE<sol>before> place the node right afterE<sol>before the destination node, except for when the destination node is a document node or one of the source nodes is an attribute: If the destination node is a document node, the source node is attac...
B<appendE<sol>prepend> appendsE<sol>prepends the source node to the destination node. If the destination node can contain other nodes (i.e. it is an element or a document node) then the entire source node is attached to it. In case of other destinati...
B<into> can also be used to place the source node to the end of an element (in the same way as B<append>), to attach an attribute to an element, or, if the destination node is a text node, cdata section, processing-instruction, attribute or comment, ...
B<replace> replaces the entire destination node with the source node except for the case when the destination node is an attribute and the source node is not. In such a case only the value of the destination attribute is replaced with the textual con...
=item B<node-type>
One of: element, attribute, text, cdata, comment, chunk and (EXPERIMENTALLY!) entity_reference. A chunk is a character string which forms a well-balanced peace of XML.
Example:
add element hobbit into //middle-earth/creatures;
add attribute 'name="Bilbo"' into //middle-earth/creatures/hobbit[last()];
add chunk '<hobbit name="Frodo">A small guy from <place>Shire</place>.</hobbit>'
into //middle-earth/creatures;
=item B<perl-code>
A block of perl code enclosed in curly brackets or an expression which interpolates to a perl expression. Variables defined in XSH are visible in perl code as well. Since, in the interactive mode, XSH redirects output to the terminal, you cannot simp...
For more information about embedded Perl code in XSH, predefined functions etc. see B<Perl_shell>.
Example:
xsh> $i="foo";
xsh> eval { echo "$i-bar\n"; } # prints foo-bar
xsh> eval 'echo "\$i-bar\n";' # exactly the same as above
xsh> eval 'echo "$i-bar\n";' # prints foo-bar too, but $i is
# interpolated by XSH. Perl actually evaluates echo "foo-bar\n";
=item B<xpath>
XSH supports arbitrary XPath expression as defined in W3C recommendation at http:E<sol>E<sol>www.w3.orgE<sol>TRE<sol>xpath. (Nice interactive XPath tutorials and references can be found at http:E<sol>E<sol>www.zvon.org.) In XSH, XPath expressoin may ...
As an extension, the following XPath extension functions are defined in the XSH namespace:
B<xsh:doc(id-string)> - returns a nodelist consisting of the document node associated in XSH with an identifier given in B<id-string>.
B<xsh:matches(match-string,regexp-string)> - returns B<true> if B<match-string> matches the regular expression given in B<regexp-string>. Otherwise returns B<false>.
B<xsh:grep(node-set, regexp-string)> - returns a node set consisting of nodes of the given B<node-set> whose content (as returned by the built-in XPath function B<string()>) matches the regular expression given in B<regexp-string>.
B<xsh:same(node-set1, node-set2)> - returns B<true> if the given node sets both contain the same node (in XPath, this can also be expressed as B<count(node-set1E<verbar>node-set2)+count(node-set1)+count(node-set2)=1>).
Example: Open a document and count all sections containing a subsection
xsh scratch:/> open v = mydocument1.xml;
xsh v:/> open k = mydocument2.xml;
xsh k:/> count //section[subsection]; # searches k
xsh k:/> count v://section[subsection]; # searches v
=back
( run in 1.565 second using v1.01-cache-2.11-cpan-39bf76dae61 )