This extension is now installed on this wiki, so you can use it here if you like. Although this extension is usable in 9.02, 9.08 or higher is recommended due to improved behavior of xml.text().
Discuss this extension on the forums.
| Author | Neil Weinstock |
| Type | Script |
| Categories | Tables |
| Requires | MindTouch Core 1.8.3 or later |
| Status | stable |
| License | Free/Open Source |
| Manifest | http://developer.mindtouch.com/@api/deki/files/4979/=tableparse.xml |
Install Script
Table of Contents
See also How to add a script, Using the Extension Dialog, Learn about DekiScript, Extensions Directory.
| Version | Date | Author | Description |
| 0.9.0 | 9-Dec-2009 | neilw | First public beta version |
| 1.0.0 | 6-Jan-2010 | neilw | Official Release
|
| 1.1.0 | 29-Jan-2010 | neilw | Added "RecordList" function |
We'll use the following table for our examples (this table has id="ex1"):
| Name | TableParse |
| Description | A little slice of fun |
This applies to all three functions.
Usually, I expect you'll pass the function the page XML containing the table, omit the XPath, and let TableParse find the first table.
If the table you want to parse might not be the first table in the page XML, then you'll want to provide an XPath argument. The XPath must specify the exact table you want to parse.
If you want to simply pass the table XML to the function, then you must provide an XPath of ".". This is because, in the absence of an XPath argument, the functions look for a table inside the given XML. If the XML is the table, then the functions will fail to see it. If you think this sounds pretty stupid, you're right, and I hope to resolve this as soon as possible. If you'd like to contribute your XPath expertise to help me fix this, stop by the forums.
So, to summarize, you can pass the functions any of the following:
| Code | Output | Notes |
{{ | [ [ "Version", "Date", "Author", "Description" ], [ "0.9.0", "9-Dec-2009", "neilw", "First public beta version" ], [ "1.0.0", "6-Jan-2010", "neilw", "Official Release Added \"xpath\" arg to each function Implemented \"format\" function" ], [ "1.1.0", "29-Jan-2010", "neilw", "Added \"RecordList\" function" ] ] |
|
{{ | [ [ <html><body><strong>("Version"; <br>nil</br>; " ")</strong></body></html>, <html><body><strong>("Date"; <br>nil</br>; " ")</strong></body></html>, <html><body><strong>("Author"; <br>nil</br>; " ")</strong></body></html>, <html><body><strong>("Description"; <br>nil</br>; " ")</strong></body></html> ], [ <html><body>"0.9.0"</body></html>, <html><body>"9-Dec-2009"</body></html>, <html><body>"neilw"</body></html>, <html><body>"First public beta version"</body></html> ], [ <html><body>"1.0.0"</body></html>, <html><body>"6-Jan-2010"</body></html>, <html><body>"neilw"</body></html>, <html><body>(" "; <p>"Official Release"</p>; " "; <ul>(" "; <li>"Added \"xpath\" arg to each function"</li>; " "; <li>"Implemented \"format\" function"</li>; " ")</ul>; " ")</body></html> ], [ <html><body>"1.1.0"</body></html>, <html><body>"29-Jan-2010"</body></html>, <html><body>"neilw"</body></html>, <html><body>"Added \"RecordList\" function"</body></html> ] ] |
|
{{ | [ [ { text : "Version", xml : <html><body><strong>("Version"; <br>nil</br>; " ")</strong></body></html> }, { text : "Date", xml : <html><body><strong>("Date"; <br>nil</br>; " ")</strong></body></html> }, { text : "Author", xml : <html><body><strong>("Author"; <br>nil</br>; " ")</strong></body></html> }, { text : "Description", xml : <html><body><strong>("Description"; <br>nil</br>; " ")</strong></body></html> } ], [ { text : "0.9.0", xml : <html><body>"0.9.0"</body></html> }, { text : "9-Dec-2009", xml : <html><body>"9-Dec-2009"</body></html> }, { text : "neilw", xml : <html><body>"neilw"</body></html> }, { text : "First public beta version", xml : <html><body>"First public beta version"</body></html> } ], [ { text : "1.0.0", xml : <html><body>"1.0.0"</body></html> }, { text : "6-Jan-2010", xml : <html><body>"6-Jan-2010"</body></html> }, { text : "neilw", xml : <html><body>"neilw"</body></html> }, { text : "Official Release Added \"xpath\" arg to each function Implemented \"format\" function", xml : <html><body>(" "; <p>"Official Release"</p>; " "; <ul>(" "; <li>"Added \"xpath\" arg to each function"</li>; " "; <li>"Implemented \"format\" function"</li>; " ")</ul>; " ")</body></html> } ], [ { text : "1.1.0", xml : <html><body>"1.1.0"</body></html> }, { text : "29-Jan-2010", xml : <html><body>"29-Jan-2010"</body></html> }, { text : "neilw", xml : <html><body>"neilw"</body></html> }, { text : "Added \"RecordList\" function", xml : <html><body>"Added \"RecordList\" function"</body></html> } ] ] |
|
| Code | Output | Notes |
{{ | { "0.9.0" : "9-Dec-2009", "1.0.0" : "6-Jan-2010", "1.1.0" : "29-Jan-2010", Version : "Date" } |
|
{{ | { "0.9.0" : <html><body>"9-Dec-2009"</body></html>, "1.0.0" : <html><body>"6-Jan-2010"</body></html>, "1.1.0" : <html><body>"29-Jan-2010"</body></html>, Version : <html><body><strong>("Date"; <br>nil</br>; " ")</strong></body></html> } |
|
{{ | { "0.9.0" : { text : "9-Dec-2009", xml : <html><body>"9-Dec-2009"</body></html> }, "1.0.0" : { text : "6-Jan-2010", xml : <html><body>"6-Jan-2010"</body></html> }, "1.1.0" : { text : "29-Jan-2010", xml : <html><body>"29-Jan-2010"</body></html> }, Version : { text : "Date", xml : <html><body><strong>("Date"; <br>nil</br>; " ")</strong></body></html> } } |
|
This function is good when you have a table from which you need to extract a known subset of of the data. By defining a format for the table (which is passed to the function, as described below), you put the table format in one place, rather than hard-coding it into a bunch of different places in your code. So, with one fell swoop, you take all the required data from the table and throw it into a map.
The "format" argument is a map specifying how to parse each desired cell of the table. Each map element is a list with two or three items:
If no cell exists at the specified location, then it is assignned the value nil.
So let's see what we can do with our sample table:
| Code | Output | Notes |
{{ | { description : <html><body>"9-Dec-2009"</body></html>, name : "Date" } |
|
The recordList() function treats the first row as the list of field names, and each row below it as a record of values of those fields. The results are put into a list of maps. To illustrate this function we'll use this table:
| Name | |
| Bugs Bunny | Being a stinker |
| Elmer J. Fudd | Hunting wabbits |
In this particular case, the first row of the table is a normal row (inside <tbody> with <td>s), but it could equally well be a <thead> with <th> elements. No difference! Also, just to show what happens, we've omitted a fieldname from the second column (it should say "Occupation"). The id attribute for this table is "exrl". Here's how it works:
| Code | Output | Notes |
{{ | [ { COLUMN1 : "Being a stinker", Name : "Bugs Bunny" }, { COLUMN1 : "Hunting wabbits", Name : "Elmer J. Fudd" } ] |
|
| Images 0 | ||
|---|---|---|
| No images to display in the gallery. |
Copyright © 2011 MindTouch, Inc. Powered by
Fortunately the path for this extension was the same as another extension on our server.
Would suggest the red 'EDIT' button is renamed 'SAVE' with a warning prompt for overwriting data - and the empty manifest field issue is looked at! edited 01:47, 17 Nov 2010