共用方式為


規則運算式和結構描述物件模型

結構描述物件模型 (SOM) 允許完整存取由全球資訊網協會 (W3C) 在 XML Schema Part2: Datatypes Recommedation 中指定的規則運算式。在 XML 結構描述中,規則運算式是用來將值空間限制為符合特定規則運算式的值。

下列程式碼範例建立 XML 結構描述,它會定義只能包含字串的 SKU (存貨保持單元) 項目。接著會進一步限制這些字串,因為它們必須以三個數字開頭,後面跟著連字號,而且必須以兩個英文字母結尾。

Imports System.IO
Imports System
Imports System.Xml
Imports System.Xml.Schema

Class RegexSample
   
Public Shared Sub ValidationCallbackOne(sender As Object, args As ValidationEventArgs)
      Console.WriteLine(args.Message)
End Sub 'ValidationCallbackOne

   Public Shared Sub Main()
      
      Try
         
         Dim schema As New XmlSchema()
         
         ' <xs:element name="quantity">
         Dim skuElem As New XmlSchemaElement()
         skuElem.Name = "SKU"
         schema.Items.Add(skuElem)
         
         ' <xs:simpleType name="SKU">
         Dim SKUType As New XmlSchemaSimpleType()
         skuElem.SchemaType = SKUType
         
         ' <xs:restriction base="xs:string">
         Dim SKURestriction As New XmlSchemaSimpleTypeRestriction()
         SKURestriction.BaseTypeName = New XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema")
         SKUType.Content = SKURestriction
         
         '<xs:pattern value="\d{3}-[A-Z]{2}"/>
         Dim SKUpattern As New XmlSchemaPatternFacet()
         SKUpattern.Value = "\d{3}-[A-Z]{2}"
         SKURestriction.Facets.Add(SKUpattern)
         
         'Compile and print to the screen.
         schema.Compile(AddressOf ValidationCallbackOne)
         schema.Write(Console.Out)
      
      Catch e As Exception
         Console.WriteLine(e)
      End Try
   End Sub
End Class
[C#]
using System.IO; 
using System;
using System.Xml;  
using System.Xml.Schema;

class RegexSample {

public static void ValidationCallbackOne(object sender, ValidationEventArgs args) {
   Console.WriteLine(args.Message);
    }

    public static void Main() {
 
      try{ 

   XmlSchema schema = new XmlSchema(); 

   // <xs:element name="quantity">
   XmlSchemaElement skuElem = new XmlSchemaElement();
   skuElem.Name = "SKU"; 
   schema.Items.Add(skuElem); 

   // <xs:simpleType name="SKU">
   XmlSchemaSimpleType SKUType = new XmlSchemaSimpleType();
   skuElem.SchemaType = SKUType; 

   // <xs:restriction base="xs:string">
   XmlSchemaSimpleTypeRestriction SKURestriction = 
       new XmlSchemaSimpleTypeRestriction();
   SKURestriction.BaseTypeName = 
       new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
   SKUType.Content = SKURestriction;        

   // <xs:pattern value="\d{3}-[A-Z]{2}"/>
   XmlSchemaPatternFacet SKUpattern = new XmlSchemaPatternFacet();
   SKUpattern.Value = "\\d{3}-[A-Z]{2}";
   SKURestriction.Facets.Add(SKUpattern);

   // Compile and print to the screen.
   schema.Compile(new ValidationEventHandler(ValidationCallbackOne));
   schema.Write(Console.Out);
   
      }catch(Exception e){
   Console.WriteLine(e);
      }
    }
}

下列輸出的 XML 結構描述是由上面的程式碼範例產生的。

<?xml version="1.0" encoding="IBM437"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="SKU">
    <xs:simpleType>
      <xs:restriction base="xs:string">
        <xs:pattern value="\d{3}-[A-Z]{2}" />
      </xs:restriction>
    </xs:simpleType>
  </xs:element>
</xs:schema>

無效規則運算式模式限制

您可建立以無效方式限制型別的模式。

例如,dateTime 型別的格式為 CCYY-MM-DDThh:mm:ss,其中 CC 表示世紀,YY 表示年份,MM 表示月份,而 DD 表示日期,前面加上選擇性的前置負號 (-) 來表示負數。如果省略負號,就表示是正號 (+)。字母 T 是日期/時間分隔符號,而 hhmmss 分別表示小時、分鐘和秒。

下列 XML 結構描述範例嘗試限制 dateTime 型別來讓它看起來像 time 型別。

<xs:simpleType name="time">
      <xs:restriction base="xs:dateTime">
        <xs:pattern value="\d\d:\d\d(:\d\d)?" /> 
      </xs:restriction>
</xs:simpleType>

先前範例中的限制無效,因為它並不是建立 dateTime 型別的子集。相反地,它是建立新的不同型別,而所使用的一組可能值與 dateTime 型別的一組可能值並未重疊。

注意 如果您建立無效模式,處理器並不會發出警告,不過,它將無法驗證包含與限制型別基底不符的內容的所有執行個體文件。

如需規則運算式和 XML 結構描述的詳細資訊,請參閱<W3C XML Schema Recommendation>中<Datatypes>的附錄,位置是 http://www.w3.org/TR/xmlschema-2/\#regexs。

請參閱

XML 結構描述物件模型 (SOM)