【问题标题】:Need to finding overlapping dates in XML code需要在 XML 代码中查找重叠日期
【发布时间】:2019-07-18 17:44:54
【问题描述】:

我需要帮助来查找此 XML 代码中的重叠日期。我需要确保结束日期不小于或等于进行中的开始日期。

       <Inventory>
          <StatusApplicationControl Start="2019-07-18" End="2019-07-18" InvTypeCode="STDX" />
          <InvCounts>
            <InvCount CountType="2" Count="9" />
          </InvCounts>
        </Inventory>
        <Inventory>
          <StatusApplicationControl Start="2019-07-18" End="2019-07-19" InvTypeCode="STDX" />
          <InvCounts>
            <InvCount CountType="2" Count="8" />
          </InvCounts>
        </Inventory>

我已经尝试了以下代码。

<rule context="Inventory">
            <report test="translate(StatusApplicationControl/@Start, '-', '') &lt;= translate(preceding::Inventory/preceding::StatusApplicationControl/@End, '-', '')">The @Start="<value-of select="@Start"/>" and @End="<value-of select="@End"/>" dates are overlaping</report>
</rule>

我希望打印此消息 - Start="2019-07-18" 小于或等于 End="2019-07-18" 日期

非常感谢任何帮助!

【问题讨论】:

  • 不清楚你的实际输出是什么。除此之外,您的消息似乎使用了同一上下文中的 StartEnd 属性。
  • 感谢 Alejandro,我更改了上面的代码,现在它可以工作了,但是我无法获取开始日期和结束日期的值。如果我弄清楚了,我会发布答案。
  • 如果您在比较StatusApplicationControl/@Startpreceding::Inventory/preceding::StatusApplicationControl/@End,为什么您的消息使用@Start@End
  • 因为这些值只能来自上下文。因此,要获取这些值,上下文必须是 context="Inventory/StatusApplicationControl"
  • 但是你想要“其他”@End 属性...

标签: xpath schematron


【解决方案1】:

看起来 cmets 没有帮助你。

规则应该是:

<rule context="Inventory">
   <report 
      test="translate(StatusApplicationControl/@Start, '-', '') 
            &lt;=translate(preceding::Inventory[1]/StatusApplicationControl/@End,'-','')"
   >The @Start="<value-of select="@Start"/>" and @End="<value-of 
    select="preceding::Inventory[1]/StatusApplicationControl/@End"
    />" dates are overlaping</report>
</rule>

编辑

这个 Schematron

<schema xmlns="http://purl.oclc.org/dsdl/schematron">
  <pattern>
    <title>Test dates</title>
    <rule context="Inventory">
      <assert 
      test="translate(StatusApplicationControl/@Start, '-', '') 
            > translate(preceding::Inventory[1]/StatusApplicationControl/@End,'-','')"
      >The @Start="<value-of 
       select="StatusApplicationControl/@Start"
       />" and @End="<value-of 
       select="preceding::Inventory[1]/StatusApplicationControl/@End"
       />" dates are overlaping</assert>
    </rule>
  </pattern>
</schema>

有了这个输入:

<root> 
    <Inventory>
          <StatusApplicationControl Start="2019-07-18" End="2019-07-18" InvTypeCode="STDX" />
          <InvCounts>
            <InvCount CountType="2" Count="9" />
          </InvCounts>
        </Inventory>
        <Inventory>
          <StatusApplicationControl Start="2019-07-18" End="2019-07-19" InvTypeCode="STDX" />
          <InvCounts>
            <InvCount CountType="2" Count="8" />
          </InvCounts>
        </Inventory>
  </root>

输出:

Pattern 'Test dates' Failed : The @Start="2019-07-18" and @End="2019-07-18" dates are overlaping.

签到https://www.liquid-technologies.com/online-schematron-validator

【讨论】:

  • 感谢 Alejandro,但是当我使用该规则时,我得到了 Schematron 异常。传递任何其他 @End 会导致异常。当从select 中删除preceding::Inventory[1]/StatusApplicationControl 时,它可以工作。
  • 嗨亚历杭德罗,上述规则仅适用于 2 个 Inventory 块我如何使其适用于更多块?谢谢!
  • 感谢 Alejandro,我想这是 Schematron 引擎的不同之处。我正在使用这个github.com/gap777/SchemaTron 而你的规则不适用于它。我以为所有的实现都是一样的,但我猜不是。再次感谢您的帮助!
  • 您的 Schematron 引擎使用 XPath 2.0。- 此表达式 translate(preceding::Inventory[1]/StatusApplicationControl/@End,'-','') 报告错误的唯一方式是因为每个 Inventory 都有许多 StatusApplicationControl,而您的输入样本没有显示。
  • 嗨 Alejandro,是的,我使用的是 XPath 2.0,但正如我的示例所示,我只有一个 StatusApplicationControl for each Inventory,所以不确定这是不是因为我使用了 .NET Schematon。
【解决方案2】:

这是对我有用的 SchemaTron 规则。问题是将preceding:: 传递给 translate() 函数。这样做时,我遇到了 SchemaTron 异常。

<rule context="Inventory/StatusApplicationControl">
            <report test="translate(@Start, '-', '') &lt;= preceding::StatusApplicationControl/translate(@End, '-', '')  ">The @Start="<value-of select="@Start"/>" and @End="<value-of select="@End"/>" dates are overlaping</report>
        </rule>

【讨论】:

  • 请注意这个表达式 preceding::StatusApplicationControl/translate(@End, '-', '') 不是 XPath 1.0 并且在 XPath 2.0 中它会导致与所有前面的(任何级别)@End 属性进行存在性比较。
  • 是的,我需要它来比较前面的所有值,但我永远不知道会有多少。 +1 指出这是 XPath 版本的区别!
猜你喜欢
  • 2017-11-04
  • 1970-01-01
  • 2011-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-21
  • 1970-01-01
  • 2022-12-10
相关资源
最近更新 更多