简体   繁体   English

将 DB 字段名称编码为 XML 标记的建议方法

[英]Suggested way to encode a DB field name to an XML tag

Let's say I have a database table with the following fields:假设我有一个包含以下字段的数据库表:

  • Age年龄
  • First Name
  • Last Name

Is there a suggested way on how to translate DB field names into XML tag names ?是否有关于如何将数据库字段名称转换为XML 标签名称的建议方法?

 [4] NameStartChar::= ":" | [AZ] | "_" | [az] | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF] | [#x370-#x37D] | [#x37F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF] \ [4a] NameChar::= NameStartChar | "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] | [#x203F-#x2040]```

I believe most DB sources have similar restrictions as XML does, though a space is usually allowed.我相信大多数数据库源都具有与 XML 类似的限制,尽管通常允许使用space Perhaps the easiest way would be:也许最简单的方法是:

<Table Name="Person">
    <Row>
        <Field name="Age">...</Field>
        <Field name="First Name">...</Field>
        <Field name="Last Name">...</Field>
    </Row>
</Table>

But I think that makes a mess out of things, and turns a semi-intuitive XML document into nothing more than an 'escape doc'.但我认为这会使事情变得一团糟,并将半直观的 XML 文档变成了“逃生文档”。 Are there any suggested guidelines on how to do this transform/conversion?是否有关于如何进行此转换/转换的建议指南? The idea that comes to mind for me (although not the 'safest') would be to slugify it according to the XML char spec:我想到的想法(尽管不是“最安全的”)是根据 XML 字符规范对其进行 slugify:

<Persons> <!-- pluralize? -->
    <Person>
        <Age>...</Age>
        <FirstName>...</FirstName>
        <LastName>...</LastName>
    </Person>
</Persons>

I suppose that works ok for asci-names.我想这适用于 asci 名称。

One convention that is sometimes used is to replace a disallowed character with _XX_ where XX is its Unicode code point in hex.有时使用的一种约定是用_XX_替换不允许的字符,其中 XX 是其十六进制的 Unicode 代码点。

Generating camelCase as in your example is another possible approach, giving more readable names, but of course there is no guarantee the resulting names will be unique.在您的示例中生成 camelCase 是另一种可能的方法,提供更易读的名称,但当然不能保证生成的名称是唯一的。

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

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