2015-06-23 47 views
0

是否可以使用属性值作为模式匹配的一部分?在xsd模式匹配中使用xpath?

例如XSD:

<xs:element name="MyElement"> 
    <xs:complexType> 
     <xs:sequence> 
      <xs:element ref="SomeElement" minOccurs="0" maxOccurs="unbounded"/> 
      <xs:element name="MyTestElement"> 
      <xs:simpleType> 
       <xs:restriction base="xs:string"> 
        <xs:pattern value="(Attribute name value) some other regex matching"/> 
       </xs:restriction> 
      </xs:simpleType> 
      </xs:element> 
     </xs:sequence> 
     <xs:attribute name="name" type="xs:string" use="required"/> 
    </xs:complexType> 
</xs:element> 

所以基本上在MyTestElement模式匹配应该包括外元件名称属性的值,这可能吗?

回答

2

您不能在xs:pattern中使用XPath。 xs:pattern的内容应该是一个模式(正则表达式),如specs中所述。

但是你可以,如果你使用的是XSD 1.0使用xs:assert XPath和其他人,如果你正在使用XSD 1.1,以及一些使用XPath子集里面xs:keyxs:keyRefxs:unique

例如,如果您使用的是XSD 1.1,则可以使用类似这样的断言来检查每个<MyTestElement>是否匹配模式或具有与name属性相同的值。

<xs:assert test="every $MyTestElement in MyTestElement satisfies (string($MyTestElement) = string(@name) or matches(string($MyTestElement), 'pattern'))"/>