Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XSD: Restrict a TextNode 1

Status
Not open for further replies.

SgtPeppa

Programmer
Jul 7, 2003
154
DE
Hi all,

this is a little weird scenario, but I need in one of my Schemas to restrict a ceratin Node.

It can hold any value other than "123". Is there any way I could do that in a Schema?

Any sites where I can read up on Restriction Patterns for Schemas?

Thanks,

Stephan
 
Code:
<xs:simpleType name="onetwothree">
  <xs:restriction base="xs:string">
    <xs:pattern value="123"/>
  </xs:restriction>
</xs:simpleType>
This is the basic syntax, but I'm not so great on regular expressions. "123" says the value must be "123", I'm not sure how to say it must not be "123". Maybe they'll know in a different (javascript maybe?) forum.
 
Generally, the "!" is used:

Code:
<xs:simpleType name="onetwothree">
  <xs:restriction base="xs:string">
    <xs:pattern value="!(123)"/>
  </xs:restriction>
</xs:simpleType>

Jon's right, about the javascript, although this'd probably have a slightly different syntax (more Perl like than javascript like). Read more here
 
I tried the !, but didn't work in XML spy (the value "!123" was valid).
 
Thanks for the reply, but this is actually not working!

Using your regex my Document does not validate no matter what kind of value I insert!

Any ideas?
 
Same result. value="!(123)" validates only when value of node is "!123
 
Sorry, not !, i think i meant ^ (which is also "beginning of line). I tried real hard at this one and the closest I got was this:

Code:
<?xml version="1.0"?>
<xs:schema xmlns:xs="[URL unfurl="true"]http://www.w3.org/2001/XMLSchema"[/URL]
targetNamespace="[URL unfurl="true"]http://www.w3schools.com"[/URL]
xmlns="[URL unfurl="true"]http://www.w3schools.com"[/URL]
elementFormDefault="qualified">
        <xs:element name="onetwothree">
            <xs:simpleType>
                <xs:restriction base="xs:string">
                    <xs:pattern value="[^123]?*" />
                </xs:restriction>
            </xs:simpleType>
        </xs:element>
  </xs:schema>

Trouble is, that this also matches 321, 213 AND 123. I've searched my arse off to find a "grouping" equivalent (in round brackets) but there doesn't seem to be one in regex.

matt
 
ack!! that regex should read

Code:
<xs:pattern value="[^123]+" />

i was fiddling with it when i copied it and posted that. :(

 
Excellent! That works! Thanks a million for all your help! I really appreciate it!

Stephan
 
It doesn't work. [^123]+ means "not one or more occurances of either 1, 2 or 3". So anything with a "1" or "2" or "3" in is invalid.
 
WOW,.... did not hit that scenario while testing, but you are right, as soon as I enter a 1, my File is invalid again!

Man, I did not expect that this 'simple' task would be such a bugger!

 
Yea, I know thats why I said <i>"Trouble is, that this also matches 321, 213 AND 123."</i> It also won't allow 1, 2 or 3 either, as Jon points out.

I searched high and low, and theirs singular regex i can find that will do a group () not matching.

(^123) doesn't work

^(123) matches 123 at the beginning of a line

[^(123)] doesnt work, grouping operations are not allowed in side []

[^1][^2][^3] only matches strings of three digits so abcd is not validated but abc is.

etc etc

However, I then tried doing some logical regular expressions as well and came up with this:

Code:
(?(?=\b123\b)[^123]+|[0-9a-zA-Z\s]+)

using the regex coach ( The expression basically states:

" if the expression matches 123, then dont match 1, 2 or 3 or match everything."

but, interestingly enough, although this is a valid expression in the coach, the Xerces parser doesn't like it
and throws this error: "This expression is not supported in the current option setting." There must be some settings that need adjusting somewhere.

HOWEVER! the .NET validator I posted before has NO PROBLEM WHATSOEVER with this regular expression and validates correctly.

So, give it a try and see.

Matt
 
Ok I tested

'123' - Validation Fails - Check
'123Test' - Validation passes - Check
'1Test' - Validation passes - Check

So it seems to work!

Thanks a lot again,

this should be absolutely perfect for my scenario!

Thanks,

Stephan

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top