2013-02-26 50 views
1

我有一個xsd文件,我需要引入一個新的驗證。我需要檢查城市代碼。城市代碼是68,這是一個始終相同的整數。xsd:如何檢查數字代碼

我該如何檢查?

謝謝!

這裏是我的代碼:

<xsd:element name="CodCity"> 
<xsd:annotation><xsd:documentation>City Code has to be 68.</xsd:documentation></xsd:annotation> 
<xsd:simpleType> 
<xsd:restriction base="xsd:int"></xsd:restriction> 
</xsd:simpleType> 
</xsd:element> 
+0

發佈您的示例輸入XML。 – 2013-02-26 12:29:11

回答

0

本來是更具體的,如果你發佈了輸入XML。

假設你不同的樣本輸入XML我張貼的答案:

樣品輸入XML:

<?xml version="1.0" encoding="utf-8"?> 
<testing>68</testing> 

XSD:

<?xml version="1.0" encoding="utf-8"?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:element name="testing" type="citycode" /> 
    <xs:simpleType name="citycode"> 
    <xs:restriction base="xs:int"> 
     <xs:pattern value="68"/> 
    </xs:restriction> 
    </xs:simpleType> 
</xs:schema> 

樣品輸入XML:

<?xml version="1.0" encoding="utf-8"?> 
<testing>Blah blah City code is: 68</testing> 

XSD [使用r egex圖案]:

<?xml version="1.0" encoding="utf-8"?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:element name="testing" type="pattern" /> 
    <xs:simpleType name="pattern"> 
    <xs:restriction base="xs:string"> 
     <xs:pattern value=".*68"/> 
    </xs:restriction> 
    </xs:simpleType> 
</xs:schema> 

其中.*是任意字符(除了換行),68 - 數68

一個多個樣品輸入XML:

<?xml version="1.0" encoding="utf-8"?> 
<testing>Blah blah City code is: '68' and something else</testing> 

XSD:

<?xml version="1.0" encoding="utf-8"?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:element name="testing" type="pattern" /> 
    <xs:simpleType name="pattern"> 
    <xs:restriction base="xs:string"> 
     <xs:pattern value=".*68.*"/> 
    </xs:restriction> 
    </xs:simpleType> 
</xs:schema>