XQuery, CSS, and XML Syntax Trees

Post Reply
Jonathan Robie
Posts: 3621
Joined: May 5th, 2011, 5:34 pm
Location: Durham, NC
Contact:

XQuery, CSS, and XML Syntax Trees

Post by Jonathan Robie » November 11th, 2014, 6:06 pm

In my day job, I'm the lead editor of XQuery, a query language for XML.

In two other threads, I discussed displaying morphology and syntax trees with Cascading Stylesheets. In the SBL 2014 talk that Micheal Palmer and I are doing, we will also show how to query syntax trees using XQuery, displaying the results with Cascading Stylesheets.

To query, we need data, so let's take a look at the way we represent syntax trees in XML again. Here is how we represent the sentence βλέπετε οὖν πῶς ἀκούετε in our syntax trees, which are derived from the Global Bible Initiative's syntax trees, but simplify the markup in order to make them easier to use for generic queries and to display with stylesheets:
blepo00.png
blepo00.png (84.57 KiB) Viewed 1827 times
XQuery is optimized for querying XML like this. The following query returns all sentences in the input:

Code: Select all

//sentence
The following query returns all wg elements in the input:

Code: Select all

//wg
If a wg element has an attribute where the class is cl, it is a clause. The following query returns all clauses in the input:

Code: Select all

//wg[@class='cl']
The following query returns words where the class is verb and the mood is indicative:

Code: Select all

//w[ @class = "verb" and @mood="indicative"]
Let's combine these to find clauses where the main verb is indicative:

Code: Select all

for $wg in //wg
where $wg/@class="cl"
  and $wg/w[ @class = "verb" and @mood="indicative"]
return $wg
Incidentally, since the above queries return w and wg elements, which are used in the stylesheets we use to display text, the same stylesheets can be used to display query results.
0 x


ἐξίσταντο δὲ πάντες καὶ διηποροῦντο, ἄλλος πρὸς ἄλλον λέγοντες, τί θέλει τοῦτο εἶναι;
http://jonathanrobie.biblicalhumanities.org/

Jonathan Robie
Posts: 3621
Joined: May 5th, 2011, 5:34 pm
Location: Durham, NC
Contact:

Re: XQuery, CSS, and XML Syntax Trees

Post by Jonathan Robie » November 11th, 2014, 6:24 pm

In our syntax trees, the clause contains a verb and its subjects and objects as direct children of the clause itself.

Here is the query I used to find the sentence used in the first example in the above post. It returns all objects of the verb βλέπω:

Code: Select all

for $wg in //wg
where $wg/@class="cl"  
 and $wg/w[ @lemma = "βλέπω" ]
