Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 525
  • Last Modified:

Enforcing ! NULL in an XML tag w/ an XSD

Hello experts

I have the following question...

I have an element in my XML document that I want to enforce in my XSD... no big problem.. I just do a minOccurs of 1... more or less thats it... this guarentees that the element is required for the XML document to be considered valid.... my question though is this is all this requires (for the element tags to be there)... is there a way that I can enforce through the XSD that this already required element have a value within the element tags? i.e. right now as it stands say I have something like

<Person>
  <FirstName>Roger</FirstName>
  <LastName>Rabbit</LastName>
</Person>

then through the XSD I force that there must always be one LastName and one FirstName tag for each Person element... no a problem... but is there a way in the XSD to take this a step further... by saying not only is one and only one occurence of these elements(FirstName and LastName) required, but that they must contain a value... so that the following would fail the XSD validation...

<Person>
  <FirstName></FirstName>
  <LastName>   </LastName>
</Person>

Null is my more important priority to test for (as seen in FirstName element above) empty tags with "whitespace" is then a secondary concern as seen in the LastName element above...

Any help is greatly appreciated... Perhaps if you can you could provide just a simple example of how to do this... that is test for NULL values...

rdest5
0
rdest5
Asked:
rdest5
  • 7
  • 5
1 Solution
 
sparkplugCommented:
Hi,

This should do the trick:

<xs:simpleType name="stringnotnull">
    <xs:restriction base="xs:string">
        <xs:minLength value="1"/>
        <xs:whiteSpace value="collapse"/>
     </xs:restriction>
</xs:simpleType>

>S'Plug<

0
 
sparkplugCommented:
Alternatively using regular expressions:

<xs:simpleType name="stringnotnull">
   <xs:restriction base="xs:string">
       <xs:pattern value=".*\S+.*"/>
   </xs:restriction>
</xs:simpleType>

>S'Plug<
0
 
rdest5Author Commented:
hey sparkplug thanks for the input...

what does the line <xs:pattern value=".*\S+.*"/>
mean exactly???

thanks
rdest5
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
rdest5Author Commented:
alternatively what does <xs:minLength value="1"/>
       <xs:whiteSpace value="collapse"/>
exactly do/mean???

which one is better of your 2 posts..

thanks again
0
 
sparkplugCommented:
<xs:pattern/> is used to restict the string to a specific pattern of characters

. means any character
.* means 0 or more occurances of any character
\S means any non-space character
\S+ means 1 or more non-space characters

therefore .*\S+.* means 0 or more characters followed by 1 or more non-space characters followed by 0 or more characters.

For more information on patterns look here: http://msdn.microsoft.com/library/en-us/xmlsdk/htm/xsd_ref_5nck.asp 

<xs:minLength value="1"/> means that the string must be at least 1 character

<xs:whiteSpace value="collapse"/> removes leading and trailing spaces as well as double spaces from the string.

See http://msdn.microsoft.com/library/en-us/xmlsdk/htm/xsd_ref_5nck.asp for more information.

Both posts are equally as good. The difference being that the first method will cause the XML DOM to trim space and replace double spaces from the XML whereas the second method will leave the spaces intact.

>S'Plug<


0
 
sparkplugCommented:
btw if you find an answer useful you can give points by clicking on the Accept Answer link above...

>S'Plug<
0
 
rdest5Author Commented:
Spark plug sorry for the delay.. I have been away
in any event... I have tried implementing your solution in my xsd and it gives errors... my xsd is something like this now...

<xs:simpleType name="value">
  <xs:restriction base="xs:string">
      <xs:enumeration value="store-path"/>
  </xs:restriction>
</xs:simpleType>


I have tried implementing both of your suggestions with this, but have not been successful... am I missing something? The idea is for store-path to not have a null value...

Also is there a way to just a "blanket null test" for the whole XSD....??? In other words to make sure that no element in the XML file contains a null value???

rdest
0
 
sparkplugCommented:
Hi,

