开发者

Can I zero-pad an integer in an XSD-generated class?

Here's the situation: I have a client who's issued me an XML schema, and my software converts their records from tab-delimited to XML.

One of those fields, "file-sequence," is typed as an integer in the schema. However, the client's client (the integration target) wants that integer zero-padded and 4 digits long (EG, <file-sequence>0001</file-sequence>) in the actual output XML.

I'm outputting using serialization, which is nice and automatic and painless. However, because the field is typed as an integer in the XSD, the file-sequence field comes out as <file-sequence>1</file-sequence>. Which makes sense. :)

So far I've narrowed potential answers down to three options:

  1. Edit the schema and change the type to string. Zero-pad the sequence number upon updating the field. Downside: I have to remember to do this anytime the integration target changes the schema (as they have about a half-dozen times already).

  2. Forego using XML serialization, manually generating the XML in code. Downside: A lot of work, possibly error-prone, a serious code-smell (to me).

  3. Serialize to an in-memory stream, get the raw XML out of that, pad the integer there. Downsides: Feels really dirty, still a lot of work.

Are there any other options? If not, which option here is the right way to do this? (I'm thinking option 1 开发者_运维知识库is probably cleanest.)


Honestly, option 1 is the only real option. If the target cares about the number of characters in the string, then the field is not an integer; it's a string that's restricted only to numeric digits.


There are variations on what I understand you to mean by option 1 that might get you what you're after. My first reaction is to define the datatype of interest to be a constrained integer, specifically, one along the lines of

xsd:integer {minLength = '4' maxLength = '4'}

If that's unsatisfying, perhaps a regex

xsd:string {pattern = ...}

will help.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