XML注释可以在任何地方吗? [英] Can XML comments go anywhere?

查看:541
本文介绍了XML注释可以在任何地方吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个简单的工具,使用用户输入的查询来生成DBUnit XML数据集。我想将每个在XML中输入的查询作为注释包括在内,但是生成XML文件的DBUnit API不支持在我想要的地方插入注释(在它生成的数据上方),所以我使用



所以我的问题:是否有效的XML将它放置在任一位置?例如,在XML声明之上:

 <! -  Queries used:...  - > 
<?xml version ='1.0'encoding ='UTF-8'?>
< dataset>
...
< / dataset>

或在根节点下面:

 <?xml version ='1.0'encoding ='UTF-8'?> 
< dataset>
...
< / dataset>
<! - 使用的查询:... - >

我计划最初尝试XML声明之上,但我怀疑,如果这是有效的XML,尽管来自维基百科的声明:


注释可以放置在树的任何位置,包括在文本中,如果元素的内容是文本或#PCDATA。




我计划发布回来,如果这个工作,但它很高兴知道它是否是一个官方的XML标准。



更新:请参阅我在下面的回复

解决方案

根据 XML规范,格式良好的XML文档是:


document :: = prolog元素Misc *


其中 prolog


prolog :: = XMLDecl ? Misc *(doctypedecl Misc *)?



$ b b


XMLDecl :: ='<?xml'VersionInfo EncodingDecl? SDDecl? S? '?&';'


这意味着,如果你想在顶部有评论





p> 2.5注释



[定义:注释可能出现在文档中的其他位置,此外,它们可能出现在文档类型声明中语法允许的位置。它们不是文档的字符数据的一部分;一个XML处理器可以,但不需要,使应用程序可以检索注释的文本。为了兼容性,字符串 - (双连字符)不能出现在注释中。]参数实体引用在注释中不能被识别。



$ b b

所有这一切意味着您可以在不在其他标记中的任何位置放置评论,除非如果您以评论发表,则不能有XML声明。



然而,虽然在理论理论与实践一致,但在实践中它不是,所以我很想知道你的实验是如何工作的。

I wrote a simple tool to generate a DBUnit XML dataset using queries that the user enters. I want to include each query entered in the XML as a comment, but the DBUnit API to generate the XML file doesn't support inserting the comment where I would like it (above the data it generates), so I am resorting to putting the comment with ALL queries either at the top or bottom.

So my question: is it valid XML to place it at either location? For example, above the XML Declaration:

<!-- Queries used: ... -->
<?xml version='1.0' encoding='UTF-8'?>
<dataset>
  ...
</dataset>

Or below the root node:

<?xml version='1.0' encoding='UTF-8'?>
<dataset>
  ...
</dataset>
<!-- Queries used: ... -->

I plan to initially try above the XML Declaration, but I have doubts on if that is valid XML, despite the claim from wikipedia:

Comments can be placed anywhere in the tree, including in the text if the content of the element is text or #PCDATA.

I plan to post back if this works, but it would be nice to know if it is an official XML standard.

UPDATE: See my response below for the result of my test.

解决方案

According to the XML specification, a well-formed XML document is:

document ::= prolog element Misc*

where prolog is

prolog ::= XMLDecl? Misc* (doctypedecl Misc*)?

and

XMLDecl ::= '<?xml' VersionInfo EncodingDecl? SDDecl? S? '?>'

which means that, if you want to have comments at the top, you cannot have an XML type declaration.

The specification agrees with Wikipedia on comments:

2.5 Comments

[Definition: Comments may appear anywhere in a document outside other markup; in addition, they may appear within the document type declaration at places allowed by the grammar. They are not part of the document's character data; an XML processor MAY, but need not, make it possible for an application to retrieve the text of comments. For compatibility, the string "--" (double-hyphen) MUST NOT occur within comments.] Parameter entity references MUST NOT be recognized within comments.

All of this together means that you can put comments anywhere that's not inside other markup, except that you cannot have an XML declaration if you lead with a comment.

However, while in theory theory agrees with practice, in practice it doesn't, so I'd be curious to see how your experiment works out.

这篇关于XML注释可以在任何地方吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