 |
| XML General XML discussions. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

March 26th, 2004, 11:55 AM
|
|
Registered User
|
|
Join Date: Mar 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Regular expressions in XML schemas
Hello,
I am trying to validate an XML document using an XML schema. I have a piece of data that I am trying to validate by using a regular expression (patterned type). My data can be an empty string, but if it is present it must conform to the pattern a999999, the letter a followed by 6 digits. Can anybody tell me how to achieve this? I tried the code below, but it does not allow for an empty string.
<element name="agentid" nillable="true">
<simpleType>
<restriction base="string">
<pattern value="a\d{6}"/>
</restriction>
</simpleType>
</element>
Many thanks
Adrian
|
|

March 26th, 2004, 01:26 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
I think you can use the union operator:
value="(a\d{6})|()"
no guarantees as I'm unable to test this at the moment.
--
Joe
|
|

March 29th, 2004, 05:06 AM
|
|
Registered User
|
|
Join Date: Mar 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi Joe,
Thanks for your suggestion. Unfortunately it didn't work. My schema validator (xmlspy version 5 rel. 4) does not think that the pattern type is valid. The search continues...
Thanks
Adrian
|
|

March 29th, 2004, 05:47 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
Unfortunately Schemas don't suppot all the atndard patterns. I will have a look at my Schema bible (Definitive XML Schemas published by Prentice Hall).
--
Joe
|
|

March 29th, 2004, 05:55 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
Are you specifying the element to be empty in the instance by using xsi:nil="true"? It is not enough to be just empty.
--
Joe
|
|

March 29th, 2004, 06:48 AM
|
|
Registered User
|
|
Join Date: Mar 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Tried that. Still don't get the choice of empty or matching a999999.
Adrian
|
|

March 29th, 2004, 06:57 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
What parser and validator are you using?
--
Joe
|
|

March 29th, 2004, 09:49 AM
|
|
Registered User
|
|
Join Date: Mar 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I'm using XMLSPY version 5 release 4
Adrian
|
|

March 31st, 2004, 01:28 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
I don't have that but here is a test bed, six filkes all told, save in same directory and run ValidateXml. js:
Code:
<xsd:schema
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://tempuri.org/test.xsd"
targetNamespace="http://tempuri.org/test.xsd"
elementFormDefault="qualified">
<xsd:element name="agentid" nillable="true">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:pattern value="a\d{6}"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
</xsd:schema>
<agentid xmlns="http://tempuri.org/test.xsd"/>
<agentid xmlns="http://tempuri.org/test.xsd">a123456</agentid>
<agentid xmlns="http://tempuri.org/test.xsd">a123456</agentid>
<agentid xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/test.xsd" xsi:nil="true"/>
var sSchemaPath = "nillable.xsd";
var sTns = "http://tempuri.org/test.xsd";
var arrTestDocs = ["nillable1.xml", "nillable2.xml", "nillable3.xml", "nillable4.xml"];
function getAsyncDom()
{
var oDom = new ActiveXObject("Msxml2.DomDocument.4.0");
oDom.async = false;
return oDom
}
function getSchemaCache()
{
return new ActiveXObject("Msxml2.XMLSchemaCache.4.0");
}
function doValidation()
{
var oSC = getSchemaCache();
oSC.add(sTns, sSchemaPath);
var oDom = getAsyncDom();
oDom.validateOnParse = false;
for (var i = 0; i < arrTestDocs.length; i++)
{
oDom.load(arrTestDocs[i]);
oDom.schemas = oSC;
var oError = oDom.validate();
if (oError.errorCode == 0)
{
WScript.echo(arrTestDocs[i] + " passes");
}
else
{
WScript.echo(arrTestDocs[i] + " fails:\n" + oError.reason + "\n" + oError.srcText);
}
}
oDom = null;
oSC = null;
}
doValidation();
--
Joe
|
|

April 6th, 2004, 01:55 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
Can't you just use "(a\d{6})?" (including the question mark)?
Michael Kay
http://saxon.sf.net/
|
|
 |