views:

23

answers:

2

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.)

+1  A: 

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.

Adam Robinson
This is what I ended up doing. On the plus side, since I'm generating the class with XSD, if I forget the crucial edit step when they next change the schema, I'll at least get a build error.
John Rudy
A: 

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.

Cameron Laird