【问题标题】:Passing pattern value as parameter or reference dynamic将模式值作为参数或参考动态传递
【发布时间】:2015-12-14 09:09:30
【问题描述】:

我想使用一个模式来强制执行我的 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>

【问题讨论】:

  • 您可以使用 XSD 1.1 吗?
  • 这应该不是问题。 XSD 1.1 是否提供了可能性?
  • 可以使用断言来完成,但是这个解决方案并没有完全实现参数类型。

标签: xsd refs


【解决方案1】:

使用 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>

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

【讨论】:

    猜你喜欢
    • 2019-12-28
    • 2016-04-11
    • 2014-03-05
    • 1970-01-01
    • 2011-12-15
    • 1970-01-01
    • 2023-01-01
    • 1970-01-01
    • 2023-03-13
    相关资源
    最近更新 更多