corner imagecorner image
FeaturesPluginsDocs & SupportCommunityPartners

XML Schema Tools: Analyzing XML Schemas

Back to Getting Started With XML Schema Tools

The goal of this tutorial is to look at the different ways you can analyze XML schemas using queries available in XML schema tools.

Note: Wherever you see the icon, you can click to view a detailed interactive demonstration of the steps described in the tutorial.

top


Before You Start

Before you proceed to the analysis part of this tutorial, you must perform some preparation work. Make sure you complete the steps in this section before going to the next section, Finding Usages of a Schema Component.

The preparation tasks are:

  1. Add two complex types, HybridType and SportsCarType, to extend and restrict a complex type in the sample schema.
  2. Add an element substitution.
  3. Add a schema file.

To add the complex type components HybridType and SportsCarType:

  1. If mySchemaPrj is not open in the IDE, open it now and then open the newLoanApplication schema file.
  2. Maximize the view by right-clicking the tab for the file and choosing Maximize Window from the pop-up menu.
    Maximizing the view lets you see more information about the schema in the column view of the Schema view.
  3. In the Schema view, click the column button Column button in Schema view to make sure you are in the column mode of the view.
  4. In the Schema view of the newLoanApplication schema file, right-click the Complex Types node and choose Add Complex Type.
    The Complex Type dialog box opens.
  5. Do the following:
    1. In the Name field, type HybridType.
    2. Under Type Definition, select the Use Existing Definition radio button.
      The lower part of the dialog box changes to provide you with options to define the reference.
    3. Under Global Reference, select the Restriction radio button, and in the list of types, expand the Complex Types node and select the CarType node.
      Notice that the IDE shows you a preview of the source code that will be added for this definition.
    4. Click OK.
      The IDE adds a node for the new complex type. The label of the node is the name you entered in the Name field, HybridType; the node label also provides additional information: (restricts CarType).

     View Demo

  6. In the Schema view of the newLoanApplication schema file, right-click the Complex Types node and choose Add Complex Type.
    The Complex Type dialog box opens.
  7. Do the following:
    1. In the Name field, type SportsCarType.
    2. Under Type Definition, select the Use Existing Definition radio button.
      The lower part of the dialog box changes to provide you with options to define the reference.
    3. Under Global Reference, select the Extension radio button, and in the list of types, expand the Complex Types node and select the CarType node.
    4. Click OK.
      The IDE adds a node for the new complex type. The label of the node is the name you entered in the Name field, SportsCarType; the node label also provides additional information: (extends CarType).
    5. From the IDE main menu, choose File > Save All.

     View Demo

    The Schema view now contains nodes for the complex types you added: HybridType and SportsCarType.
    Note: If you have derivations in your schema, you can select the derivation, in this example, HybridType or SportsCarType, right-click and choose Go To > Definition to navigate directly to the source definition.

To add an element substitution:

  1. In the Schema view of the newLoanApplication schema file, click the Source button.
  2. In the Source view, add the following lines immediately before the last line of the file, the line that ends the schema definition:
    <xs:element name="carLoanApplication" substitutionGroup="autoLoanApplication">
    </xs:element>
    
  3. From the IDE main menu, choose File > Save All.

 View Demo

