Obtaining unique elements on a page (and their attributes) via XQuery

August 20th, 2010

Though I made a post just now at http://blog.brett-zamir.me/?p=196 on using XQuery to build XSL, I realized that other people not interested in XSL may still like to be able to find out what unique elements (and their attributes) are being used in an X/HT/ML document. Here’s some XQuery for that:

let $uniqueNames :=
    for $name in distinct-values(doc()//*/local-name())
    order by $name
    return $name
for $name in $uniqueNames
    return
        ('Element: ', $name,
            (
                let $attList := (
                    let $names := doc()//*[local-name() = $name]
                    for $att in distinct-values($names/@*/local-name())
                    order by $att
                    return $att
                )
                return
                    if (empty($attList)) then '
'
                    else ('
',
                        'Attributes: ',
                        $attList, '
'
                    ),
                    '
'
            )
        )

XQuery for identifying templates needed in XSL

August 20th, 2010

Today I was looking briefly at how I could update my Firefox extension to migrate to HTML5 (having problems with XUL layouts, but it is also not a bad idea to future-proof and make available the code in other browsers to the extent it is currently possible to support certain functionality of Firefox add-ons, as they can be quite powerful and HTML5 has not caught up yet).

I’ve heard a lot of theoretical talk about using XSL to convert XUL to HTML, but haven’t really seen anything. That would be quite ambitious, but sorry, but I don’t think I’ll be attempting that here (a lot of XUL could be reused once XBL 2.0 gets implemented as Firefox is supposedly going to do, and though there is a nice XBL JavaScript library at http://code.google.com/p/xbl2/ , it is not fully complete, and in any case, these solutions require extra code, while being purely in HTML I think would be more elegant for cases where the XUL tag is not all that complex).

Anyhow, I wanted to find out which elements my XUL was using, and also the attributes on these elements. XQuery once again is awesome for extracting text in a very short order (the only real ugliness here is XQuery’s line break which I think would be nicer, if geekier, to use \n (though guess I could use a character reference instead)).

The following XQuery code can be used for any XML document (though it doesn’t distinguish languages at the moment), finding out which unique elements were used, turning these into XSL templates, and then as a convenience, putting inside it, the unique attributes to be handled on these elements (I know I should probably make the XSL to handle the attributes, but just starting simple).

(For anyone following my Identica/Twitter feed, we’ve got a project started now at http://code.google.com/p/jsxqueryparser/ to make the beauty of XQuery available to JavaScript. My own special interest in this is allowing users the ability to query collections of IndexedDB content (or possibly remote Ajax).)

declare namespace xsl="http://www.w3.org/1999/XSL/Transform";
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">{
let $uniqueNames := for $name in distinct-values(doc()//*/local-name()) order by $name return $name
for $name in $uniqueNames
return
(<xsl:template match="{$name}">
{
let $attList :=
(
let $names := doc()//*[local-name() = $name]
for $att in distinct-values($names/@*/local-name())
order by $att
return $att
)
return if (empty($attList)) then '
' else ('
', $attList, '
')
}
</xsl:template>, '

')}</xsl:stylesheet>

IE9 not to support HTML XPath

July 27th, 2010

Got a reply from Microsoft regarding XPath. Although I’m a bit disappointed full XPath support on HTML documents is not making it in (hopefully next time), I’ll add my voice to those praising Microsoft for vastly improving adherence to standards in IE9. Expecting it to keep getting better…

—————————–

Brett-

Nothing is changing here for IE9; if you want to use XPATH, you’ll need to
use MSXML. However, you may find that the Selectors API does everything you need, and because it’s baked into the DOM directly, it provides extremely high performance.

-Eric

From: Brett Zamir [mailto:....]
Sent: Sunday, July 25, 2010 4:52 PM
Subject: MSDN Blogs: Contact request: XPath

Subject: XPath

Hi, Was just wondering if IE9 would be covering XPath so as to work in a standard way on regular HTML documents? This is a very powerful feature. (Since browsers do not seem to have plans on the horizon to implement XQuery, it would be great if at least XPath 1.0 could be available.)

XDIB

July 19th, 2010

This page is to host comments pertaining to my Firefox add-on, XDIB (XML Database in a Browser) at https://addons.mozilla.org/en-US/firefox/addon/199900/ .

Ideas on XQuery in the browser and implications for jQuery

June 21st, 2010

My interest is to see the web become a database, and one’s (local) databases become accessible to the web.

I believe the former can be done by making Sedna, a native XML database, available as an XPCOM component to Mozilla/Firefox allowing user permission for sites to write to or access databases (for their own use, but also potentially sharing local databases across sites), while the latter could be done by some additional code in Firefox allowing the user to be asked by a site for permission to make cross-domain searches (or even posts for that matter) even without that other site’s permission (as can be done by servers). There are security concerns, but with the user’s permission (as is possible in signed scripts), it should be possible to overcome this traditional server-client-server barrier (as well as the client-server barrier).

I also have partially changed my mind since posting about making jQuery accessible to users of XML databases (and the ones preceding it) as I do wish to continue implementing Sedna, an XML database with XQuery/XUpdate (hopefully XQUF in future) features, in Firefox.

jQuery’s use of CSS Selectors, while more familiar to more developers (probably since CSS is already familiar to many developers), has the disadvantage of being less powerful than XPath (I believe categorically so), and XPath is only subset of the even more powerful XQuery.

Instead of dumbing down XPath by converting it to jQuery, I would like to see jQuery convertible to XPath. Thus, people could use jQuery against an XQuery-compliant engine or database if they wished, but also perform XPath or XQueries against the same data.

There is also something here for jQuery itself though. Inspired by XQuery’s use of doc() or collection(), and my interest in seeing jQuery accessible to users of XML databases, I thought of an idea for jQuery (which would also need to be given permission to access other sites, ideally I feel, allowing explicit user permission to access other sites, albeit in a potentially sandboxed connection to avoid the site having a chance to grab sensitive data unless the user explicitly allowed it).

For synchronous connections, the following syntax might be usable:

1
$('doc("http://example.com") h1.someClass').each(function (h1) {});

For asynchronous connections, the following might be used:

1
$('doc("http://example.com") h1.someClass', function (h1s) {});

Maybe that could be modified to run the function for each result, but after an asynchonous connection:

1
$each('doc("http://example.com") h1.someClass', function (h1) {});

Even more interesting uses involve collections which could either be aggregated files stored locally, or a set of server-side files which jQuery was configured to consider as a single collection.

1
2
var collec = {myShakespeareWorksCollection: ['Hamlet.html', 'Macbeth.html']};
$('collection("myShakespeareWorksCollection") .irony', function (ironicPassages) {}, collec);

I believe the above fits in well with the intuitive API of jQuery and simplifies its API even further with at least Ajax GET requests (with return type XML/HTML). And it could of course work even without an additional browser add-on or web standard giving unrestricted access to other clients, if the URL were restricted to same domain or explicitly permitted requests.

Open URIs

June 3rd, 2010

This page is for Open URIs, an extension which aims to offer a means of trying different protocols for the same link. Feel free to leave comments or bug reports here.

Politics and Truth

April 27th, 2010

I don’t think there is anything more obstructive to finding truth and spirituality than the spirit of partisanship and politics. It is not that “politics” is a euphemism for getting in the way of truth–politics in itself, at least in the current partisan system that exists, really itself gets in the way of finding truth.

I do not mean the spirit of seeking justice, including through law, as that is a very high expression of such virtues. But “justice”, related to “judging” with fairness (and acting accordingly) is something which so few people can do. In fact, I think the world is so steeped in politics that the ability for anyone to actually be fair is very, very low.

If a person were to become truly just, and freed from partisanship, while adequately concerned with justice, both in its connotation of fairness for all, and in its connotation of perceiving things through one’s own eyes in order to judge fairly, and recognize merits wherever they are, the world will not know what to think of such a person. In fact, everyone would likely oppose them, since partisans–on both sides–are too vested in defending their narrow strongholds of orthodoxy.

Maybe I am not being fair, since it is the moderates who often evoke the widest and longest lasting, if not strongest support (at least those who are moderate not in the sense of pandering and vote-seeking, or moral equivocation). But even with courtesy, one still does not make friends–unless those friends are themselves eager to find truth wherever it leads–by giving a balanced view-point.

As it is my belief that religion–true religion–holds the key to bringing the true moderation that we need, albeit primarily in our personal and social rather than political lives, I think the biggest failure is when those of us who acquire such a legacy, out of a lack of a true belief, due often to a lack of diligent investigation, which could bring ourselves courage and faith, water down a true religion according to the political lenses and tint of the wider society.

Because of this unfortunate reality, even the word religion is still mistakenly associated with politics, whereas true religion is really the realm of opening our eyes to issues that run even deeper, while still allowing us to acquire a frame of reference and keen insights into political discourse which can balance one so as to avoid getting drawn into the fruitless squabbling of partisan politics, while inspiring one to more emphatically draw attention to the less controversial, but as in family or friend squabbles, more fundamental ailments of the system.

Baha’u'llah lists the requirements of a true seeker as one who abandons (limited) loves and hates as either can keep one from the truth, while avoiding back-biting–maybe the fundamental cause of partisanship where fault is found in others and dwelt upon, rather than looking to one’s own shortcomings (“own” as a group as well as individual).

Recognizing my own weaknesses we well as those of others’, I believe it is only through a True Physician that it is possible for one’s eyes to be drawn completely away from the distractions of tendencies to take unbalanced sides of the issues of the day.

Color Source

April 18th, 2010

This page is for comments or help regarding the Mozilla Thunderbird extension “Color Source”: https://addons.mozilla.org/en-US/firefox/addon/146367

Patent? Nonsense!

February 7th, 2010

Feel free to place here any comments about the Firefox add-on, Patent? Nonsense! (a front-end for converting video files into Ogg Theora format).

My JavaScript

February 4th, 2010

There are 3 changes/additions to JavaScript which I think would make my work in JavaScript programming much more easy (and fun).

  • Have a very short alias for “function”
  • Avoid need for parentheses around function (or even method) calls (or even possibly definitions)
  • Allow object destructuring and allow destructuring (of arrays or objects) within function definitions

Short alias for “function”

This is JavaScript. We use functions for everything: “classes”, as data, etc.  Let’s please drop the need for this long name… “def” is used in Python, how about that?

No parentheses needed around function calls or even definitions

I was checking out some old Apple IIe BASIC files and was not only nostalgic, but sad that one can’t just quickly type up a program like in those days. I don’t like having to reach around something to put something redundant at the end.

How much I’d like to just do this when debugging:

alert testVar

And even this:

function myName param1, param2 {

}

Allow object destructuring and allow destructuring within function definitions

Object destructuring, where objects preserve property order (depends on predictable iteration order, genuine deletability of properties including their position, etc.)

[n, n2] = {num1: 123, num2:456};
alert n+n2; // 579
[k:v, k2:v2] = {num1:123, num2:456};
alert k2+':'+v2; // "num2:456"

Object destructuring, where objects don’t need to have properties in order

Get only the properties we need:

{prop:property} = {something: 'someValue', prop: 'val'};
alert property; // 'val'

Assuming variable can be defined with same name as property, we can use this simple form (esp. useful for functions–see next section):

{prop} = {something: 'someValue', prop: 'val'};
alert prop; // 'val'

Destructuring within function definitions

Especially in function libraries where one cannot easily change the API without pain for its consumers, it is nice for functions to accept (unordered) objects for configurability so that new configuration options do not disturb the existing API.

This also avoids the need for the user to remember the order of the function’s arguments (though they have to remember the more semantic property names), and it makes clear in the calling code (without needing to check the called function) how the called function is being used,

However, currently in JavaScript, it is a bit of a pain to write such such an API.

function listPrefs (obj) {
    var newPrefName1 = obj.pref1,
        newPrefName2 = obj.pref2,
        newPrefName3 = obj.pref3;
    alert(newPrefName1+newPrefName2+newPrefName3);
}

But in object destructuring (especially the unordered kind), we can do this,

keeping everything in the function’s parameter declaration:

function listPrefs ({pref1:newPrefName1, pref2:newPrefName2, pref3:newPrefName3}) {
    alert(newPrefName1+namePrefName2+namePrefName3);
}

And even more succinctly, where we don’t need to rename the variable, to just be able to do this:

function listPrefs ({pref1, pref2, pref3}) {
    alert(pref1+pref2+pref3);
}

Or yet further abbreviated, but with the same potential conflicts as the ‘with’ statement:

function listPrefs ({}) { // Empty could represent importing all properties into local scope (an empty object here otherwise would have no use)
    alert(pref1+pref2+pref3);
}

Note that the preferences in these scenarios do not need need to be in that order on the object; for that, we would use an array–which has the connotation of being ordered:

function listPrefs ([key1:val1, key2:val2]) {
    alert(key1+':'+val1+'\n'+key2+':'+val2);
}

If we didn’t care about a key (or on the other hand, if we didn’t care about a value), but we did care about the order, we could do:

function listPrefs ([key1:, :val2]) {
    alert(key1+':'+val2);
}

Actually, for cases where we don’t care about the key, we could simply treat as an array, by dropping the initial colon:

function listPrefs ([key1:, val2]) {
    alert(key1+':'+val2);
}

And of course, this ought to work for regular numeric arrays as well:

function listPrefs ([val1, val2]) {
    alert(val1+':'+val2);
}

Or to bring together all of our innovations:

def listPrefs {pref1, prop2:val2} { // Reuse name of property in one case, and rename in the other
    alert pref1+','+val2;
}

Looks clean and succinct, and let’s us focus on the business of programming (and our users focus on our API without requiring us to use JSDocToolkit syntax or the like, at least for the sake of knowing what property names are expected/allowed).

Besides avoiding the function body being cluttered with distracting definitions, the above has the advantage of making explicit (and easily documents) what types of properties (in a semantic sense) that the function is expecting.

This discussion states: We decided against object property shorthand in destructuring…This would be nice and concise, but possibly so terse as to be confusing. Part of the justification of destructuring is that its syntax exactly mimics that of structuring.

While I think we can all appreciate their concern, I think this is a huge loss, particularly for the sake of function definitions. If people can understand array destructuring (not to speak of iterators or generators), then they can understand object destructuring. The concepts and appearance are not that different.