Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Apress committed Oct 7, 2016
0 parents commit 17e8e7a
Show file tree
Hide file tree
Showing 166 changed files with 2,254 additions and 0 deletions.
Binary file added 3005.pdf
Binary file not shown.
Binary file added 3006.pdf
Binary file not shown.
Binary file added 3007.pdf
Binary file not shown.
Binary file added 3014.pdf
Binary file not shown.
8 changes: 8 additions & 0 deletions 6668/Chapter01/0553211757.xml
@@ -0,0 +1,8 @@
<book isbn="0553211757">
<title>Crime and Punishment</title>
<author id="923117"/>
<publisher>Bantam Classics</publisher>
<weight>9.3</weight>
<pages>576</pages>
</book>

6 changes: 6 additions & 0 deletions 6668/Chapter01/author-923117.xml
@@ -0,0 +1,6 @@
<author id="923117">
<name>Fyodor Dostoevsky</name>
<birthdate>November 11 1821</birthdate>
<deathdate>February 9 1881</deathdate>
</author>

23 changes: 23 additions & 0 deletions 6668/Chapter02/2-1.pl
@@ -0,0 +1,23 @@
#!/usr/bin/perl -w
use strict;
use XML::LibXML;
use DB_File;
$DB_BTREE->{'flags'} = R_DUP ;
my $datadir = "./xml-src/";
my $indexdir = "./index/";
my $parser = new XML::LibXML;
my %btree;
tie %btree, 'DB_File', "$indexdir/words.index", O_RDWR|O_CREAT, 0666, $DB_BTREE,
or die "Cannot open $indexdir/words.index: $!\n";

opendir my $dir, $datadir;
while (my $file = readdir($dir)) {
next if ($file =~ /^\./);
my $dom = $parser->parse_file("$datadir/$file");
foreach my $node ($dom->findnodes("/Synset/Word")) {
my ($textnode) = $node->findnodes("text()");
my $lcname = lc($textnode->getData);
if ($textnode) { $btree{$lcname} = $file; }
}
}

23 changes: 23 additions & 0 deletions 6668/Chapter07/7-1.xml
@@ -0,0 +1,23 @@
<person id="6641">
<name>
<last>Brown</last>
<first>Jim</first>
<middle>Austin</middle>
<nick>Big</nick>
</name>
<age>24</age>
<phone>
<office>612-555-0091</office>
<home/>
</phone>
<street> Attn: Jim Brown
Pleax Systems, Inc.
18520 25th Ave
</street>
<city>Minneapolis</city>
<state>MN</state>
<sex>male</sex>
<hobby>boats</hobby>
<hobby>carpentry</hobby>
</person>

6 changes: 6 additions & 0 deletions 6668/Chapter07/7-10.xq
@@ -0,0 +1,6 @@
declare namespace my = "http://brians.org/temperature";
declare function my:celsius-to-fahrenheit ($celsius as xs:decimal) as xs:decimal {
($celsius + 32) * (9 div 5)
};
my:celsius-to-fahrenheit(15)

5 changes: 5 additions & 0 deletions 6668/Chapter07/7-11.xqm
@@ -0,0 +1,5 @@
module namespace temp = "http://brians.org/temperature";
declare function temp:celsius-to-fahrenheit ($celsius as xs:decimal) {
($celsius + 32) * (9 div 5)
};

3 changes: 3 additions & 0 deletions 6668/Chapter07/7-12.xq
@@ -0,0 +1,3 @@
import module namespace temp = "http://brians.org/temperature" at "temperature.xqm";
temp:celsius-to-fahrenheit(10)

4 changes: 4 additions & 0 deletions 6668/Chapter07/7-13.xq
@@ -0,0 +1,4 @@
for $person in collection("people.dbxml")/person
where starts-with($person/phone, "612")
return $person

5 changes: 5 additions & 0 deletions 6668/Chapter07/7-14.xq
@@ -0,0 +1,5 @@
for $person in collection("people.dbxml")/person
for $phone in $person/phone
where starts-with($phone, "612")
return $person

4 changes: 4 additions & 0 deletions 6668/Chapter07/7-15.xq
@@ -0,0 +1,4 @@
for $person in collection("people.dbxml")/person
where some $phone in $person/phone satisfies (starts-with($phone, "612"))
return $person

