2016-05-09 31 views
0

的模式验证我有一个这样的XML实体,XHTML场

a:news-article xmlns:c="http://abc/core" xmlns:f="http://abc/fields" xmlns:a="http://abc/assets" xmlns:r="http://abc/refdata"> 
    <c:id>xyz</c:id> 
    <c:type>asset</c:type> 
    <c:created-on>2016-03-17T08:26:27.764Z</c:created-on> 
    <c:released-on>1985-11-03T00:00:00Z</c:released-on> 
    <c:expires-on>2009-12-12T00:00:00Z</c:expires-on> 
    <f:short-headline> 
    <c:content><c:l10n xml:lang="en"> 
    <p> 
     Carbide technology for South Korean project 
    </p> 
     </c:l10n></c:content> 
    <c:resources/> 
    </f:short-headline> 
</a:news-article> 

在这个XML,是一个XHTML领域。我需要使用模式验证来验证这样的XHTML字段。即如果我提供了空值,那么它应该抛出一个模式验证错误。

回答

1

对于每个名称空间,您需要单独的模式。

在XSD为“http://abc/core”命名空间,你可能需要一个图案以检查元素的含量非空:

<xs:element name="id"> 
      <xs:simpleType> 
       <xs:restriction base="xs:string"> 
        <xs:pattern value="\S+"/> 
       </xs:restriction> 
      </xs:simpleType> 
    </xs:element> 

然后您需要导入模式变成你的“根”模式(在您的示例我假定 “一个:” 前缀表示根模式)是这样的:

<xs:import namespace="http://abc/core" 

的schemaLocation = “core.xsd”/>

,最后引用该在正确的位置,从外部名称空间的元素:

<xs:element name="authors"> 
     <xs:complexType> 
      <xs:sequence>news-article 
      <xs:element ref="c:id"/> 
      <xs:element ref="c:type"/> 
      <!-- ... --> 
      </xs:sequence> 
     </xs:complexType> 
    </xs:element> 

如果你想确保p元素是非空的,你需要编写自己的模式,千篇一律如上 - 申报非 - p的空模式,并在c:l10n元素的模式中引用它。