#!/usr/bin/perl -w

# Query generator for 
# C8(i) = Y(i)/id(.)/name for i = 1,2,...
# Y(1) = /site/catgraph/edge[@from = "category0"]/@to
# Y(i) = /site/catgraph/edge[@from = Y(i-1)]/@to for i >= 2

# query components
my $X = "site/catgraph/edge[\@from = ";
my $Y = "'category0'";
my $W = "]/\@to";
my $Z = "/id(.)/name";

# help message
my $help = "Usage: perl C8.pl type k
type (either xpath or xslt) is the query type
k (>=1) is the query complexity\n";

if (($#ARGV + 1) ne 2) {print $help; exit;}

# type of query
my $type = $ARGV[0];
if (($type ne "xpath") and ($type ne "xslt")) {print $help; exit;}

# query complexity
my $k = $ARGV[1];
if ($k < 1) {print $help; exit;}

my $query = $X;
for ($i = 1; $i <= $k; $i++) {
  if ($i < $k) {$query = $query."/".$X;}
  else {$query = $query.$Y;}
}
for ($i = 1; $i <= $k; $i++) {$query = $query.$W;}
$query = $query.$Z;

if ($type eq "xpath") {
  $query = "doc()/".$query;
}

if ($type eq "xslt") {
  my $head = "<?xml version=\"1.0\"?>
  <xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">
    <xsl:template match=\"/\">
      <xsl:copy-of select=\"";
  my $foot = "\"/>
    </xsl:template>
  </xsl:stylesheet>";
  $query = $head.$query.$foot;
}

print $query;