Warm tip: This article is reproduced from serverfault.com, please click

Suggested way to encode a DB field name to an XML tag

发布于 2020-11-30 00:37:37

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?

[4]       NameStartChar      ::=      ":" | [A-Z] | "_" | [a-z] | [#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. 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'. 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:

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

I suppose that works ok for asci-names.

Questioner
David542
Viewed
0
Michael Kay 2020-12-03 03:43:15

One convention that is sometimes used is to replace a disallowed character with _XX_ where XX is its Unicode code point in hex.

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.