When starting to use XML-related technologies like XSL and XPath, the details and concepts can be intimidating in the first place. One question I had to solve was: How can I create a testing setup on my windows workstation that takes a XML source file and a ‘console’ from which I could execute XPath queries against that XML and see the result. This is comparable to having a mySQL table or database (your XML content) and shooting SQL queries to it via phpMyAdmin.
Here is the setup to get started:
(1) Preparations – do the following:
- Download and install: Cooktop
- Download and print: My XPath Cheatsheet (additions welcome!)
- View: XML Example
(2) Get your hands dirty:
- Start Cooktop and paste the XML Example into the XML-tab.
- If you have ugly XML or a machine generated oneline XML -> Menu ‘Tools’ / ‘Format current XML Tidy’.
- Be aware of namespaces! They have to be registered and used throughout your queries. It always confused me when testing, because the expected result is just empty nothing if you get that wrong. Solution: To simplify your test setup, make sure to delete all namespace info in the source XML. The example does not use a namespace, so we can forget about that for the moment.
- Go to the XPath-tab in Cooktop, read the instructions comment and delete it.
- All your XPath queries must be preceded by ‘nodes: ‘ or ‘values: ‘! Otherwise pressing the enter-key will not execute your query and just return the cursor to a new line.
- Write to a new line: ‘nodes: //dealnews_issues/name_de’ and press enter.
- You see the resulting nodes from your query. Cool!
- That’s what it is all about with XPath: Isolate a particular XML chunk to further process it in your software or use it in a XSL file for transformation into something different or check for existence or count elements etc. etc.
- Change your query to ‘values: //dealnews_issues/name_de’ and notice the difference.
- Now you can get a more complex XML content and play with it. Some good introductory examples of queries can be taken from my XPath Cheatsheet. You will soon discover that XPath can be very powerful and also fun to use.