[xquery-talk] novice questions mailing list

Misztur, Chris CMisztur at macleanfogg.com
Thu Dec 12 17:48:24 PST 2013


Here is a sample xquery script.  It declares 1 function and a try/catch block to execute the 'main' code.



xquery version '3.0';

declare %private function local:get-category(
    $document as document-node()?,
    $category as xs:string)
    as item()*
{
    (: select from all book nodes :)
    $document/bookstore/book
    (: where the category attribute value is equal to our variable :)
    [@category/string() eq $category]
};

try
{

let $books :=
    doc('/Users/Waldo/Documents/xquery-list/books.xml')

return
    local:get-category($books, 'WEB')
}
catch *
{
    element exception
    {
        attribute unhandled { true() },
        'an unhandled exception occured'
    }
}



________________________________________
From: talk-bounces at x-query.com [talk-bounces at x-query.com] on behalf of e-letter [inpost at gmail.com]
Sent: Thursday, December 12, 2013 5:38 PM
To: David Lee
Cc: talk at x-query.com
Subject: Re: [xquery-talk] novice questions mailing list

On 12/12/2013, David Lee <dlee at calldei.com> wrote:
> I think you will find this list very accommodating to basic questions.
> All of us on this list knew nothing about xpath at one point.
>
> I recommend with any question you supply sample XML and good description of
> what you are trying to accomplish along with any xpath or xquery code you
> have attempted
> and what it produced and why you belive it is the wrong result.
>

Here goes:

The objective is to learn how to select nodes in an xml file and use
xpath and xslt to select nodes of interest and display in a new file.

An xml file was created:

<?xml version="1.0" encoding="utf-8"?>
<!-- Edited by XMLSpy®, taken from http://www.w3schools.com-->
<?xml-stylesheet type='text/xsl" href="xstylesheetexample.xslt" ?>
<bookstore>
        <book category="COOKING">
                <title lang="en">Everyday Italian</title>
                <author>Giada De Laurentiis</author>
                <year>2005</year>
                <price>30.00</price>
        </book>
        <book category="CHILDREN">
                <title lang="en">Harry Potter</title>
                <author>J K. Rowling</author>
                <year>2005</year>
                <price>29.99</price>
        </book>
        <book category="WEB">
                <title lang="en">XQuery Kick Start</title>
                <author>James McGovern</author>
                <author>Per Bothner</author>
                <author>Kurt Cagle</author>
                <author>James Linn</author>
                <author>Vaidyanathan Nagarajan</author>
                <year>2003</year>
                <price>49.99</price>
        </book>
        <book category="WEB">
                <title lang="en">Learning XML</title>
                <author>Erik T. Ray</author>
                <year>2003</year>
                <price>39.95</price>
        </book>
</bookstore>

An xml stylesheet was created:

<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:import href="xqueryexampledata.xml"/>
        <xsl:output
                method="xml"
                doctype-public="-//W3C//DTD XHTML Basic 1.1//EN"
                doctype-system="http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd"
                encoding="utf-8"
                indent="no"
                omit-xml-declaration="yes"
                media-type="text/xml"
                standalone="yes"
                version="1.1"
                />
        <xsl:template
                match="author">
                <html xmlns='http://www.w3.org/1999/xhtml' lang='en' xml:lang='en'>
                        <head>
                                <meta
                                        name='note' content='xhtml document created by xml transformation' />
                                <title>Test output web page</title>
                        </head>
                        <body>
                                <p>
                                        <xsl:apply-templates/>
                                </p>
                        </body>
                </html>
        </xsl:template>
</xsl:stylesheet>

The text editor 'jedit' is used with xml plugins xslt, xquery, xml.
The xslt transformer plugin is used to select the above xml file and
xslt file for subsequent transformation, to create an output xhtml
file.

The result of this transformation is the creation of a new xhtml file
(as stated in the stylesheet above):



                Everyday Italian
                <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN"
"http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
lang="en"><head><meta content="xhtml document created by xml
transformation" name="note" /><title>Test output web
page</title></head><body><p>Giada De Laurentiis</p></body></html>
                2005
                30.00


                Harry Potter
                <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
lang="en"><head><meta content="xhtml document created by xml
transformation" name="note" /><title>Test output web
page</title></head><body><p>J K. Rowling</p></body></html>
                2005
                29.99


                XQuery Kick Start
                <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
lang="en"><head><meta content="xhtml document created by xml
transformation" name="note" /><title>Test output web
page</title></head><body><p>James McGovern</p></body></html>
                <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
lang="en"><head><meta content="xhtml document created by xml
transformation" name="note" /><title>Test output web
page</title></head><body><p>Per Bothner</p></body></html>
                <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
lang="en"><head><meta content="xhtml document created by xml
transformation" name="note" /><title>Test output web
page</title></head><body><p>Kurt Cagle</p></body></html>
                <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
lang="en"><head><meta content="xhtml document created by xml
transformation" name="note" /><title>Test output web
page</title></head><body><p>James Linn</p></body></html>
                <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
lang="en"><head><meta content="xhtml document created by xml
transformation" name="note" /><title>Test output web
page</title></head><body><p>Vaidyanathan Nagarajan</p></body></html>
                2003
                49.99


                Learning XML
                <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
lang="en"><head><meta content="xhtml document created by xml
transformation" name="note" /><title>Test output web
page</title></head><body><p>Erik T. Ray</p></body></html>
                2003
                39.95


Clearly, there's a lot to learn in specifying the transformation correctly!

The ideal output is:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN"
"http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd">
        <html xmlns='http://www.w3.org/1999/xhtml' lang='en' xml:lang='en'>
                <head>
                        <meta
                                content="xhtml document created by xml transformation" name="note"
                                />
                        <title>Test output web page</title>
                </head>
                <body>
                        <p>Giada De Laurentiis</p>
                        <p>J K. Rowling</p>
                        <p>James McGovern</p>
                        <p>Per Bothner</p>
                        <p>Kurt Cagle</p>
                        <p>James Linn</p>
                        <p>Vaidyanathan Nagarajan</p>
                        <p>Erik T. Ray</p>
                </body>
        </html>

_______________________________________________
talk at x-query.com
http://x-query.com/mailman/listinfo/talk

________________________________

The contents of this message may be privileged and confidential. Therefore, if this message has been received in error, please delete it without reading it. Your receipt of this message is not intended to waive any applicable privilege. Please do not disseminate this message without the permission of the author.

Please consider the environment before printing this e-mail




More information about the talk mailing list