3

我想使用模式强制执行我的 ID/IDRefs。下面的代码完美运行,但我想对其进行一些优化,因为除了模式中的前 3 个字符之外,所有不同的类型都是相同的。是否可以有一个泛型类型,它将前缀(SEG,ITI,...)作为参数?

<xsd:complexType name="SegmentIDRefs">
    <xsd:complexContent>
        <xsd:restriction base="common:IDRefs">
            <xsd:attribute name="Id">
                <xsd:simpleType>
                    <xsd:restriction base="xsd:ID">
                        <xsd:pattern value="SEG_[\da-fA-F]{8}"/>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:attribute>
            <xsd:attribute name="GUID" type="common:external.GUID"/>
            <xsd:attribute name="RefId">
                <xsd:simpleType>
                    <xsd:restriction base="xsd:IDREF">
                        <xsd:pattern value="SEG_[\da-fA-F]{8}"/>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:attribute>
        </xsd:restriction>
    </xsd:complexContent>
</xsd:complexType>

<xsd:complexType name="ItineraryIDRefs">
    <xsd:complexContent>
        <xsd:restriction base="common:IDRefs">
            <xsd:attribute name="Id">
                <xsd:simpleType>
                    <xsd:restriction base="xsd:ID">
                        <xsd:pattern value="ITI_[\da-fA-F]{8}"/>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:attribute>
            <xsd:attribute name="GUID" type="common:external.GUID"/>
            <xsd:attribute name="RefId">
                <xsd:simpleType>
                    <xsd:restriction base="xsd:IDREF">
                        <xsd:pattern value="ITI_[\da-fA-F]{8}"/>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:attribute>
        </xsd:restriction>
    </xsd:complexContent>
</xsd:complexType>
4

1 回答 1

0

使用 XSD 1.1,您可以使用断言来测试所需的正则表达式。这不是真正的参数类型,因为它需要基于元素名称的正则表达式。

示例(我假设属性是必须简化的):

<!-- The ref type contains the attributes and an assertion to test the regex -->
<xsd:complexType name="myRefType">
    <xsd:attribute name="Id" type="xsd:ID" use="required"/>
    <xsd:attribute name="RefId" type="xsd:IDREF" use="required"/>
    <xsd:attribute name="GUID" type="xsd:string"/>

    <!-- Regex prefix is set based on node local name (it can be cahnged to use node first 3 letters if you want) -->
    <xsd:assert test="let $regex:=(
        concat(if (local-name()='itinerary') then 'ITI'
            else if (local-name()='segment') then 'SEG'
            else error(), '_[\da-fA-F]{8}')) 
        return matches(@Id, $regex) and matches(@RefId, $regex)"/>
</xsd:complexType>

<!-- Example root element containing an unbounded number of itinerary and semgent elements-->
<xsd:element name="root">
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element name="itinerary" type="common:myRefType" maxOccurs="unbounded"/>
            <xsd:element name="segment" type="common:myRefType" maxOccurs="unbounded"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:element>

这并不完美,但它是我能找到的最佳解决方案。

于 2015-12-17T20:43:32.563 回答