2013-02-26 51 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>