return $wg/*[@role='o']
Of course, we might be interested in the objects of all verbs, perhaps to create some kind of verb catalog. And we might want to group the objects for each verb by case. Here's a query that does that for all indicative verbs:

Code: Select all

for $wg in //wg
where $wg/@class="cl"  and $wg/*[@role='o']
let $verb := $wg/w[ @class = "verb" and @mood="indicative"]/@lemma
let $objects := $wg/*[@role='o']
group by $verb
order by $verb
return
   <verb name="{$verb}">
     <objects>
      {
         for $o in $objects
         let $sentence := $o/ancestor::sentence/@ID
         let $case := ($o//@case)[1]
         group by $case
         return
            <case name="{$case}">
             {
                $o
             }
            </case>
      }
     </objects>
   </verb>
The w and wg elements can be displayed using the standard stylesheet, but we've added some new elements to our output. Let's write a CSS stylesheet to say how these new elements should be displayed:

Code: Select all

verb:before {
  content: attr(name);
  font-size: x-large;
  color: blue;
}
  
verb {
  padding-top:10px;
  padding-bottom:10px;
  padding-left: 15px;
  display: block;
}   
    
case:before {
  padding-top:10px;
  padding-bottom:10px;
  content: attr(name);
  font-size: large;
  color: blue;
  display: block;
}

Here's an example of part of the output for one verb:
blepo3.png
blepo3.png (25.18 KiB) Viewed 1821 times
0 x
ἐξίσταντο δὲ πάντες καὶ διηποροῦντο, ἄλλος πρὸς ἄλλον λέγοντες, τί θέλει τοῦτο εἶναι;
http://jonathanrobie.biblicalhumanities.org/

Jonathan Robie
Posts: 3621
Joined: May 5th, 2011, 5:34 pm
Location: Durham, NC
Contact:

Re: XQuery, CSS, and XML Syntax Trees

Post by Jonathan Robie » November 19th, 2014, 3:35 pm

I've been playing with some of the feedback I have received, and modified my query to return this verb catalog. I intend to improve it as I learn more, but I learn by playing.

Here's an excerpt from the output, you can see the entire catalog at the above link.
verb-catalog.png
verb-catalog.png (56.74 KiB) Viewed 1700 times
Here's the query:

Code: Select all

declare default collation "http://saxon.sf.net/collation?lang=el;ignore-case=yes;ignore-modifiers=yes;ignore-width=yes";
    
declare function local:cite($nodeId as xs:string)
{
      concat("Luke ", substring($nodeId, 4, 2), ":", substring($nodeId, 7, 2))
};
    
for $wg in //wg
where $wg/@class="cl"  and $wg/*[@role='o' or @role='io'] and $wg/*[@class='verb' and @mood='indicative']
let $verb := $wg/w[ @class = "verb" and @mood="indicative"]/@lemma
let $objects := $wg/*[@role='o' or @role='io'] 
group by $verb
order by $verb
return
<group name="{$verb}">
   {
      $wg ! <group name="{local:cite(@nodeId)}">{ . }</group>
   }
</group>
0 x
ἐξίσταντο δὲ πάντες καὶ διηποροῦντο, ἄλλος πρὸς ἄλλον λέγοντες, τί θέλει τοῦτο εἶναι;
http://jonathanrobie.biblicalhumanities.org/

TimNelson
Posts: 61
Joined: October 17th, 2014, 11:04 pm
Location: Australia, Victoria, Geelong

Re: XQuery, CSS, and XML Syntax Trees

Post by TimNelson » December 3rd, 2014, 1:01 am

On a related topic, do you have a recommendation as to what XML-related software to use for querying and writing XML? I'm interested in the "writing" aspect because this would allow a query to be made which would determine lexical information, and then that lexical information could be stored with a lexical entry (which would aid in eg. the preparation of lexica; in theory this could be done for grammar too). My only other requirement is that it must run on Linux (bonus points for Open Source or Perl integration). I've been looking briefly at BaseX, which seems to be a combination of a database server and a GUI. I've also seen https://wiki.duraspace.org/display/FEDO ... ject+Model, although I'm far from understanding it.

I won't be able to do anything with the info for a couple of weeks (lets just say hardware problems), but would appreciate any relevant information anyone has.
0 x
--
Tim Nelson
B. Sc. (Computer Science), M. Div. Looking for work (in computing or language-related jobs).

Jonathan Robie
Posts: 3621
Joined: May 5th, 2011, 5:34 pm
Location: Durham, NC
Contact:

Re: XQuery, CSS, and XML Syntax Trees

Post by Jonathan Robie » December 3rd, 2014, 10:32 am

TimNelson wrote:On a related topic, do you have a recommendation as to what XML-related software to use for querying and writing XML? I'm interested in the "writing" aspect because this would allow a query to be made which would determine lexical information, and then that lexical information could be stored with a lexical entry (which would aid in eg. the preparation of lexica; in theory this could be done for grammar too). My only other requirement is that it must run on Linux (bonus points for Open Source or Perl integration).
The Oxygen editor is very useful, but it is not free. You can request a free copy for open source projects, though. I use Emacs and NXML mode for writing XML, and a variety of XQuery processors - usually xdb, Zorba, Saxon, Base-X, or eXist.
TimNelson wrote:I've been looking briefly at BaseX, which seems to be a combination of a database server and a GUI. I've also seen https://wiki.duraspace.org/display/FEDO ... ject+Model, although I'm far from understanding it.

I won't be able to do anything with the info for a couple of weeks (lets just say hardware problems), but would appreciate any relevant information anyone has.
Base-X is good. For many things, plain files and oXygen is just as easy, our corpora is small, it depends how much you need database functionality. Zorba and Saxon are both good XQuery processors for plain files.

If you're interested in collaborating with others to produce freely available materials for biblical studies, ping me, I can try to connect you.
0 x
ἐξίσταντο δὲ πάντες καὶ διηποροῦντο, ἄλλος πρὸς ἄλλον λέγοντες, τί θέλει τοῦτο εἶναι;
http://jonathanrobie.biblicalhumanities.org/

Post Reply

Return to “Syntax and Grammar”