Not sure where the <xs:enumeration> came from? Both of my suggestions above work in my environment. What error message are you getting? Please post your full XSD and XML. This is turning into a more than 20 points question.

>S'Plug<
0
 
rdest5Author Commented:
Sparkplug don't worry about the points I will assign more... in any event heres the problem... I work at a secure sight and thus can not get the XSD and XML files out the door to post....  Sorry...

Try to work with me here if you can ;)

the enumeration came from me... I am trying to make sure that this is the only value allowed to be specified for the elemnt in question attribute...

that is why the enumeration type is there.. in any event I tried to wrap your stuff around this and error... something about enumeration or pattern (depending on where I put your code) is not or can not be an element of <xs:annotation> or something...

I am sorry this is so vague... have to do this from home and then go to work and try it.. memory fuzzy...

could you perhaps provide me with an example how to test for nulls, when there also has to be a limited/given set of allowable attribute values....

also what about a blanket is null test in the XSD?

rdest
0
 
sparkplugCommented:
Ok,

Say we have the following XML

<?xml version="1.0" encoding="UTF-8"?>
<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="C:\person.xsd">
     <Title>Mr</Title>
     <FirstName>Roger</FirstName>
     <LastName>Rabbit</LastName>
</Person>


Title, FirstName and LastName must not be an empty string, therefore we define the type 'stringnotempty' as follows:

<xs:simpleType name="stringnotempty">
          <xs:restriction base="xs:string">
                <xs:pattern value=".*\S+.*"/>
       </xs:restriction>
</xs:simpleType>

Then define the elements as follows:

<xs:element name="FirstName" type="stringnotempty"/>
<xs:element name="LastName" type="stringnotempty"/>


The Title is an enumeration and can be either 'Mr', 'Mrs', 'Ms' or 'Miss'. This also must not be an empty string. We therefore define a type 'titles' as follows:

<xs:simpleType name="titles">
          <xs:restriction base="stringnotempty">
               <xs:enumeration value="Mr"/>
               <xs:enumeration value="Mrs"/>
               <xs:enumeration value="Miss"/>
       </xs:restriction>
</xs:simpleType>

Note that this is based on the type 'stringnotempty'. This is not actually necessary as the enumeration allows only the value indicated and these values do not contain an empty string. I have done this however to show how it can be done. The Title is defined to be of this type with the following statement:

<xs:element name="Title" type="titles" />

Here is the full schema 'person.xsd':

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">

     <xs:simpleType name="'stringnotempty'">
            <xs:restriction base="xs:string">
                     <xs:pattern value=".*\S+.*"/>
           </xs:restriction>
     </xs:simpleType>

     <xs:simpleType name="titles">
            <xs:restriction base="'stringnotempty'">
                   <xs:enumeration value="Mr"/>
                   <xs:enumeration value="Mrs"/>
                   <xs:enumeration value="Miss"/>
           </xs:restriction>
     </xs:simpleType>

     <xs:element name="FirstName" type="'stringnotempty'"/>
     <xs:element name="LastName" type="'stringnotempty'"/>
     <xs:element name="Title" type="titles" />

     <xs:element name="Person">
          <xs:complexType>
               <xs:sequence>
                    <xs:element ref="Title"/>
                    <xs:element ref="FirstName"/>
                    <xs:element ref="LastName"/>
               </xs:sequence>
          </xs:complexType>
     </xs:element>
</xs:schema>



Note how the 'stringnotempty'type is defined in one place and used by several elements. This is the nearest we can get to applying a 'blanket null test'. If we were to add another element, say 'Job', which should not be empty we would simply define this as:


<xs:element name="Job" type="'stringnotempty'"/>


Hope this helps.

>S'Plug<
0
 
rdest5Author Commented:
Thanks Sparkplug.. i'll try it.. may be back with more questions....

rdest5
0
 
sparkplugCommented:
Grade B!! What happened to"...don't worry about the points I will assign more..."? Disappointed...

>S'Plug<
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now