Tag: XQT

QXT version 1.2

The possibility to save the currently loaded XML document is added.

A new option is added to the Options dialog that lets users choose if XML editing i allowed in the loaded document (“Allow XML editing”).

When the “Allow XML editing” option is checked, you can double click on a attribute to change its value.

Open the XQT download page.

Changes XQT in version 1.1

These are the changes made in XQT version 1.1, available here.

  • The text output bug is fixed. The Text Output Window now displays correct information.
  • An options dialog is added. From here you can control the program behavior.
  • A query history window is added, so that you easily can see what queries has run, and re-run them by double clicking on them. You can very quickly switch between different result sets by stepping through queries with the keyboard, and press enter to bring a result set up.

XQT at Nonags.com

XQT is listed at Nonags.com. Nonags.com checks this software for viruses and spyware, and provides a safe download link.

Jämförelseoperatörer och logiska operatörer

Vill du följa med i detta dokument, starta XQT och anslut dig till exempeldokumentet genom att växla till Direct mode och skriv

ConnectExample RecordCollection

För att välja element i XPath, skriver du sökvägen till elementen du vill ska ingå i resultatet. Genom att bara skriva Record, så får du alla skivor i XML-filen. Filter skrivs inom hakparenteser. Är du endast intresserad av skivor som släpptes ett visst år, använd jämförelseoperatören “lika med” =. Detta ger skivor släppta 1975.

Record[Year="1975"]

Frågan ger två skivor som svar. Vill du filtrera ytterligare, kan du använda logiska operatörer. Logiskt “och” heter and i XPath. Tänk på att XPath är skiftlägeskänsligt (And är inte samma sak som and). Denna fråga ger skivorna med Journey släppte 1975, skriv:

Record[Year="1975" and Artist="Journey"]

Vill du istället se skivorna släppta antingen år 1975 eller av artisten Roger Waters, använd logiskt “eller”. I XPath heter det or.

Record[Year="1975" or Artist="Roger Waters"]

De jämförelseoperatörer du kan använda är “lika med” (=), “inte lika med” (!=), “större än” (>), “mindre än” (<), “större än eller lika med” och “mindre än eller lika med” (>= resp. <=).

Denna fråga visar skivor som inte är släppta år 1975.

Record[Year!="1975"]

XQT finns att ladda hem från denna sida.

Testa XPath-frågor med XQT

Verktyget XQT (XPath Query Tool) kan användas för att analysera innehållet i en XML-fil, och att ställa frågor mot dokumentet och analysera svaret. Programmet innehåller ett inbyggt XML-dokument om du vill testa att formulera några enkla XPath-frågor. För att ladda det inbyggda XML-dokumentet, växla till direct mode genom att klicka på fliken märkt Direct eller genom att trycka Ctrl+D. Där, skriv:

ConnectExample RecordCollection

Tryck Enter och konstatera att resultatträdet nu visar texten Records (3 children). Expandera gärna noden i resultatträdet och undersök exempelfilen.

(Läget Edit mode låter dig skriva en fråga och trycka F5 för att exekvera den, medan Direct mode låter dig skriva en fråga och exekvera den genom att trycka Enter.)

XPath fungerar så att du anger sökvägen till de element du vill ha i ditt resultat. För att se vilka titlar som finns, skriv

Record/Title

…och tryck Enter.

När som helst, tryck F6 för att visa hela XML-dokumentet istället för svaret på den senaste frågan.

Två av de tre skivorna (Journey och A Night at the Opera) är släppta 1975. För att se dessa två, skriv:

Record[Year="1975"]/Title

Alla skivor har ett identitetsnummer som attribut till Record-element. För att se listan över låtar för den skiva vars attribut är 7575, skriv:

Record[@ID="7575"]/Tracks/Track

Som sista exempel, vill du bara titta på det första spåret, notera att spårens sekvensnummer ligger lagrade i ett attribut som heter Sequence, och skriv:

Record[@ID="7575"]/Tracks/Track[@Sequence="1"]

En bra guide till XPath finns här: http://www.w3schools.com/Xpath/

XQT kan du ladda hem från denna sida (direktlänk).

XQT is taking off after all

I still haven’t made the installation program for XQT, but it is getting close to a useful application now. This is how version 0.9 looks (click on it for full size):

In edit mode, you can type XPath queries in the editor and press F5 to execute them. The result shows up below. If text is selected when you hit F5, only the selected text will be executed. You can switch between edit mode and direct mode by clicking on the tabs or pressing Ctrl+E for edit and Ctrl+D for direct mode.

In direct mode, the query gets executed as you type it in and press Return. If you hold down Shift, a line break is created without the typed text being executed.

After querying, F6 takes you back to the root node of the XML document you are connected to. XQT.exe is located here. Since 17/1 2010, the program is available from the Programs page.

Query tool for XML

Today I had to be able to query XML files using XPath and view the result clean and structured. Doing this from within Visual Basic is very easy, so making a tool was an easy option. This is the user interface:

The top pane is used for writing XPath queries. Below, there are three panes. One displays the result as a tree structure, the next displays the attributes of the selected node in the tree view and the last display the source of the selected node.

This is the Click event of the query button:

Private Sub DoExecute(ByVal XPath As String)

    'Store the last executed query in case the user wants to refresh the query.
    Me.LastExecutedQuery = XPath

    'Clear the output textbox.
    txtOutput.Text = ""

    'Clear the elements tree view.
    tvElements.Nodes.Clear()

    'Clear the attribute list.
    lvAttributes.Items.Clear()

    If XPath.Trim() = "" Then

        'If no query is sent in, display the root element.

        'Display it in the tree (using a custom function not shown here).
        Me.AddNode(Dom.DocumentElement, tvElements.Nodes)

        'This is the status bar of the window.
        lblResult.Text = "Elements in result: 1"

    Else

        'The query might fail.
        Try

            Dim Result As Xml.XmlNodeList = Dom.DocumentElement.SelectNodes(XPath)
            If Result Is Nothing Then
                lblResult.Text = "Elements in result: 0"
            Else
                If Result.Count = 0 Then
                    lblResult.Text = "Elements in result: 0"
                Else
                    Me.ResultList = Result
                    lblResult.Text = "Elements in result: " & Result.Count.ToString()

                    Dim ResultNode As TreeNode = tvElements.Nodes.Add(XPath.Trim())
                    ResultNode.Tag = "Query"
                    ResultNode.ImageIndex = 3
                    ResultNode.SelectedImageIndex = 3

                    'Populate the tree structure (using a custom function not shown here).
                    tvElements.BeginUpdate()
                    For Each Xn As Xml.XmlNode In Result
                        Me.AddNode(Xn, ResultNode.Nodes)
                    Next
                    tvElements.EndUpdate()

                    'Expand the root node that contains the result.
                    ResultNode.Expand()

                End If
            End If

        Catch ex As Exception

            'If the query failed, extract the reason from the exception and display it.
            lblResult.Text = "Elements in result: [Error]"
            txtOutput.Text = "XPath failed." & ControlChars.CrLf & ControlChars.CrLf & ex.Message

        End Try
    End If

    'Select the first node. Other panes will be updated in the AfterSelect event of the treeview.
    If tvElements.Nodes.Count > 0 Then
        tvElements.SelectedNode = tvElements.Nodes(0)
        tvElements.SelectedNode.EnsureVisible()
    End If

End Sub

To use this, just open an XML file and click away in the tree view. You can also write XPath queries to browse a subset of the data. If you want to download this early version, the exe file (.NET Framework 3.5) is located here Since 17/1 2010, the program is can be downloaded from here.