[英]Check with Saxon-JS 2.1 if a DOM classList contains a specific class
Saxon-JS enables not only to run XSLT in the browser but also to read and write HTML content. Saxon-JS 不仅可以在浏览器中运行 XSLT,还可以读写 HTML 内容。 With JavaScript, checking if a class 'edited' is contained by an elements class list can be done by使用 JavaScript,检查 class 'edited' 是否包含在元素 class 列表中
document.getElementById(id).classList.contains('edited')
but how to achieve this with Saxon-JS 2.1?但是如何使用 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:get($node, 'class')
但我不知道如何通过 id 指定节点(这将返回整个 class 列表)
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). ixsl:eval('document.getElementById({$id}).classList.contains('edited')')
但我想不支持使用 {$id} 并且 'edited' 使用已用于两者的撇号包含 eval 参数和整个 xpath 语句(此处不包括)。 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(document.getElementById('+$id+').classList.contains('+$class+')')
在这里我收到一条错误消息,指出“没有为 xs:string 类型的 arguments 定义算术运算符”。
ixsl:eval(concat('document.getElementById(',$id,').classList.contains(',$class,')'))
but even this doesn't work. ixsl:eval(concat('document.getElementById(',$id,').classList.contains(',$class,')'))
但即使这样也行不通。 It gets compiled into sef but on runtime an error "cannot read property of null" occurs.它被编译成 sef 但在运行时出现错误“无法读取 null 属性”。
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. 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.
If you want to return any XML element that matches that condition, you could just use a standard XPath expression:如果您想返回任何符合该条件的 XML 元素,您可以使用标准 XPath 表达式:
//*[@id eq $id][contains-token(@class, 'edited')]
See: fn:contains-token请参阅: fn:contains-token
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.