gpt4 book ai didi

xsd - 将模式值作为参数或引用动态传递

转载 作者:行者123 更新时间:2023-12-02 06:59:27 25 4
gpt4 key购买 nike

我想使用模式强制执行我的 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,您可以使用断言来测试所需的正则表达式。这并不是真正的参数类型,因为它需要将正则表达式基于元素的名称。

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

<!-- 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>

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

关于xsd - 将模式值作为参数或引用动态传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34263127/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com