source: trunk/gsdl/perllib/cpan/XML/XPath/Literal.pm@ 7909

Last change on this file since 7909 was 7909, checked in by mdewsnip, 20 years ago

CPAN module for processing XPath expressions.

  • Property svn:keywords set to Author Date Id Revision
File size: 1.8 KB
Line 
1# $Id: Literal.pm 7909 2004-08-06 05:11:55Z mdewsnip $
2
3package XML::XPath::Literal;
4use XML::XPath::Boolean;
5use XML::XPath::Number;
6use strict;
7
8use overload
9 '""' => \&value,
10 'cmp' => \&cmp;
11
12sub new {
13 my $class = shift;
14 my ($string) = @_;
15
16# $string =~ s/"/"/g;
17# $string =~ s/'/'/g;
18
19 bless \$string, $class;
20}
21
22sub as_string {
23 my $self = shift;
24 my $string = $$self;
25 $string =~ s/'/'/g;
26 return "'$string'";
27}
28
29sub as_xml {
30 my $self = shift;
31 my $string = $$self;
32 return "<Literal>$string</Literal>\n";
33}
34
35sub value {
36 my $self = shift;
37 $$self;
38}
39
40sub cmp {
41 my $self = shift;
42 my ($cmp, $swap) = @_;
43 if ($swap) {
44 return $cmp cmp $$self;
45 }
46 return $$self cmp $cmp;
47}
48
49sub evaluate {
50 my $self = shift;
51 $self;
52}
53
54sub to_boolean {
55 my $self = shift;
56 return (length($$self) > 0) ? XML::XPath::Boolean->True : XML::XPath::Boolean->False;
57}
58
59sub to_number { return XML::XPath::Number->new($_[0]->value); }
60sub to_literal { return $_[0]; }
61
62sub string_value { return $_[0]->value; }
63
641;
65__END__
66
67=head1 NAME
68
69XML::XPath::Literal - Simple string values.
70
71=head1 DESCRIPTION
72
73In XPath terms a Literal is what we know as a string.
74
75=head1 API
76
77=head2 new($string)
78
79Create a new Literal object with the value in $string. Note that &quot; and
80&apos; will be converted to " and ' respectively. That is not part of the XPath
81specification, but I consider it useful. Note though that you have to go
82to extraordinary lengths in an XML template file (be it XSLT or whatever) to
83make use of this:
84
85 <xsl:value-of select="&quot;I'm feeling &amp;quot;sad&amp;quot;&quot;"/>
86
87Which produces a Literal of:
88
89 I'm feeling "sad"
90
91=head2 value()
92
93Also overloaded as stringification, simply returns the literal string value.
94
95=head2 cmp($literal)
96
97Returns the equivalent of perl's cmp operator against the given $literal.
98
99=cut
Note: See TracBrowser for help on using the repository browser.