質問

I'm playing around with some schematron rules embedded in an XSD file. The example is one of the canonical ones, and it works when there are no name spaces involved, but when I introduce a namespace it stops validating and I can't work out why.

The schema is simple:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
   targetNamespace="http://me.com/ns" xmlns:q="http://me.com/ns">
<xs:element name="socket">
    <xs:annotation>
        <xs:appinfo>
            <sch:pattern name="Mutually exclusive attributes on the socket element"
                xmlns:sch="http://purl.oclc.org/dsdl/schematron">
                <sch:rule context="socket" >
                    <sch:assert test="@hostName and @hostAddress">On a socket element only one
                        of the attributes hostName and hostAddress are allowed, not
                        both.</sch:assert>
                </sch:rule>
            </sch:pattern>
        </xs:appinfo>
    </xs:annotation>
    <xs:complexType>
        <xs:attribute name="hostName" type="xs:string" use="optional"/>
        <xs:attribute name="hostAddress" type="xs:string" use="optional"/>
    </xs:complexType>
</xs:element>
</xs:schema>

and the document being validated is:

<?xml version="1.0" encoding="UTF-8"?>
<socket xmlns="http://me.com/ns" hostAddress="192.168.200.76"/>

The schematron assertion is fired when the namespaces are removed, but as shown above they don't. I tried referencing the namespace in the context, <sch:rule context="q:socket">, but then I get compilation errors from the schematron pipeline.

Does anyone know off the top of their head how to fix this?

役に立ちましたか?

解決

This is an updated XSD that will work:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://me.com/ns" targetNamespace="http://me.com/ns" xmlns:sch="http://purl.oclc.org/dsdl/schematron">
    <xs:annotation>
        <xs:appinfo>
            <sch:ns uri="http://me.com/ns" prefix="q"/>         
        </xs:appinfo>
    </xs:annotation>
    <xs:element name="socket">
        <xs:annotation>
            <xs:appinfo>
                <sch:pattern name="Mutually exclusive attributes on the socket element" xmlns:sch="http://purl.oclc.org/dsdl/schematron">
                    <sch:rule context="q:socket">
                        <sch:assert test="@hostName and @hostAddress">On a socket element only one
                            of the attributes hostName and hostAddress are allowed, not
                            both.</sch:assert>
                    </sch:rule>
                </sch:pattern>
            </xs:appinfo>
        </xs:annotation>
        <xs:complexType>
            <xs:attribute name="hostName" type="xs:string" use="optional"/>
            <xs:attribute name="hostAddress" type="xs:string" use="optional"/>
        </xs:complexType>
    </xs:element>
</xs:schema>

Schematron requires namespace prefix declarations as indicated above.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top