如何记录 XML 文件的结构
当谈到记录 XML 文件的结构时......
我的一位同事在 Word 表格中进行了这项工作。
另一种方法将元素粘贴到 Word 文档中,并带有如下注释:
<learningobject id="{Learning Object Id (same value as the loid tag)}"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://www.aicpcu.org/schemas/cms_lo.xsd">
<objectRoot>
<v>
<!-- Current version of the object from the repository. !-->
<!-- (Occurance: 1) -->
</v>
<label>
<!-- Name of the object from the repository. !-->
<!-- (Occurance: 0 or 1 or Many) -->
</label>
</objectRoot>
首选哪种方法?有更好的办法吗?
是否有其他选项不需要第三方架构文档工具来更新?
When it comes to documenting the structure of XML files...
One of my co-workers does it in a Word table.
Another pastes the elements into a Word document with comments like this:
<learningobject id="{Learning Object Id (same value as the loid tag)}"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://www.aicpcu.org/schemas/cms_lo.xsd">
<objectRoot>
<v>
<!-- Current version of the object from the repository. !-->
<!-- (Occurance: 1) -->
</v>
<label>
<!-- Name of the object from the repository. !-->
<!-- (Occurance: 0 or 1 or Many) -->
</label>
</objectRoot>
Which one of these methods is preferred? Is there a better way?
Are there other options that do not require third party Schema Documenter tools to update?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我将编写一个 XML 架构 (XSD) 文件来定义 XML 文档的结构。可以包含
xs:annotation
和xs:documentation
标签来描述元素。可以使用 XSLT 样式表(例如 xs3p 或工具(例如 XML 架构文档管理器。有关 XML 架构的介绍,请参阅 XML Schools 教程。
这是您的示例,表示为带有
xs:annotation
标记的 XML 架构:I'd write an XML Schema (XSD) file to define the structure of the XML document.
xs:annotation
andxs:documentation
tags can be included to describe the elements. The XSD file can be transformed into documentation using XSLT stylesheets such as xs3p or tools such as XML Schema Documenter.For an introduction to XML Schema see the XML Schools tutorial.
Here is your example, expressed as XML Schema with
xs:annotation
tags:享受 RELAX NG 紧凑语法
通过尝试各种 XML 模式语言,我发现 RELAX NG 最适合大多数情况(推理在最后)。
要求
修改后的示例 XML (doc.xml)
我添加了一个属性,以说明文档中的这种类型的结构。
使用带注释的 RELAX NG 紧凑语法 (schema.rnc)
RELAX NG 允许通过以下方式描述示例 XML 结构:
我认为,保持给定的表达水平,很难超越简单性。
如何注释结构
##
前缀,该前缀会自动转换为其他模式格式的文档元素。单个哈希#
转换为 XML 注释,而不是文档元素。多个连续注释(如示例中所示)将变成单个元素中的单个多行文档字符串。
明显的事实:
doc.xml
中的内联 XML 注释无关紧要,只有schema.rnc
中的内容才算数。如果需要 XML Schema 1.0,请生成它 (schema.xsd)
假设您有一个名为
trang
的(开源)工具可用,您可以创建一个 XML Schema 文件,如下所示:生成的架构如下所示:
现在,坚持只使用 XML Schema 1.0 的客户可以使用您的 XML 文档规范。
根据 schema.rnc 验证 doc.xml
有一些开源工具,例如
jing
和rnv
,支持 RELAX NG Compact 语法,并且可以在 Linux 和 MS Windows 上运行。注意:这些工具相当旧,但非常稳定。将其视为稳定的标志,而不是过时的标志。
使用 jing:
-c
很重要,jing
默认情况下假定 XML 形式的 RELAX NG。使用
rnv
检查schema.rnc
本身是否有效:并验证
doc.xml
:rnv
允许一次验证多个文档:RELAX NG 语法紧凑 - 优点
RELAX NG 限制
结论
对于上面定义的要求,RELAX NG Compact 语法看起来最适合。通过 RELAX NG,您可以获得两种 - 人类可读的模式,甚至可用于自动验证。
现有的限制并不经常生效,在许多情况下可以通过评论或其他方式解决。
Enjoy RELAX NG compact syntax
Experimenting with various XML schema languages, I have found RELAX NG the best fit for most of the cases (reasoning at the end).
Requirements
Modified sample XML (doc.xml)
I have added one attribute, to illustrate also this type of structure in the documentation.
Use RELAX NG Compact syntax with comments (schema.rnc)
RELAX NG allows describing sample XML structure in the following way:
I think, it is very hard to beat the simplicity, keeping given level of expressiveness.
How to comment the structure
##
prefix, which is automatically translates into documentation element in other schema format. Single hash#
translates into XML comment and not a documentation element.multiple consecutive comments (as in the example) will turn into single multi-line documentation string within single element.
obvious fact: the inline XML comments in
doc.xml
are irrelevant, only what is inschema.rnc
counts.If XML Schema 1.0 is required, generate it (schema.xsd)
Assuming you have a (open sourced) tool called
trang
available, you may create an XML Schema file as follows:Resulting schema looks like this:
Now can your clients, insisting on using only XML Schema 1.0 use your XML document specification.
Validating doc.xml against schema.rnc
There are open source tools like
jing
andrnv
supporting RELAX NG Compact syntax and working on both Linux as well as on MS Windows.Note: those tools are rather old, but very stable. Read it as a sign of stability not as sign of being obsolete.
Using jing:
The
-c
is important,jing
by default assumes RELAX NG in XML form.Using
rnv
to check, theschema.rnc
itself is valid:and to validate
doc.xml
:rnv
allows validating multiple documents at once:RELAX NG Compact syntax - pros
RELAX NG limitations
Conclusions
For the requirement defined above, RELAX NG Compact syntax looks like the best fit. With RELAX NG you get both - human readable schema which is even usable for automated validation.
Existing limitations do not come into effect very often and can be in many cases resolved by comments or other means.
您可以尝试通过创建 XSD 架构来记录它,该架构将提供更正式的 XML 规范。许多工具都会以示例 XML 为起点,为您生成 XSD。
You might try documenting it by creating an XSD schema which would provide a more formal specification of your XML. Many tools will generate the XSD for you from sample XML as a starting point.
就我个人而言,我更喜欢在 XML 中看到它(第二种方式)。
将元素放在表中并不能清楚地告诉您哪些元素是哪些元素的父子元素等等。将其放入 XML 中会更加清晰,我可以看到发生了什么。
Personally, I would prefer seeing it in XML (the 2nd way).
Putting the elements in the table won't tell you clearly which elements are which elements' parent child and so on. Putting it in XML is rather clearer and I can see what's going on.
在表中显示它有其局限性,例如多层嵌套子项,但对于简单的 XML 结构,我认为这会很好。对于任何具有多个嵌套级别的内容,我更喜欢 XML 方式。
更好的方法是创建 XML 架构 (XSD) 文件。这样,您就可以获得在 XML 中查看它的好处,并且可以在使用某些软件根据架构文件输入数据后检查该文件。
有关 XSD 的一系列精彩教程,请查看 w3schools - XML 架构教程
Showing it in a table has its limitaions e.g. mulit-levels of nested children, but for a simple XML structure I think this would be fine. For anything with more than one nested level I would prefer the XML way.
An even better way would be to create an XML Schema (XSD) file. That way, you get the benifits of seeing it in XML, and you can check the file after the data is inputted against the schema file using some software.
For a great series of tutorials on XSD check out w3schools - XML Schema Tutorial
我只是想再添加一件事,以防有人觉得有用。
我有时用 HTML 编程,有时用 android 编程。当我做 HTML 时,我按照与 W3Schools 相同的格式记录我的自定义 XML,如 http://www .w3schools.com/tags/att_a_href.asp 如果这是我正在开发的 Android 项目,那么我会遵循 Google 标准,如 http://developer.android.com/guide/topics/manifest/activity-element.html#screen
这样,与我一起工作的程序员就不必做任何额外的工作来理解我的文档。
I just want to add one more thing, in case someone finds it useful.
I do sometimes programming in HTML and other times in android. When I do HTML I document my custom XML following the same format as W3Schools, as in http://www.w3schools.com/tags/att_a_href.asp if it is an android project I am working on then I follow Google standards as in http://developer.android.com/guide/topics/manifest/activity-element.html#screen
This way the programmers I work with do not have to do any extra work to understand my documentation.