简体   繁体   English

服务器端css选择器

[英]server-side css selectors

I am creating a tool that will check dynamically generated XHTML and validate it against expected contents. 我正在创建一个工具,它将检查动态生成的XHTML并根据预期的内容对其进行验证。

I need to confirm the structure is correct and that specific attributes exist/match. 我需要确认结构是否正确以及特定属性是否存在/匹配。 There may be other attributes which I'm not interested in, so a direct string comparison is not suitable. 可能还有其他我不感兴趣的属性,因此直接字符串比较不合适。

One way of validating this is with XPath, and I have implemented this already, but I would also like something less verbose - I want to be able to use CSS Selectors, like I can with jQuery, but on the server - within CFML code - as opposed to on the client. 验证这一点的一种方法是使用XPath,我已经实现了这个,但我还想要一些不那么冗长的东西 - 我希望能够使用CSS选择器,就像我可以使用jQuery,但在服务器上 - 在CFML代码中 - 而不是客户端。

Is there a CFML or Java library that allows me to use CSS Selectors against an XHTML string? 是否有CFML或Java库允许我对XHTML字符串使用CSS选择器?

I've just released an open source project which is a W3C CSS Selectors Level 3 implementation in Java. 我刚刚发布了一个开源项目,它是Java中的W3C CSS Selectors Level 3实现。 Please give it a try. 请试一试。 I was looking for the same thing and decided to implement my own engine. 我一直在寻找相同的东西,并决定实施自己的引擎。 It's inspired by the code in WebKit etc. 它受到了WebKit等代码的启发。

http://github.com/chrsan/css-selectors/tree http://github.com/chrsan/css-selectors/tree

If you can use PHP within your CFML (as mentioned above), you could take advantage of this excellent "jQuery for PHP" library, phpQuery 如果你可以在你的CFML中使用PHP(如上所述),你可以利用这个优秀的“jQuery for PHP”库, phpQuery

Full CSS selector support, manipulation functions, traversing, etc. It should work great for what you need. 完整的CSS选择器支持,操作功能,遍历等。它应该可以满足您的需求。

Hope it helps. 希望能帮助到你。

I don't know of a Java library itself, but there is a Ruby library called Hpricot that does exactly what you're looking for. 我不知道Java库本身,但是有一个名为Hpricot的Ruby库可以完全满足您的需求。 In conjunction with the Ruby implementation on the Java platform, JRuby , it should be relatively straightforward to call Ruby methods from your Java code (using BSF, JSR-222 Scripting APIs , or an internal API ). 结合Java平台上的Ruby实现, JRuby ,从Java代码(使用BSF, JSR-222 Scripting API内部API )调用Ruby方法应该相对简单。

Are you using Coldfusion 8? 你在使用Coldfusion 8吗? Coldfusion 8, being based on Java 6, supports JSR-222 Scripting APIs "javax.scripting". Coldfusion 8基于Java 6,支持JSR-222 Scripting API“javax.scripting”。

Take a look at this blog entry on embedding PHP within CFML . 看一下关于在PHP中嵌入PHP的博客文章 You should be able to do the same with Ruby . 你应该能够用Ruby同样的事情。 There is ZIP file example code linked from this blog posting, and if you crack open the CFML, you'll see a good example of embedding Ruby within CFML. 这个博客文章中链接了ZIP文件示例代码,如果您打开CFML,您将看到在CFML中嵌入Ruby的一个很好的示例。

Although it might take a bit of work to make all the pieces work together, but with a bit of investment, it should give you the robust parsing/CSS selector querying that you're looking for. 尽管将所有部分组合在一起可能需要一些工作,但是通过一些投资,它应该为您提供强大的解析/ CSS选择器查询您正在寻找的。

Hpricot is definetly a fantastic solution if the JRuby-route is open to you. 如果JRuby路线对你开放,Hpricot绝对是一个很棒的解决方案。

Wrt. WRT。 XPath being the "correct" way to access XML documents... sorry but this is rubbish. XPath是访问XML文档的“正确”方式...抱歉,这是垃圾。 There are numerous ways to access elements of an XML document: DOM traversal, XPath, XQuery, CSS selectors to name a few. 有许多方法可以访问XML文档的元素:DOM遍历,XPath,XQuery,CSS选择器等等。 XPath is certainly popular but CSS selectors are very very powerful, assuming your XML document has HTML semantics. XPath当然很受欢迎,但CSS选择器非常强大,假设您的XML文档具有HTML语义。

There is a theoretical difference between the server and client. 服务器和客户端之间存在理论差异。 To a web browser, the document is a living DOM hierarchy. 对于Web浏览器,该文档是一个活动的DOM层次结构。 To your server code it's merely an XML document of whatever type. 对于您的服务器代码,它只是一个任何类型的XML文档。 XPath is the "correct" way to access elements of an XML document. XPath是访问XML文档元素的“正确”方法。

So unless you have a serious performance problem with your current XPath solution, or it doesn't actually work correctly, I suggest you stick with it. 因此,除非您当前的XPath解决方案存在严重的性能问题,否则它实际上无法正常工作,我建议您坚持使用它。 Trying something too clever brings the risk of breaking something that's working. 尝试一些太聪明的东西会带来破坏正常工作的风险。

If you find the XPath to be too verbose and ugly to leave sitting around, or want more power to re-use the tool in different cases, or just can't resist trying to do something clever, then you could try writing a utility that compiles a given CSS selector into an XPath. 如果你发现XPath 冗长和丑陋而不能坐下来,或者想要在不同的情况下重用这个工具,或者只是无法抗拒尝试做一些聪明的事情,那么你可以尝试编写一个实用程序,将给定的CSS选择器编译为XPath。 You could then call this in one line whenever you needed. 然后,您可以随时在一行中调用它。

it may be easier to use cQuery.com - cQuery.com is an API based 'Content Query Engine' to extract content from live websites by using CSS. 使用cQuery.com可能更容易--cQuery.com是一个基于API的“内容查询引擎”,用于通过CSS从实时网站中提取内容。

You can using it programatically in you application. 您可以在您的应用程序中以编程方式使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM