Check with Saxon-JS 2.1 if a DOM classList contains a specific class

107 views Asked by At

Saxon-JS enables not only to run XSLT in the browser but also to read and write HTML content. With JavaScript, checking if a class 'edited' is contained by an elements class list can be done by

document.getElementById(id).classList.contains('edited')

but how to achieve this with Saxon-JS 2.1?

Possible ways seem to be

ixsl:get($node, 'class') but I don't know how to specify the node by id (and this would return the whole class list)

ixsl:eval('document.getElementById({$id}).classList.contains('edited')') but I suppose using {$id} isn't supported and 'edited' uses apostrophes which are already in use for both containing the eval parameter and the whole xpath statement (not included here). So both single and double apostrophes can't be used.

ixsl:eval(document.getElementById('+$id+').classList.contains('+$class+')') here I get an error stating that "arithmetic operator is not defined for arguments of types xs:string".

ixsl:eval(concat('document.getElementById(',$id,').classList.contains(',$class,')')) but even this doesn't work. It gets compiled into sef but on runtime an error "cannot read property of null" occurs.

I'm aware that I could write a JavaScript function into the HTML page and call that own function, but I want to accomplish it in the XSLT script.

1

There are 1 answers

5
pgfearo On BEST ANSWER

If you want to return any XML element that matches that condition, you could just use a standard XPath expression:

//*[@id eq $id][contains-token(@class, 'edited')]

See: fn:contains-token