Peter Roche
Peter Roche

Reputation: 345

Multiple attributes of the same xml file

The XQuery script needs to display all Company Name and Contact Name that is in the xml file.

This is what I have:-

for $x in doc("Customers.xml")/dataroot/Customers return $x/CompanyName $x/ContactName

Example xml

<dataroot>
    <Customers>
        <CustomerID>ALFKI</CustomerID>
        <CompanyName>Alfreds Futterkiste</CompanyName>
        <ContactName>Maria Anders</ContactName>
        <ContactTitle>Sales Representative</ContactTitle>
        <Address>Obere Str. 57</Address>
        <City>Berlin</City>
        <PostalCode>12209</PostalCode>
        <Country>Germany</Country>
        <Phone>030-0074321</Phone>
        <Fax>030-0076545</Fax>
    </Customers>
    <Customers>
        <CustomerID>ANATR</CustomerID>
        <CompanyName>Ana Trujillo Emparedados y helados</CompanyName>
        <ContactName>Ana Trujillo</ContactName>
        <ContactTitle>Owner</ContactTitle>
        <Address>Avda. de la Constitución 2222</Address>
        <City>México D.F.</City>
        <PostalCode>05021</PostalCode>
        <Country>Mexico</Country>
        <Phone>(5) 555-4729</Phone>
        <Fax>(5) 555-3745</Fax>
    </Customers>
    <Customers>
        <CustomerID>ANTON</CustomerID>
        <CompanyName>Antonio Moreno Taquería</CompanyName>
        <ContactName>Antonio Moreno</ContactName>
        <ContactTitle>Owner</ContactTitle>
        <Address>Mataderos  2312</Address>
        <City>México D.F.</City>
        <PostalCode>05023</PostalCode>
        <Country>Mexico</Country>
        <Phone>(5) 555-3932</Phone>
    </Customers>
    <Customers>
        <CustomerID>AROUT</CustomerID>
        <CompanyName>Around the Horn</CompanyName>
        <ContactName>Thomas Hardy</ContactName>
        <ContactTitle>Sales Representative</ContactTitle>
        <Address>120 Hanover Sq.</Address>
        <City>London</City>
        <PostalCode>WA1 1DP</PostalCode>
        <Country>UK</Country>
        <Phone>(171) 555-7788</Phone>
        <Fax>(171) 555-6750</Fax>
    </Customers>
    <Customers>
        <CustomerID>BERGS</CustomerID>
        <CompanyName>Berglunds snabbköp</CompanyName>
        <ContactName>Christina Berglund</ContactName>
        <ContactTitle>Order Administrator</ContactTitle>
        <Address>Berguvsvägen  8</Address>
        <City>Luleå</City>
        <PostalCode>S-958 22</PostalCode>
        <Country>Sweden</Country>
        <Phone>0921-12 34 65</Phone>
        <Fax>0921-12 34 67</Fax>
    </Customers>

I want to return CompanyName and ContactName ONLY. These must be able to easily be converted into a table format so it is structured in columns

Upvotes: 1

Views: 1596

Answers (1)

tohuwawohu
tohuwawohu

Reputation: 13618

XQuery's basic data model is the sequence, not the table or the relation. This applies to the result of a XQuery, too. So, by default you will receive a sequence returned by the return clause. There are no "columns" or "tables" to return by default. If you need a different or "deeper-structured" return structure other then sequence, you will have to add it yourself.

The easiest way to get a structured result is to create XML output. Because of that, most XQuery tutorials start with returning XML, not plain text or anything else.

If you need a "table structure" as output, you have different options:

  • output XML and import it into another application;
  • output (X)HTML with HTML tables;
  • output CSV and import/parse it with you preferred spreadsheet software.

To output XML, you could use the following:

xquery version "1.0";
<table>
{
    for $cust in fn:doc("Customers.xml")/dataroot/Customers
    return
        <row>
        {
            $cust/CompanyName, $cust/ContactName
        }
        </row>
}
</table>

It will return something like:

<table>
    <row>
        <CompanyName>Alfreds Futterkiste</CompanyName>
        <ContactName>Maria Anders</ContactName>
    </row>
    <row>
        <CompanyName>Ana Trujillo Emparedados y helados</CompanyName>
        <ContactName>Ana Trujillo</ContactName>
    </row>
    <row>
        <CompanyName>Antonio Moreno Taquería</CompanyName>
        <ContactName>Antonio Moreno</ContactName>
    </row>
    <row>
        <CompanyName>Around the Horn</CompanyName>
        <ContactName>Thomas Hardy</ContactName>
    </row>
    <row>
        <CompanyName>Berglunds snabbköp</CompanyName>
        <ContactName>Christina Berglund</ContactName>
    </row>
</table>

For CSV output (to be more precise: a sequence of CSV-like strings), you could use

xquery version "1.0";
for $cust in fn:doc("Customers.xml")/dataroot/Customers
return
concat('"', $cust/CompanyName, '"', ',', '"', $cust/ContactName, '"')

to get something like

"Alfreds Futterkiste","Maria Anders"
"Ana Trujillo Emparedados y helados","Ana Trujillo"
"Antonio Moreno Taquería","Antonio Moreno"
"Around the Horn","Thomas Hardy"
"Berglunds snabbköp","Christina Berglund"

EDIT: Some XQuery processors offer built-in CSV serialisation, too. For example, th Zorba XQuery processor includes a module to parse / serialize CSV.

Upvotes: 1

Related Questions