将模式值作为参数或参考动态传递

时间:2015-12-14 09:09:31

标签: xsd refs

我想用模式强制执行我的ID / IDRef。下面的代码工作得很好,但我想稍微优化一下,因为除了模式中的前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>

1 个答案:

答案 0 :(得分: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>

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