4 changes: 4 additions & 0 deletions 6668/Chapter07/7-16.xq
@@ -0,0 +1,4 @@
for $person in collection("people.dbxml")/person
where some $phone in $person/phone satisfies (matches($phone, "^612"))
return $person

4 changes: 4 additions & 0 deletions 6668/Chapter07/7-17.xq
@@ -0,0 +1,4 @@
let $areacode := "612"
let $match := concat("^", $areacode, "[-\\.]\\d{3}[-\\.]\\d{4}$")
return matches("612.423.1124", $match)

4 changes: 4 additions & 0 deletions 6668/Chapter07/7-18.xq
@@ -0,0 +1,4 @@
for $document in collection("people.dbxml")/*
where $document[dbxml:metadata("dbxml:name") = "person1"]
return $document

5 changes: 5 additions & 0 deletions 6668/Chapter07/7-19.xq
@@ -0,0 +1,5 @@
for $x in collection("people.dbxml")/person/name/first
for $y in collection("synsets.dbxml")/Synset/Word
where contains($y, $x)
return (string($x), "=>", string($y))

13 changes: 13 additions & 0 deletions 6668/Chapter07/7-2.xml
@@ -0,0 +1,13 @@
<Synset fileVersion="1.0" pos="n">
<Id>14861</Id>
<WnOffset version="2.1" pos="n">02772480</WnOffset>
<LexFileNum>06</LexFileNum>
<SsType>n</SsType>
<Word lexId="0">baseball</Word>
<Pointers>
<Hypernym>14746</Hypernym>
<Hypernym>14866</Hypernym>
</Pointers>
<Gloss>a ball used in playing baseball</Gloss>
</Synset>

14 changes: 14 additions & 0 deletions 6668/Chapter07/7-20.xml
@@ -0,0 +1,14 @@
<Synset fileVersion="1.0" pos="n">
<Id>41886</Id>
<WnOffset version="2.1" pos="n">07647890</WnOffset>
<LexFileNum>13</LexFileNum>
<SsType>n</SsType>
<Word lexId="0">banana</Word>
<Pointers>
<Hypernym>41581</Hypernym>
<Meronym type="component">65855</Meronym>
<Meronym type="component">65852</Meronym>
</Pointers>
<Gloss>elongated crescent-shaped yellow fruit with soft sweet flesh</Gloss>
</Synset>

13 changes: 13 additions & 0 deletions 6668/Chapter07/7-21.xq
@@ -0,0 +1,13 @@
declare namespace my = "http://brians.org/synsets";
declare function my:hypernyms ($synset) {
let $hyp := $synset/Pointers/Hypernym[1]/string()
return
if (empty($hyp))
then ($synset/Word)[1]/string()
else
let $next := my:hypernyms(collection("synsets.dbxml")/Synset[Id = $hyp])
return concat($next, " => ", $synset/Word[1])
};

my:hypernyms((collection("synsets.dbxml")/Synset[Word="banana"])[1])

11 changes: 11 additions & 0 deletions 6668/Chapter07/7-22.xq
@@ -0,0 +1,11 @@
declare namespace my = "http://brians.org/temperature";
declare function my:binary ($dec as xs:decimal) {
if ($dec eq 0 or $dec eq 1)
then $dec
else
let $m := xs:integer($dec div 2)
let $j := $dec mod 2
let $D := my:binary($m)
return concat(string($D), string($j))
};

16 changes: 16 additions & 0 deletions 6668/Chapter07/7-23.xq
@@ -0,0 +1,16 @@
declare namespace my = "http://brians.org/synsets";
declare function my:steps ($synset) as element() {
let $hyp := $synset/Pointers/Hypernym[1]/string()
return
if (empty($hyp))
then element step { attribute name {($synset/Word)[1]} }
else
let $next := my:steps(collection("synsets.dbxml")/Synset[Id = $hyp])
return element step {
attribute name { $synset/Word[1] },
$next
}
};

my:steps((collection("synsets.dbxml")/Synset[Word="banana"])[1])

24 changes: 24 additions & 0 deletions 6668/Chapter07/7-24.xq
@@ -0,0 +1,24 @@
declare namespace my = "http://brians.org/synsets";
declare function my:hypernyms ($synset) {
let $hyp := $synset/Pointers/Hypernym[1]/string()
return
if (empty($hyp))
then $synset/Id/string()
else
let $next := my:hypernyms(collection("synsets.dbxml")/Synset[Id = $hyp])
return ($synset/Id/string(), $next)
};
declare function my:tree ($idlist) {
if (empty($idlist))
then ()
else
element step {
attribute id {$idlist[1]},
attribute name {collection("synsets.dbxml")/Synset[Id = $idlist[1]]/Word[1]},
my:tree(remove($idlist, 1))
}
};

let $list := my:hypernyms((collection("synsets.dbxml")/Synset[Word="flan"])[1])
return my:tree(reverse($list))

11 changes: 11 additions & 0 deletions 6668/Chapter07/7-27.xq
@@ -0,0 +1,11 @@
declare namespace my = "http://brians.org/synsets";
declare variable $rand as xs:decimal external;
declare function my:random-synset () {
let $count := 250000 (: the number of records for our set :)
let $synset := (collection("steps.dbxml")//*[@id="9"]//*)[($count * $rand) cast as
xs:integer]
return ($synset/@id/string(), $synset/@name/string())
};

my:random-synset()

10 changes: 10 additions & 0 deletions 6668/Chapter07/7-28.xq
@@ -0,0 +1,10 @@
declare namespace my = "http://brians.org/synsets";
declare function my:guess($answerId as xs:decimal, $guessId as xs:decimal) {
if (collection("steps.dbxml")//*[@id = $guessId]//*[@id = $answerId])
then true()
else false()
};

my:guess(56056, 19), (: policeman, person :)
my:guess(56056, 53188) (: policeman, cook :)

4 changes: 4 additions & 0 deletions 6668/Chapter07/7-29.xq
@@ -0,0 +1,4 @@
for $step in collection("steps.dbxml")//*[@id = "24"]//*
where matches($step/@name, "an$")
return $step/@name/string()

14 changes: 14 additions & 0 deletions 6668/Chapter07/7-31.xml
@@ -0,0 +1,14 @@
<kml xmlns="http://earth.google.com/kml/2.0">
<Placemark id="property_1">
<name>Four Seasons Cairo at Nile Plaza</name>
<description><![CDATA[<p>1089 Corniche El Nile<br/>Cairo, Egypt </p>
<p><strong>Score: <span style="color:red;">86.09</span></strong></p>
<p>30-story hotel near Garden City on the east bank of the river. </p>
&#169; 2006 <em>Travel + Leisure</em></p>]]></description>
<Point>
<altitudeMode>relativeToGround</altitudeMode>
<coordinates>31.229338,30.03595,0</coordinates>
</Point>
</Placemark>
</kml>

13 changes: 13 additions & 0 deletions 6668/Chapter07/7-33.xq
@@ -0,0 +1,13 @@
declare namespace my = "http://brians.org/range";
declare function my:in-range ($myLon as xs:decimal, $myLat as xs:decimal, $range
as xs:decimal) {
for $place in collection("coord.dbxml")/place
where ($place/latitude > ($myLat - $range) and $place/latitude < ($myLat +
$range))
and ($place/longitude > ($myLon - $range) and $place/longitude < ($myLon +
$range))
return $place/name/text()
};

my:in-range (-111.651862515931, 40.00652821419428, 2)

12 changes: 12 additions & 0 deletions 6668/Chapter07/7-34.xq
@@ -0,0 +1,12 @@
declare namespace my = "http://brians.org/range";
declare function my:in-range ($myLon as xs:decimal, $myLat as xs:decimal, $range as xs:decimal) {
for $place in collection("coord.dbxml")/place
where ($place/latitude > ($myLat - $range) and $place/latitude < ($myLat + $range))
and ($place/longitude > ($myLon - $range) and $place/longitude < ($myLon + $range))
return $place/name/text()
};

let $placesCloseToHome := my:in-range (-111.651862515931, 40.00652821419428, 12)
let $placesCloseToJim := my:in-range (-93.49764084020113, 45.01312134030998, 12)
return $placesCloseToHome union $placesCloseToJim

5 changes: 5 additions & 0 deletions 6668/Chapter07/7-35.xq
@@ -0,0 +1,5 @@
for $person in collection("people.dbxml")/person
for $phone in $person/phone/*/string()
where $phone = "612-555-9901"
return $person

