1

可以对元素的类型有唯一的约束吗?

假设我有一个诺亚方舟,其中 Animal/@name 必须是唯一的。

下面是一个不对模式进行验证的 XML:

<ns:NoahsArk xmlns:ns="http://www.xxx.com" xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" xs:schemaLocation="http://www.xxx.com Persons.xsd"> 
    <Animal xs:type="ns:Dog" ns:name="Gipsy" ns:pedigree="caniche"/>
    <Animal xs:type="ns:Spider" ns:name="Gipsy" ns:legNumber="5"/>
</ns:NoahsArk>


<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ns="http://www.xxx.com"
targetNamespace="http://www.xxx.com" elementFormDefault="unqualified" attributeFormDefault="qualified"> 
    <xs:element name="NoahsArk">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Animal" maxOccurs="unbounded" type="ns:Animal"/>
            </xs:sequence>
        </xs:complexType>
        <xs:unique name="NameUnicity">
            <xs:selector xpath="Animal"/>
            <xs:field xpath="@ns:name"/>
        </xs:unique>
    </xs:element>
    <xs:complexType name="Animal" abstract="true">
        <xs:attribute name="name" type="xs:string" use="required"/>
    </xs:complexType>
    <xs:complexType name="Dog">
        <xs:complexContent>
            <xs:extension base="ns:Animal">
                <xs:attribute name="pedigree" type="xs:string" use="required"/>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
    <xs:complexType name="Spider">
        <xs:complexContent>
            <xs:extension base="ns:Animal">
                <xs:attribute name="legNumber" type="xs:integer" use="required"/>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>    
</xs:schema>

这很好,但现在假设我想要一个诺亚方舟,其中 Animal/@xsi:type 是独一无二的。

我试过这个约束:

<xs:unique name="AnimalUnicity">
    <xs:selector xpath="Animal"/>
    <xs:field xpath="@xs:type"/>
</xs:unique>

