Querying XML Documents Using XPath

In each view of an XML document, you can query the document to obtain a subset of the information in that document. You can query stylesheets and XML Schema documents, but you cannot query DTD schema documents because they are not XML. This section discusses the following topics:

Steps for Querying a Document

To query a document:
1. Open the document you want to query.
2. In the document window tool bar, click in the query field. This field contains either a prompt to create a new query, or it contains a query you ran previously. For information about how to write a query, see Chapter 9Writing XPath Expressions.

Alternative: Press F6.

3. Press Enter to run the query or click Refresh Query , which is to the left of the query field at the top of the document window.

If a document is part of a Stylus Studio project, Stylus Studio saves the queries you specify. If you save, close, and reopen a document, the queries you already specified are still defined. In the document window, click the down arrow to the right of the query field to display the defined queries.

Stylus Studio assigns a number, beginning with 1, to each query you define. This allows you to run multiple queries against the same document and distinguish one from another.

After you make some modifications to your document, you might want to run a query that you already ran. Display the query in the field in the document tool bar and click Refresh Query.

Displaying Query Results

How Stylus Studio displays the result of a query depends on the type of result.

  • If the XPath processor returns a Boolean, numeric, or string value, Stylus Studio displays the value in the Query Output window.
  • If the XPath processor returns a collection of nodes, Stylus Studio displays the nodes in a DOM tree structure in the Query Output window to the right of the main XML editor pane. The number of Hits is the number of top-level nodes the query returned. You can expand and collapse the elements as needed. If you click a node in the result, then in the XML document window Stylus Studio moves the focus to the source node for the result node you clicked.

A tab at the bottom of the Query Output window displays the number that Stylus Studio assigned to your query. After you run multiple queries on the same document, there is a tab for each query that you run. This allows you to view the results of the different queries as needed.

Saving Query Results

After you execute a query, Stylus Studio displays the results in the Query Output window.

To save query results:
1. In the Query Output window, click Save Result .
2. In the Save As dialog box that appears, navigate to the directory that you want to contain the query result.
3. In the File name: field, type the name for the file that will contain the query result.
4. Click Save.

To add a query result file to a project, see Adding Files to Projects.

 
Free Stylus Studio XML Training: