Skip to content
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
21 changes: 15 additions & 6 deletions js/pjxml.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,19 +398,28 @@ var pjXML = (function () {
if (typeof xpath === 'function') {
const ar = [];

function safWorker(nd) {
if (xpath(nd)) {
function safWorker(nd, xpath, depth) {
const res = xpath(nd);

if (typeof res === 'function') {
xpath = res;
depth = 2;
} else if (res) {
ar.push(nd);
} else if (res === null) {
depth = 1;
}

const el = nd.elements();
if (--depth) {
const el = nd.elements();

for (let i = 0; i < el.length; i++) {
safWorker(el[i]);
for (let i = 0; i < el.length; i++) {
safWorker(el[i], xpath, depth);
}
}
}

safWorker(this);
safWorker(this, xpath, 0);

return ar;
}
Expand Down
40 changes: 40 additions & 0 deletions nested-lambda-demo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<!DOCTYPE html>

<script src='js/pjxml.js'></script>

<script>
function parsexml(text)
{
const doc = pjXML.parse(text);
const paragraphs = document.getElementsByTagName("p");
for (var i = 0; i < paragraphs.length; ++i)
{
const val = paragraphs[i].id;
const key = isNaN(val) ? "name" : "number";
paragraphs[i].innerText = doc.selectAll(
r => !(r.name == "score-partwise") ? false :
r => !(r.name == "part") ? false :
r => !(r.name == "measure") ? false :
r => !(r.name == "note") ? false :
r => !(r.name == "lyric" && r.attributes[key] == val) ? false :
r => r.name == "text").map(node => node.text()).join(" | ");
}
content.innerText = text;
}

function filechange()
{
const reader = new FileReader();
reader.addEventListener("load", () => parsexml(reader.result));
reader.readAsText(xml.files.item(0));
}
</script>

<h3>Demo on parsing MusicXML with pjXML using nested lambdas</h3>
<h4>Usage: Download <a href='https://lilypond.org/doc/v2.25/input/regression/musicxml/71/lily-1941d34e.xml'>lily-1941d34e.xml</a> and drop it into the below file input field.</h4>
<input type='file' id='xml' onchange='filechange()'>
<hr><p id='1'></p>
<hr><p id='2'></p>
<hr><p id='Verse'></p>
<hr><p id='Chorus'></p>
<hr><pre id='content'></pre>