Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add examples/ directory #98

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions examples/1.xq
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
(: declare default element namespace "http://www.w3.org/2001/XMLSchema"; :)
xquery version "3.1";

declare function local:row($table as element()){
$table / tbody / tr
};
declare function local:td($tr as element()){
$tr / td[1] / text()
};
declare function local:td2($tr as element()){
$tr /(if ( count(*) > 1 ) then td[2] / text() else "")
};
declare function local:comment($c as xs:string){
parse-xml(concat("<!-- ", $c, " -->"))
};

parse-xml('<html><body>
<table id="t1"><tbody>
<tr><td>Hello</td></tr>
</tbody></table>
<table id="t2"><tbody>
<tr><td>123</td></tr>
<tr><td>456</td><td>other</td></tr>
<tr><td>foo</td><td>columns</td></tr>
<tr><td>bar</td><td>are</td></tr>
<tr><td>xyz</td><td>ignored</td><td>zomg</td></tr>
</tbody></table>
</body></html>')/<table>
<tbody>{for $row in local:row(id('t2')), $td in local:td($row), $comment in local:comment(local:td2($row)) return ('
',<tr class="{$row/*/concat('C_',text())} {$row/(for $i in 1 to count($row/*) return map:get(map{'1': 'hascol1', '2': 'hascomment', '3': 'zomg'}, xs:string($i)))}" id="{$td}">
{<td>{if (count($row/*) > 2) then attribute name{$row/td[3]/text()} else ()}{$td}</td>}{$comment}
</tr>
)}
</tbody>
</table>
31 changes: 31 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
xidel examples
==============

Run as e.g.:
```bash
xidel --html --xquery3="$(cat examples/1.xq)"
```

Which in this case yields:
```html
<!DOCTYPE html>
<table>
<tbody>
<tr class="C_123 hascol1" id="123">
<td>123</td><!-- -->
</tr>
<tr class="C_456 C_other hascol1 hascomment" id="456">
<td>456</td><!-- other -->
</tr>
<tr class="C_foo C_columns hascol1 hascomment" id="foo">
<td>foo</td><!-- columns -->
</tr>
<tr class="C_bar C_are hascol1 hascomment" id="bar">
<td>bar</td><!-- are -->
</tr>
<tr class="C_xyz C_ignored C_zomg hascol1 hascomment zomg" id="xyz">
<td name="zomg">xyz</td><!-- ignored -->
</tr>
</tbody>
</table>
```