using xpath in xsd pattern matching?

541 views Asked by At

Is it possible to use an attributes value as part of a pattern match?

eg 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>

so basically the pattern match in MyTestElement should include the value of the outer elements name attribute, is this possible?

1

There are 1 answers

0
sergioFC On

You can't use XPath in a xs:pattern. The content of xs:pattern should be a pattern (regular expression) as it is said in the specs.

However you can use XPath in xs:assert and others if you are using XSD 1.1, and some XPath subset inside xs:key, xs:keyRef and xs:unique if you are using XSD 1.0.

For example if you were using XSD 1.1 you could use an assertion like this to check that every <MyTestElement> matches a pattern or has the same value than the name attribute.

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