?
Solved

Enforcing ! NULL in an XML tag w/ an XSD

Posted on 2003-03-15
12
Medium Priority
?
517 Views
Last Modified: 2013-11-19
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
Comment
Question by:rdest5
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
12 Comments
 
LVL 9

Expert Comment

by:sparkplug
ID: 8142764
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
 
LVL 9

Expert Comment

by:sparkplug
ID: 8142892
Alternatively using regular expressions:

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

>S'Plug<
0
 

Author Comment

by:rdest5
ID: 8143655
hey sparkplug thanks for the input...

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

thanks
rdest5
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

Author Comment

by:rdest5
ID: 8143658
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
 
LVL 9

Expert Comment

by:sparkplug
ID: 8144178
<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
 
LVL 9

Expert Comment

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

>S'Plug<
0
 

Author Comment

by:rdest5
ID: 8170596
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
 
LVL 9

Expert Comment

by:sparkplug
ID: 8170652
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
 

Author Comment

by:rdest5
ID: 8170719
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
 
LVL 9

Accepted Solution

by:
sparkplug earned 60 total points
ID: 8172514
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
 

Author Comment

by:rdest5
ID: 8186265
Thanks Sparkplug.. i'll try it.. may be back with more questions....

rdest5
0
 
LVL 9

Expert Comment

by:sparkplug
ID: 8191563
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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question