但是这个 XML 仍然有效 :(

<?xml version="1.0" encoding="UTF-8"?>
<ns:NoahsArk xmlns:ns="http://www.xxx.com" xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" xs:schemaLocation="http://www.xxx.com Persons.xsd">
    <Animal xs:type="ns:Dog" ns:name="Pierre-Louis" ns:pedigree="doberman"/>
    <Animal xs:type="ns:Spider" ns:name="Gipsy" ns:legNumber="5"/>
</ns:NoahsArk>

有任何想法吗 ?

谢谢,-E

4

2 回答 2

0

All the implementations I could test on XSD 1.0 seem to agree on one thing here: the identity constraints will be tested only if the selectors/fields match XML nodes for which an associated user defined schema component can be found. xsi:type虽然它是与模式相关的标记,但由于其意图不同,因此此处不考虑。检查 Schematron 断言是否在这里有帮助可能在学术上很有趣……

更新:我也包括了 Schematron 版本;因为我在 .NET 上,所以我正在运行 ISO 版本、带有 Microsoft 扩展的 XSLT1。

<?xml version="1.0"?>
<sch:schema xmlns:sch="http://purl.oclc.org/dsdl/schematron" xmlns:ms="urn:schemas-microsoft-com:xslt">
    <sch:ns uri="http://www.w3.org/2001/XMLSchema-instance" prefix="xsi"/>
    <sch:ns uri="urn:schemas-microsoft-com:xslt" prefix="ms"/>

   <sch:pattern id="about-using-xsunique-on-xsitype-in-a-sequence">
      <sch:rule context="//Animal/@xsi:type">
        <sch:let name="targetNodeSet" value="//Animal/@xsi:type"/>
        <sch:assert test="count($targetNodeSet[concat('{', ms:namespace-uri(.), '}', ms:local-name(.)) = concat('{', ms:namespace-uri(current()), '}', ms:local-name(current()))]) = 1">
            Only one-of-a-kind animal is allowed.</sch:assert>
        <sch:assert test="count($targetNodeSet[. = current()]) = 1">
            Only one-of-a-kind animal is allowed (naive).</sch:assert>
      </sch:rule>
   </sch:pattern>
</sch:schema>

我定义了两个断言,一个“幼稚”的断言,它在文本上比较 xsi:type 属性的值;以及将它们比较为QName的“正确”。

对于这个 XML:

<ns:NoahsArk xmlns:ns1="http://www.xxx.com" xmlns:ns="http://www.xxx.com" xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" xs:schemaLocation="http://www.xxx.com Persons.xsd">  
    <Animal xs:type="ns:Dog" ns:name="A" ns:pedigree="caniche"/> 
    <Animal xs:type="ns1:Dog" ns:name="B" ns:pedigree="caniche"/> 
    <Animal xs:type="ns1:Dog" ns:name="C" ns:pedigree="caniche"/> 
</ns:NoahsArk> 

这是结果:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<svrl:schematron-output title="" schemaVersion="" xmlns:svrl="http://purl.oclc.org/dsdl/svrl" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:schold="http://www.ascc.net/xml/schematron" xmlns:sch="http://www.ascc.net/xml/schematron" xmlns:iso="http://purl.oclc.org/dsdl/schematron" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ms="urn:schemas-microsoft-com:xslt">
    <svrl:ns-prefix-in-attribute-values uri="http://www.w3.org/2001/XMLSchema-instance" prefix="xsi"/>
    <svrl:ns-prefix-in-attribute-values uri="urn:schemas-microsoft-com:xslt" prefix="ms"/>
    <svrl:active-pattern id="about-using-xsunique-on-xsitype-in-a-sequence" name="about-using-xsunique-on-xsitype-in-a-sequence"/>
    <svrl:fired-rule context="//Animal/@xsi:type"/>
    <svrl:failed-assert test="count($targetNodeSet[concat(', ms:namespace-uri(.), ', ms:local-name(.)) = concat(', ms:namespace-uri(current()), ', ms:local-name(current()))]) = 1" location="/@*[local-name()='type' and namespace-uri()='http://www.w3.org/2001/XMLSchema-instance']">
        <svrl:text>
            Only one-of-a-kind animal is allowed.</svrl:text>
    </svrl:failed-assert>
    <svrl:fired-rule context="//Animal/@xsi:type"/>
    <svrl:failed-assert test="count($targetNodeSet[concat(', ms:namespace-uri(.), ', ms:local-name(.)) = concat(', ms:namespace-uri(current()), ', ms:local-name(current()))]) = 1" location="/@*[local-name()='type' and namespace-uri()='http://www.w3.org/2001/XMLSchema-instance']">
        <svrl:text>
            Only one-of-a-kind animal is allowed.</svrl:text>
    </svrl:failed-assert>
    <svrl:failed-assert test="count($targetNodeSet[. = current()]) = 1" location="/@*[local-name()='type' and namespace-uri()='http://www.w3.org/2001/XMLSchema-instance']">
        <svrl:text>
            Only one-of-a-kind animal is allowed (naive).</svrl:text>
    </svrl:failed-assert>
    <svrl:fired-rule context="//Animal/@xsi:type"/>
    <svrl:failed-assert test="count($targetNodeSet[concat(', ms:namespace-uri(.), ', ms:local-name(.)) = concat(', ms:namespace-uri(current()), ', ms:local-name(current()))]) = 1" location="/@*[local-name()='type' and namespace-uri()='http://www.w3.org/2001/XMLSchema-instance']">
        <svrl:text>
            Only one-of-a-kind animal is allowed.</svrl:text>
    </svrl:failed-assert>
    <svrl:failed-assert test="count($targetNodeSet[. = current()]) = 1" location="/@*[local-name()='type' and namespace-uri()='http://www.w3.org/2001/XMLSchema-instance']">
        <svrl:text>
            Only one-of-a-kind animal is allowed (naive).</svrl:text>
    </svrl:failed-assert>
</svrl:schematron-output>

正如您从失败的断言中看到的那样,“天真的”方式仅标记 3 个实例中的 2 个;同样,将 xsi:type 属性的值作为文本进行比较与比较QName不同(这就是 @xsi:types 的含义)。

于 2012-10-09T16:01:38.013 回答
0

感谢您的答复。

只是为了好玩,我尝试使用 Schematron ;但是我无法让它工作,因为我发现的 XPath 表达式使用氧气或 XMLSpy 中不可用的“current()”,所以我无法测试它:(

    <?xml version="1.0" encoding="UTF-8"?>
    <schema xmlns="http://www.ascc.net/xml/schematron" xmlns:ns="http://www.xxx.com" >
        <ns prefix="ns" uri="http://www.xxx.com"/>
        <ns prefix="xs" uri="http://www.w3.org/2001/XMLSchema-instance"/>
            <pattern name="AnimalNameUnicity">
                <rule context="Animals/Animal">
                    <assert test="count(//@ns:name[ . = current()]) > 1">name not unique !</assert>        
                </rule>
            </pattern> 
    </schema>
于 2012-10-10T12:21:02.580 回答