【问题标题】:XSD validation rule for assertion断言的 XSD 验证规则
【发布时间】:2018-01-09 17:54:48
【问题描述】:

我正在处理一个 XSD,我需要使用断言验证同一复杂类型下的 2 个元素。

有两个元素 OpenDate 和 CloseDate。我正在使用规则

<xs:assert test="if(  
    xs:date(concat(
            substring( OpenDate ,7 ,4 ) ,
            '-',
            substring( OpenDate ,1,2 ),
            '-', 
            substring( OpenDate ,4 ,2 ))  )
    &lt; xs:date(concat(
            substring( ClosedDate,7,4),
            '-',
            substring( ClosedDate,1,2),
            '-',
            substring( ClosedDate,4,2)))) 
    then true() 
    else false()" 
    saxon:message="close date cannot be a date before membership open date"/>

但这会在记录级别生成错误报告。 Xpath 显示为 Root/Record。它也不会显示错误的元素。我需要报告以显示 xpath 作为 Root/Record/OpenDate 和元素名称,而不仅仅是记录。

另外,您能否建议是否有一种方法可以在元素中使用 Assertion 来调用兄弟元素。喜欢

<xs:element name="OpenDate" 
            type="CMGDateFormat" 
            minOccurs="0" 
            maxOccurs="1">
  <xs:annotation>
    <xs:appinfo>
      <format>AAAAAAA</format>
      <originalName><![CDATA[MembershipOpenDate]]></originalName>
      <parent> </parent>
    </xs:appinfo>
  </xs:annotation>
</xs:element>
<xs:element name="MembershipClosedDate" 
            minOccurs="0" 
            maxOccurs="1">
  <xs:annotation>
    <xs:appinfo>
      <format>AAAAAAA</format>
      <originalName><![CDATA[MembershipClosedDate]]></originalName>
      <parent> </parent>
    </xs:appinfo>
  </xs:annotation>
  <xs:simpleType>
    <xs:restriction base="CMGDateFormat">
      <xs:assertion test="
        if(  xs:date(concat(  
               substring( $value,7 ,4 ) ,
               '-',
               substring( $value,1,2 ),
               '-', 
               substring( $value,4 ,2 ))  ) 
             &lt; 
             xs:date(concat(
               substring( Root/Record/Opendate,7,4),
               '-',
               substring( Root/Record/Opendate,1,2),
               '-',
               substring( Root/Record/Opendate,4,2)))) 
        then true() 
        else false()" 
      saxon:message="close date cannot be a date before membership open date"/>
    </xs:restriction>
  </xs:simpleType>
</xs:element>

关于如何实现上述目标的任何建议。我想使用断言来验证两个不同的元素。

【问题讨论】:

    标签: xml validation xpath xsd


    【解决方案1】:

    XSD 1.1 断言被设计为可在不参考上下文的情况下进行评估:当根据包含断言 A 的声明 D 验证元素 E 时,则(至少从概念上)将 E 复制到包含 E 而没有任何内容的新文档中否则,然后针对该临时文档的根元素(即 E 的副本)评估断言 A。 A 包含在原始上下文中引用 E 之外的元素的子表达式不是错误,但在为评估 A 而创建的上下文中,这些子表达式将评估为空序列。

    请注意,您的示例包含对Root/Record/Opendate 的引用,它在任何情况下都不表示兄弟元素。 (你的意思可能是../Opendate。)

    如果您的问题是您的错误消息没有提供足够的详细信息,您可能需要寻找方法使其提供更多信息。

    【讨论】:

      猜你喜欢
      • 2020-05-27
      • 2013-12-05
      • 2023-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多