如何记录 XML 文件的结构

发布于 2024-08-12 00:22:56 字数 754 浏览 3 评论 0原文

当谈到记录 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 技术交流群。

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

发布评论

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

评论(6

腻橙味 2024-08-19 00:22:56

我将编写一个 XML 架构 (XSD) 文件来定义 XML 文档的结构。可以包含 xs:annotationxs:documentation 标签来描述元素。可以使用 XSLT 样式表(例如 xs3p 或工具(例如 XML 架构文档管理器

有关 XML 架构的介绍,请参阅 XML Schools 教程

这是您的示例,表示为带有 xs:annotation 标记的 XML 架构:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="objectroot">
    <xs:complexType>
      <xs:sequence>
        
        <xs:element name="v" type="xs:string">
          <xs:annotation>
            <xs:documentation>Current version of the object from the repository.</xs:documentation>
          </xs:annotation>
        </xs:element>

        <xs:element name="label" minOccurs="0" maxOccurs="unbounded" type="xs:string">
          <xs:annotation>
            <xs:documentation>Name of the object from the repository.</xs:documentation>
          </xs:annotation>
        </xs:element>
        
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

I'd write an XML Schema (XSD) file to define the structure of the XML document. xs:annotation and xs: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:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="objectroot">
    <xs:complexType>
      <xs:sequence>
        
        <xs:element name="v" type="xs:string">
          <xs:annotation>
            <xs:documentation>Current version of the object from the repository.</xs:documentation>
          </xs:annotation>
        </xs:element>

        <xs:element name="label" minOccurs="0" maxOccurs="unbounded" type="xs:string">
          <xs:annotation>
            <xs:documentation>Name of the object from the repository.</xs:documentation>
          </xs:annotation>
        </xs:element>
        
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>
水波映月 2024-08-19 00:22:56

享受 RELAX NG 紧凑语法

通过尝试各种 XML 模式语言,我发现 RELAX NG 最适合大多数情况(推理在最后)。

要求

  • 允许记录 XML 文档结构
  • 以可读的形式进行 使
  • 作者保持简单

修改后的示例 XML (doc.xml)

我添加了一个属性,以说明文档中的这种类型的结构。

<objectRoot created="2015-05-06T20:46:56+02:00">
    <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>

使用带注释的 RELAX NG 紧凑语法 (schema.rnc)

RELAX NG 允许通过以下方式描述示例 XML 结构:

start =

## Container for one object
element objectRoot {

    ## datetime of object creation
    attribute created { xsd:dateTime },

    ## Current version of the object from the repository
    ## Occurrence 1 is assumed by default
    element v {
        text
    },

    ## Name of the object from the repository
    ## Note: the occurrence is denoted by the "*" and means 0 or more
    element label {
        text
    }*
}

我认为,保持给定的表达水平,很难超越简单性。

如何注释结构

  • 始终将注释放在相关元素之前,而不是之后。
  • 为了便于阅读,请在注释块之前使用一个空行,
  • 使用 ## 前缀,该前缀会自动转换为其他模式格式的文档元素。单个哈希 # 转换为 XML 注释,而不是文档元素。
  • 多个连续注释(如示例中所示)将变成单个元素中的单个多行文档字符串。

  • 明显的事实:doc.xml 中的内联 XML 注释无关紧要,只有 schema.rnc 中的内容才算数。

如果需要 XML Schema 1.0,请生成它 (schema.xsd)

假设您有一个名为 trang 的(开源)工具可用,您可以创建一个 XML Schema 文件,如下所示:

$ trang schema.rnc schema.xsd

生成的架构如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
  <xs:element name="objectRoot">
    <xs:annotation>
      <xs:documentation>Container for one object</xs:documentation>
    </xs:annotation>
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="v"/>
        <xs:element minOccurs="0" maxOccurs="unbounded" ref="label"/>
      </xs:sequence>
      <xs:attribute name="created" use="required" type="xs:dateTime">
        <xs:annotation>
          <xs:documentation>datetime of object creation</xs:documentation>
        </xs:annotation>
      </xs:attribute>
    </xs:complexType>
  </xs:element>
  <xs:element name="v" type="xs:string">
    <xs:annotation>
      <xs:documentation>Current version of the object from the repository
Occurance 1 is assumed by default</xs:documentation>
    </xs:annotation>
  </xs:element>
  <xs:element name="label" type="xs:string">
    <xs:annotation>
      <xs:documentation>Name of the object from the repository
Note: the occurance is denoted by the "*" and means 0 or more</xs:documentation>
    </xs:annotation>
  </xs:element>
</xs:schema>

现在,坚持只使用 XML Schema 1.0 的客户可以使用您的 XML 文档规范。

根据 schema.rnc 验证 doc.xml

有一些开源工具,例如 jingrnv,支持 RELAX NG Compact 语法,并且可以在 Linux 和 MS Windows 上运行。

注意:这些工具相当旧,但非常稳定。将其视为稳定的标志,而不是过时的标志。

使用 jing:

$ jing -c schema.rnc doc.xml

-c 很重要,jing 默认情况下假定 XML 形式的 RELAX NG。

使用rnv检查schema.rnc本身是否有效:

$ rnv -c schema.rnc

并验证doc.xml

$ rnv schema.rnc doc.xml

rnv允许一次验证多个文档:

$ rnv schema.rnc doc.xml otherdoc.xml anotherone.xml

RELAX NG 语法紧凑 - 优点

  • 是非常易读,即使是新手也应该​​理解文本
  • 易于学习(RELAX NG 附带很好的教程,一天之内就可以学会大部分内容)
  • 非常灵活(尽管事实上,它看起来很简单,它涵盖了很多情况,其中一些甚至无法通过 XML Schema 1.0 解决)。
  • 存在一些用于转换为其他格式的工具(RELAX NG XML 形式、XML Schema 1.0、DTD,甚至生成示例 XML 文档)。

RELAX NG 限制

  • 多重性只能是“零或一”、“仅一”、“零或多个”或“一或多个”。 (少量元素的多重性可以通过“零或一个”定义的“愚蠢重复”来描述)
  • 存在 XML Schema 1.0 构造,无法通过 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

  • Allow documenting XML document structure
  • Do it in readable form
  • Keep it simple for the author

Modified sample XML (doc.xml)

I have added one attribute, to illustrate also this type of structure in the documentation.

<objectRoot created="2015-05-06T20:46:56+02:00">
    <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>

Use RELAX NG Compact syntax with comments (schema.rnc)

RELAX NG allows describing sample XML structure in the following way:

start =

## Container for one object
element objectRoot {

    ## datetime of object creation
    attribute created { xsd:dateTime },

    ## Current version of the object from the repository
    ## Occurrence 1 is assumed by default
    element v {
        text
    },

    ## Name of the object from the repository
    ## Note: the occurrence is denoted by the "*" and means 0 or more
    element label {
        text
    }*
}

I think, it is very hard to beat the simplicity, keeping given level of expressiveness.

How to comment the structure

  • always place the comment before relevant element, not after it.
  • for readability, use one blank line before the comment block
  • use ## 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 in schema.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:

$ trang schema.rnc schema.xsd

Resulting schema looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
  <xs:element name="objectRoot">
    <xs:annotation>
      <xs:documentation>Container for one object</xs:documentation>
    </xs:annotation>
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="v"/>
        <xs:element minOccurs="0" maxOccurs="unbounded" ref="label"/>
      </xs:sequence>
      <xs:attribute name="created" use="required" type="xs:dateTime">
        <xs:annotation>
          <xs:documentation>datetime of object creation</xs:documentation>
        </xs:annotation>
      </xs:attribute>
    </xs:complexType>
  </xs:element>
  <xs:element name="v" type="xs:string">
    <xs:annotation>
      <xs:documentation>Current version of the object from the repository
Occurance 1 is assumed by default</xs:documentation>
    </xs:annotation>
  </xs:element>
  <xs:element name="label" type="xs:string">
    <xs:annotation>
      <xs:documentation>Name of the object from the repository
Note: the occurance is denoted by the "*" and means 0 or more</xs:documentation>
    </xs:annotation>
  </xs:element>
</xs:schema>

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 and rnv 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:

$ jing -c schema.rnc doc.xml

The -c is important, jing by default assumes RELAX NG in XML form.

Using rnv to check, the schema.rnc itself is valid:

$ rnv -c schema.rnc

and to validate doc.xml:

$ rnv schema.rnc doc.xml

rnv allows validating multiple documents at once:

$ rnv schema.rnc doc.xml otherdoc.xml anotherone.xml

RELAX NG Compact syntax - pros

  • very readable, even newbie should understand the text
  • easy to learn (RELAX NG comes with good tutorial, one can learn most of it within one day)
  • very flexible (despite the fact, it looks simple, it covers many situation, some of them cannot be even resolved by XML Schema 1.0).
  • some tools for converting into other formats (RELAX NG XML form, XML Schema 1.0, DTD, but even generation of sample XML document) exists.

RELAX NG limitations

  • multiplicity can be only "zero or one", "just one", "zero or more" or "one or more". (Multiplicity of small number of elements can be described by "stupid repetition" of "zero or one" definitions)
  • There are XML Schema 1.0 constructs, which cannot be described by RELAX NG.

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.

当梦初醒 2024-08-19 00:22:56

您可以尝试通过创建 XSD 架构来记录它,该架构将提供更正式的 XML 规范。许多工具都会以示例 XML 为起点,为您生成 XSD。

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="objectroot">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="v" minOccurs="1" type="xs:string"/> <!-- current version -->
      <xs:element name="label" type="xs:string"/> <!-- object name -->
    </xs:sequence>
  </xs:complexType>
</xs:element>
</xs:schema>

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.

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="objectroot">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="v" minOccurs="1" type="xs:string"/> <!-- current version -->
      <xs:element name="label" type="xs:string"/> <!-- object name -->
    </xs:sequence>
  </xs:complexType>
</xs:element>
</xs:schema>
一页 2024-08-19 00:22:56

就我个人而言,我更喜欢在 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.

暮光沉寂 2024-08-19 00:22:56

在表中显示它有其局限性,例如多层嵌套子项,但对于简单的 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

我只土不豪 2024-08-19 00:22:56

我只是想再添加一件事,以防有人觉得有用。
我有时用 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.

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