检查字符串是 HTML 还是 XML

发布于 2024-11-16 10:49:52 字数 148 浏览 0 评论 0原文

有没有办法在 JavaScript 中检查字符串是 HTML 还是 XML?最好使用 jQuery 而不是其他库? 我之所以需要这样做是因为我需要知道是否可以有一个可以向其中传递 XML 或 HTML 的函数。如果是 HTML,我们采取一项操作;如果是 XML,我们采取另一项操作。

Is there a way to check if a String is HTML or XML in JavaScript? Preferably using jQuery rather than some other library?
Why I need to do this is because I need to know if it possible to have a function into which XML or HTML can be passed. If it is HTML we take one action and if it is XML we take another action.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雪化雨蝶 2024-11-23 10:49:52

有没有办法在 JavaScript 中检查字符串是 HTML 还是 XML?

不可靠。您可以测试其中一个(例如针对 DTD 或 XSD),如果失败,则假设是另一个。但是,这些测试应该在具有有效 DOCTYPE 的整个文档上运行。在许多情况下,标记片段将通过多种标记语言的验证。然后呢?

我认为您需要解释为什么您需要知道其中的区别。

Is there a way to check if a String is HTML or XML in JavaScript?

Not reliably. You can test for one (e.g. against a DTD or XSD) and if it fails, assume it's the other. However, those tests are meant to be run on entire documents with a valid DOCTYPE. There are many cases where a snippet of markup will pass validation for multiple markup languages. What then?

I think you need to explain why you need to know the difference.

远昼 2024-11-23 10:49:52

规则是,如果它以 开头,那么它是基于XML的。如果没有它,则不应将其视为 XML,因为 XML 需要该特定标记。

“XML 文件或流由以下结构组成:
一个或多个处理指令,最常见的是必需的 "

http://en.wikipedia.org/wiki/XML

The rule is that if it starts with <?xml version="1.0"> then it's XML-based. If it doesn't have it, it should NOT be considered XML as XML requires that particular tag.

"An XML file or stream is made up of the following structures:
One or more Processing directives, the most common being the required <?xml version="1.0">"

http://en.wikipedia.org/wiki/XML

未蓝澄海的烟 2024-11-23 10:49:52

我可以想到两种方法来做到这一点。

暴力方法是拥有所有有效 html 元素的列表,如果它不是其中之一,那么它一定是 xml。这可能是最干净的方法。

如果您对 html 使用命名空间,那么 xml 可能会位于默认命名空间中,因此,如果您在此页面 (https://developer.mozilla.org/en/Introduction_to_using_XPath_in_JavaScript)您将看到,在使用 XPath 评估时,默认命名空间中的任何内容都将返回 null。

根据此页面(http://www .nczonline.net/blog/2009/04/04/xpath-in-javascript-part-3/),xpath 不适用于 HTML 元素,这将是区分 xml 和 html 的另一种方法。

我没有使用过xpath,但根据此页面,不同浏览器的实现可能有所不同: http://www.yaldex.com/ajax-tutorial-4/BBL0029.html,因为 Firefox 可能允许 xpath 处理 html 元素。

因此,xpath 可能是一个很好的解决方案,具体取决于支持的浏览器以及您的 xml 是否可以使用名称空间,但由于 html 中支持的元素数量是有限的,因此在该组中查找似乎是最精确的解决方案。

I can think of two ways to do this.

The brute-force method is to have a list of all the valid html elements and if it isn't one of those then it must be xml. This may be the cleanest approach.

If you use a namespace for the html then the xml will probably be in a default namespace, so if you look for this line (Implementing a default namespace for XML documents) on this page (https://developer.mozilla.org/en/Introduction_to_using_XPath_in_JavaScript) you will see that anything in the default namespace will return null when using an XPath evaluation.

According to this page (http://www.nczonline.net/blog/2009/04/04/xpath-in-javascript-part-3/), xpath doesn't work on HTML elements, which would be another way to tell xml from html.

I haven't used xpath, but the implementation may differ among different browsers, according to this page: http://www.yaldex.com/ajax-tutorial-4/BBL0029.html, as Firefox may allow xpath to work on html elements.

So, xpath may be a good solution, depending on browsers supported and whether your xml may use a namespace, but since the number of elements supported in html is finite it would seem that looking within that group would be the most precise solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文