Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7590661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:27:38+00:00 2026-05-30T20:27:38+00:00

The XQuery script needs to display all Company Name and Contact Name that is

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T20:27:39+00:00Added an answer on May 30, 2026 at 8:27 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have an XQuery that looks something like this: for $i in /*:rootElement
I have this XQuery element that is giving me an error: let $mx :=
I originally had written a simple XQuery script: $mediaNodes := doc('/db/portfolio/media_data_101109.xml'), $query := concat('$mediaNodes//media[contains(@product,',$product,')'
I am trying this in an XQuery (assume that doc('input:instance') does indeed return a
According to this: http://www.xmlplease.com/xquery-xhtml XQuery does not have a standard way of setting the
Have alot of the following in this project <script id=placementsListTemplate type=text/x-query/tmpl> Trouble is, Eclipse
I am using xquery over BaseX XML Database. Say, I have the following documents
I'm trying to learn xQuery coming from a php background, I have this expression
I am trying to write an XQuery which would find all text nodes that
Can I use XQuery to query all XML files under a specific directory? All

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.