Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
|
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
 
Old March 26th, 2004, 11:55 AM
Registered User
 
Join Date: Mar 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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
 
Old March 26th, 2004, 01:26 PM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

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
 
Old March 29th, 2004, 05:06 AM
Registered User
 
Join Date: Mar 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
 
Old March 29th, 2004, 05:47 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

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
 
Old March 29th, 2004, 05:55 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

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
 
Old March 29th, 2004, 06:48 AM
Registered User
 
Join Date: Mar 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Tried that. Still don't get the choice of empty or matching a999999.

Adrian
 
Old March 29th, 2004, 06:57 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

What parser and validator are you using?

--

Joe
 
Old March 29th, 2004, 09:49 AM
Registered User
 
Join Date: Mar 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'm using XMLSPY version 5 release 4

Adrian
 
Old March 31st, 2004, 01:28 PM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

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
 
Old April 6th, 2004, 01:55 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Can't you just use "(a\d{6})?" (including the question mark)?

Michael Kay
http://saxon.sf.net/





Similar Threads
Thread Thread Starter Forum Replies Last Post
Do I need regular expressions..? ypomonh XSLT 2 May 20th, 2007 05:09 PM
Help with Regular Expressions WestRowOps Other Programming Languages 1 May 18th, 2007 05:34 AM
XML Schema - Regular Expressions bonekrusher XML 6 March 13th, 2007 07:02 PM
Regular Expressions mega Beginning PHP 1 February 5th, 2007 05:31 PM
Regular Expressions The_Iceman ASP.NET 2.0 Basics 0 September 29th, 2006 05:06 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.