6 changes: 6 additions & 0 deletions 6668/Chapter07/7-36.xq
@@ -0,0 +1,6 @@
for $person in collection("people.dbxml")/person
for $office in $person/phone/office
for $home in $person/phone/home
where $office = "612-555-9901" or $home = "612-555-9901"
return $person

5 changes: 5 additions & 0 deletions 6668/Chapter07/7-37.xq
@@ -0,0 +1,5 @@
declare variable $phone xs:string external;
for $person in collection("people.dbxml")/person
where $person/phone eq $phone
return concat($person/name/first, "'s ", $person/phone[string() = $phone]/@loc, " phone is: ", $phone)

21 changes: 21 additions & 0 deletions 6668/Chapter07/7-5.xq
@@ -0,0 +1,21 @@
declare namespace people = "urn:something";
declare variable $name as xs:string external;
declare function people:age-ok($age) {
if (21 < $age and $age < 100)
then true()
else false()
};
(: Here is a comment. :)
<people>
{
for $person in collection("people.dbxml")/person
where people:age-ok($person/age/number()) and $person/name/*/string() = $name
order by $person/name/last
return
<person>
<name>{$person/name/*}</name>
<age>{$person/age/string()}</age>
</person>
}
</people>

3 changes: 3 additions & 0 deletions 6668/Chapter07/7-6.xq
@@ -0,0 +1,3 @@
collection("synsets.dbxml")/Synset[Pointers/Hypernym =
collection("synsets.dbxml")/Synset[Word="baseball"]/Id/string()]/Id

5 changes: 5 additions & 0 deletions 6668/Chapter07/7-7.xq
@@ -0,0 +1,5 @@
for $baseball in collection("synsets.dbxml")/Synset[Word="baseball"]/Id,
$synset in collection("synsets.dbxml")/Synset
where $synset/Pointers/Hypernym = $baseball/Id
return $synset/Id

5 changes: 5 additions & 0 deletions 6668/Chapter07/7-8.xq
@@ -0,0 +1,5 @@
let $baseballs := collection("synsets.dbxml")/Synset[Word="baseball"]/Id
for $synset in collection("synsets.dbxml")/Synset
where $synset/Pointers/Hypernym = $baseballs
return $synset/Id

5 changes: 5 additions & 0 deletions 6668/Chapter07/7-9.xq
@@ -0,0 +1,5 @@
for $person in collection("people.dbxml")//person[name/first = "Jim"]
for $known in collection("people.dbxml")//person[name/last = "Brown"]
where $person is $known
return $person

13 changes: 13 additions & 0 deletions 6668/Chapter07/README.txt
@@ -0,0 +1,13 @@
Most of these XQuery files cannot be run outside of BDB XML's shell or an
application using the API, since the database collection is unavailable.

One exception is listing 7-10. To run it using the XQuery eval tool
(or xqilla):

% dbxml-2.2.13/xquery-1.2.0/build_unix/eval 7-10.xq

The module example:

% dbxml-2.2.13/xquery-1.2.0/build_unix/eval 7-12.xq


5 changes: 5 additions & 0 deletions 6668/Chapter07/temperature.xqm
@@ -0,0 +1,5 @@
module namespace temp = "http://brians.org/temperature";
declare function temp:celsius-to-fahrenheit ($celsius as xs:decimal) {
($celsius + 32) * (9 div 5)
};

18 changes: 18 additions & 0 deletions 6668/Chapter08/8-1.cpp
@@ -0,0 +1,18 @@
#include "DbXml.hpp"

using namespace DbXml;
int main(void)
{
// Create an XmlManager
XmlManager myManager;
try {
// Open a container
XmlContainer myContainer =
myManager.openContainer("container.dbxml");
} catch (XmlException &xe) {
printf ("%s\n", xe.what());
} catch (std::exception &e) {
// Other error handling goes here
}
}

0 comments on commit 17e8e7a

Please sign in to comment.