Author |
Message
|
whydieanut |
Posted: Fri Aug 24, 2012 3:09 am Post subject: Validating XMLNSC messages |
|
|
 Disciple
Joined: 02 Apr 2010 Posts: 186
|
Sorry if this has been asked before, but I really couldn't come up with relevant search terms to use to search for existing threads.
I have an XML message which I'd like to validate using the a XMLNSC Message Set.
I am having trouble validating some data types.
Mandatory String fields:
I want to reject mandatory string fields with empty tags (<StrFld></StrFld>).
The parser seems to treat such fields as valid empty strings.
Is setting a Min length the only way to validate such fields?
Optional Date/Integer/Decimal fields:
Optional Date/Integer/Decimal fields fail validation if they are sent as empty tags (<Fld><Fld> as well as <Fl/> fail).
If I can't ask the sending system to completely supress the tag, what work around do I have?
Treating such fields as String fields and then casting then to respective types in code, if they are non empty; is this a good approach?
Sorry again if these basic questions have been asked earlier; just couldn't find any answers in my searches... |
|
Back to top |
|
 |
nathanw |
Posted: Fri Aug 24, 2012 3:14 am Post subject: |
|
|
 Knight
Joined: 14 Jul 2004 Posts: 550
|
what you mean is you want to reject mandatory string fields with no values
in the xml an empty field can have no value if the field is mandatory which we know
therefore you want to put a validation on the content of the field ie does it exist? if it does not that reject _________________ Who is General Failure and why is he reading my hard drive?
Artificial Intelligence stands no chance against Natural Stupidity.
Only the User Trace Speaks The Truth  |
|
Back to top |
|
 |
whydieanut |
Posted: Fri Aug 24, 2012 3:27 am Post subject: |
|
|
 Disciple
Joined: 02 Apr 2010 Posts: 186
|
nathanw wrote: |
what you mean is you want to reject mandatory string fields with no values |
Yes, an empty string has to be rejected.
nathanw wrote: |
therefore you want to put a validation on the content of the field ie does it exist? if it does not that reject |
Yes |
|
Back to top |
|
 |
kimbert |
Posted: Fri Aug 24, 2012 12:39 pm Post subject: |
|
|
 Jedi Council
Joined: 29 Jul 2003 Posts: 5542 Location: Southampton
|
Quote: |
I want to reject mandatory string fields with empty tags (<StrFld></StrFld>). |
Put a minLength facet on the simple type
Quote: |
Optional Date/Integer/Decimal fields fail validation if they are sent as empty tags (<Fld><Fld> as well as <Fl/> fail).
If I can't ask the sending system to completely supress the tag, what work around do I have? |
So you have an element or attribute which is of type xs:dateTime, but the sending application is sending an empty string. XML Schema can handle this, but it requires a little more work. Your value is not a valid xs:dateTime, and you do not want to make it an xs:string. You want it to be an xs:dateTime when it can be, and an (empty ) xs:string otherwise. So you need a simple type union
Quote: |
Sorry again if these basic questions have been asked earlier; just couldn't find any answers in my searches... |
No apology required - many XML Schema users get confused about these points. |
|
Back to top |
|
 |
fjb_saper |
Posted: Fri Aug 24, 2012 8:24 pm Post subject: Re: Validating XMLNSC messages |
|
|
 Grand High Poobah
Joined: 18 Nov 2003 Posts: 20756 Location: LI,NY
|
whydieanut wrote: |
Optional Date/Integer/Decimal fields:
Optional Date/Integer/Decimal fields fail validation if they are sent as empty tags (<Fld><Fld> as well as <Fl/> fail).
If I can't ask the sending system to completely supress the tag, what work around do I have?
Treating such fields as String fields and then casting then to respective types in code, if they are non empty; is this a good approach?
Sorry again if these basic questions have been asked earlier; just couldn't find any answers in my searches... |
How about just making these fields nillable?
So you would get
Code: |
<mydate xsi:nil="true"/> |
and the parser would be happy with it... maybe a little bit easier than the type union....  _________________ MQ & Broker admin |
|
Back to top |
|
 |