To add a schema file:

  1. If mySchemaPrj is not open in the IDE, open it now.
  2. In the Projects window, expand the mySchemaPrj project node and expand the Source Packages node.
  3. Right-click the <default package> node and choose New > File/Folder.
    The New File wizard opens.
  4. In the Choose File Type page, in the Categories list, select the XML node, then in the File Types list, select XML Schema and click Next.
  5. In the Name and Location page of the wizard, in the File Name field, type newLoanHistory, accept the other default values (Folder: src, Target Namespace: http://xml.netbeans.org/schema/newLoanHistory) and click Finish.
    In the Projects window, the <default package> node under the Source Packages node now contains a subnode labeled newLoanHistory.xsd. The newly created subnode is selected and the Source Editor contains a tab for the XML schema file, newLoanHistory.xsd.
  6. In the Schema view, click the Source button to go to the Source view of the schema file.
  7. In the Source view, replace the existing content with the sample code below.
    <?xml version="1.0" encoding="UTF-8"?>
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                targetNamespace="http://xml.netbeans.org/examples/targetNS"
                xmlns:tns="http://xml.netbeans.org/examples/targetNS"
                xmlns:la="http://xml.netbeans.org/examples/LoanApplication"
                elementFormDefault="qualified">
        <xsd:import namespace="http://xml.netbeans.org/examples/LoanApplication" schemaLocation="newLoanApplication.xsd"/>
        <xsd:complexType name="PriorLoan">
            <xsd:sequence>
                <xsd:element name="Type" type="la:LoanType"></xsd:element>
                <xsd:element name="State" type="la:StateType"></xsd:element>
            </xsd:sequence>
        </xsd:complexType>
        <xsd:element name="History" type="tns:PriorLoan"></xsd:element>
    </xsd:schema>
    
  8. From the IDE main menu, choose File > Save All.

 View Demo

top


Finding Usages of a Schema Component

In this procedure, you will use the Find Usages command to display usages of a component in the two XML schema files in your project.

Note: The Find Usages command shows usages of a component in all XML schema, WSDL, and BPEL files in the current IDE project.

To find usages of a schema component:

  1. If mySchemaPrj is not open in the IDE, open it now and then open the newLoanApplication schema file.
  2. Maximize the view by right-clicking the tab for the file and choosing Maximize Window from the pop-up menu.
    Maximizing the view lets you see more information about the schema in the column view of the Schema view.
  3. In the Schema view, click the column button Column button in Schema view to make sure you are in the column mode of the view.
  4. In the first column of the Schema view, select the Complex Types node.
  5. In the second column, select the AddressType node, right-click and choose Find Usages from the pop-up menu.
    The IDE opens the XML Usages window in the lower part of the IDE. The XML Usages window has two panes, each with its own toolbar.
    • The left pane, the usages pane, contains a tree that shows the usages of AddressType.
      The toolbar buttons in this pane let you refresh information in the pane, expand and collapse nodes in the tree, and cycle through the found usages. Note that when you click the Refresh button in this pane, the IDE refreshes the data in both panes.
    • The right pane, the graph pane, shows a graphical representation of the usages. If you resize the window or drag nodes outside of the pane, you can use the Layout toolbar button in this pane to refresh the graph in the pane.

    XML Usages window, click to enlarge

    Productivity Tips
    • In the graph pane, you can right-click a schema component, except the node that represents the queried component, and choose an option from the Go To menu to navigate directly to that component in the Source view, Schema view, or Design view. If the schema component is an extension or a restriction of another schema component, you can navigate directly to the extended or restricted schema component in the active view of the XML schema editor by choosing Definition from the Go To menu.
    • Use the information below to zoom, pan, and drag the graph pane of the XML Refactoring window.
      • To zoom out: right-click in the graphic and drag up.
      • To zoom in: right-click in the graphic and drag down.
      • To pan: click and drag in any direction.
      • To drag a node: click and drag the node.
    • If you drag a node out of the display area, you can bring it back using one of the following methods:
      • Pan in the direction of the node until it is visible in the display area again.
      • Zoom out until the node is visible in the display area.
      • Click the Layout button in the toolbar of the graph pane of the XML Usages window.
  6. To find usages of LoanType, do the following:
    1. In the first column of the Schema view of the newLoanApplication schema file, select the Simple Types node.
    2. In the second column of the view, select the LoanType node, right-click and choose Find Usages from the pop-up menu.
      If the XML Usages window was still open, the IDE opens a Find XML Usages tab in the XML Usages window in the lower part of the IDE. The results show two file clouds with the file node and the usage nodes within the file clouds. You can double-click the file clouds to expand or collapse them and you can move a cloud with its content by dragging the cloud in the graph pane.
      An illustration of the graph pane with the expanded newLoanHistory.xsd and collapsed newLoanApplication.xsd file clouds is shown below.

      XML usages window, graph pane with 2 file nodes

  7. Close the XML Usages window by clicking the Close button in the window's title bar.

 View Demo

top


Finding Unused Global Components

In this procedure, you will use a query to find global components in the sample Loan Application Schema that are not used in any of the schema files in your project, mySchemaPrj.

This query allows you to identify top-level global elements when restructuring the schema towards a single root element (Venetian Blind design pattern).

To find unused global components:

  1. If mySchemaPrj is not open in the IDE, open it now.
  2. In the Projects window, select the newLoanApplication.xsd file node, right-click and choose Query > Find Unused Components.
    A dialog box opens that lets you exclude global elements.
  3. In the dialog box, select the Exclude Global Elements checkbox and click OK.
    The XML Schema Query window opens with a result tree. The query found five unused components: Borrow, HybridType, InternationalAddressType, and SportsCarType appear under the Complex Types node, and Lend appears under the Simple Types node, as shown in the following illustration.
    The status bar also provides information about the query results.

    Find unused global components, exclude global elements, five found

  4. In the Projects window, select the newLoanApplication.xsd file node, right-click and choose Query > Find Unused Components.
    A dialog box opens that lets you exclude global elements.
  5. In the dialog box, make sure the Exclude Global Elements checkbox is not selected and click OK.
    The IDE opens a Find Unused tab in the XML Schema Query window with the results of the query. Notice that the results now include an Elements node, with one child node, carLoanApplication.

    Find unused global components, include global elements, six found

  6. In the Find Unused tab of the XML Schema Query window, select the carLoanApplication node, right-click and choose Go To Source.
    The IDE opens the Source view and positions the cursor at the start of the block for the carLoanApplication element.
  7. Close the XML Schema Query window by clicking the Close button in the window's title bar.

 View Demo

top


Finding Derivations of a Complex Type

In this procedure, you will use a query to find the derivations you created during the preparation phase of this tutorial.

To find derivations of a global complex type:

  1. In the Projects window, select the newLoanApplication.xsd file node, right-click and choose Query > Find Derivations of Complex Type.
    The Find Derivations dialog box opens.
  2. In the list of complex types, select CarType and click OK.
    The XML Schema Query opens with the result of your query. The derivations you just created for CarType are shown in the window.
    The status bar provides additional information about the results: Found 1 extension and 1 restriction on CarType.

    Derivations in XML Schema Query window

  3. Close the XML Schema Query window by clicking the Close button in the window's title bar.

 View Demo

top


Finding Substitution Groups

In this procedure, you will use a query to find the element substitution you created during the preparation phase of this tutorial.

To find substitution groups:

  1. In the Projects window, select the newLoanApplication.xsd file node, right-click and choose Query > Find Substitution Groups.
    The IDE shows the substitution you just created in the XML Schema Query window and also provides result information in the status bar (One Substitution Group Found). In this example, autoLoanApplication is the head element, and carLoanApplication can be substituted for autoLoanApplication.

    Substitution group in XML Schema Query window

  2. Close the XML Schema Query window by clicking the Close button in the window's title bar.

 View Demo

top


Next Step

The steps in Retrieving and Referencing External XML Resources show you how to retrieve external resources into your IDE project and then reference those resources in your XML schema.



top


Bookmark this page

del.icio.us furl simpy slashdot technorati digg