rekarm01 |
Posted: Fri Aug 24, 2012 11:42 pm Post subject: Re: Validating XMLNSC messages |
|
|
Grand Master
Joined: 25 Jun 2008 Posts: 1415
|
whydieanut wrote: |
Mandatory String fields:
I want to reject mandatory string fields with empty tags (<StrFld></StrFld>).
The parser seems to treat such fields as valid empty strings.
Is setting a Min length the only way to validate such fields? |
minLength is not the only constraining facet for invalidating empty strings, but it is the simplest one. The pattern facet is a bit much for just invalidating empty strings, but might be useful for more complex validation, (such as invalidating strings with only white space).
whydieanut wrote: |
Optional Date/Integer/Decimal fields:
Optional Date/Integer/Decimal fields fail validation if they are sent as empty tags (<Fld><Fld> as well as <Fl/> fail).
If I can't ask the sending system to completely supress the tag, what work around do I have?
Treating such fields as String fields and then casting then to respective types in code, if they are non empty; is this a good approach? |
Another workaround, which may or may not be suitable in this case, is to supply a default value for empty elements.
fjb_saper wrote: |
How about just making these fields nillable? So you would get
Code: |
<mydate xsi:nil="true"/> |
|
... assuming that the sending system is willing or able to do this. |
|
Back to top |
|
 |
whydieanut |
Posted: Sun Aug 26, 2012 11:42 pm Post subject: |
|
|
 Disciple
Joined: 02 Apr 2010 Posts: 186
|
kimbert wrote: |
Put a minLength facet on the simple type |
I suppose if there is no explicit minimum length for a field, I can just set it is 1?
rekarm01 wrote: |
... assuming that the sending system is willing or able to do this. |
Ya, I have about 10 different systems sending similar data; might be a problem.
kimbert wrote: |
So you have an element or attribute which is of type xs:dateTime, but the sending application is sending an empty string. XML Schema can handle this, but it requires a little more work. Your value is not a valid xs:dateTime, and you do not want to make it an xs:string. You want it to be an xs:dateTime when it can be, and an (empty ) xs:string otherwise. So you need a simple type union |
Looking at Simple Type Union right now...
How about I treat it as string initially and in code try to CAST it to Date/Integer/Decimal IF NOT EMPTY, and rely on the exception that will be thrown? |
|
Back to top |
|
 |
kimbert |
Posted: Mon Aug 27, 2012 8:55 am Post subject: |
|
|
 Jedi Council
Joined: 29 Jul 2003 Posts: 5542 Location: Southampton
|
Quote: |
I suppose if there is no explicit minimum length for a field, I can just set it is 1? |
Yes, probably. But you're guy who understands the data types, so ultimately it's your decision.
Don't forget this suggestion from rekarm01:
Quote: |
Another workaround, which may or may not be suitable in this case, is to supply a default value for empty elements. |
Default values are certainly easier to model than simple type unions.
Quote: |
How about I treat it as string initially and in code try to CAST it to Date/Integer/Decimal IF NOT EMPTY, and rely on the exception that will be thrown? |
Sure - that's possible. Personally I would not put that logic into a message flow if a nil or a default value was a viable alternative. |
|
Back to top |
|
 |
whydieanut |
Posted: Tue Aug 28, 2012 1:28 am Post subject: |
|
|
 Disciple
Joined: 02 Apr 2010 Posts: 186
|
kimbert wrote: |
Quote: |
I suppose if there is no explicit minimum length for a field, I can just set it is 1? |
Yes, probably. But you're guy who understands the data types, so ultimately it's your decision. |
Got it.
kimbert wrote: |
Don't forget this suggestion from rekarm01:
Quote: |
Another workaround, which may or may not be suitable in this case, is to supply a default value for empty elements. |
Default values are certainly easier to model than simple type unions. |
This isn't really an option as those fields are supposed to be empty in the destination system if not sent by the source system.
Is setting the default to NULL an option? I tried setting the default as NULL and it threw an error saying NULL wasn't of type:date
Quote: |
How about I treat it as string initially and in code try to CAST it to Date/Integer/Decimal IF NOT EMPTY, and rely on the exception that will be thrown? |
Sure - that's possible. Personally I would not put that logic into a message flow if a nil or a default value was a viable alternative.[/quote]
I don't see any other feasible solution. Might have to go with this approach. |
|
Back to top |
|
 |
whydieanut |
Posted: Tue Aug 28, 2012 1:30 am Post subject: |
|
|
 Disciple
Joined: 02 Apr 2010 Posts: 186
|
And simple type union seems a bit of an overkill really... |
|
Back to top |
|
 |
kimbert |
Posted: Tue Aug 28, 2012 4:31 am Post subject: |
|
|
 Jedi Council
Joined: 29 Jul 2003 Posts: 5542 Location: Southampton
|
Well...the sender is not playing the game, really. It cannot be an xs:dateTime AND be empty AND not have a default value. It doesn't make sense.
Quote: |
How about I treat it as string initially and in code try to CAST it to Date/Integer/Decimal IF NOT EMPTY, and rely on the exception that will be thrown? |
That is *exactly* what the XML processor will do for you, if you model the field as a simple type union. I'm just saying... |
|
Back to top |
|
 |
|