断言条件以优化XSD中的查询

时间:2018-08-07 13:48:29

标签: xml xsd xml-parsing xsd-validation xsd-1.1

我有一个XSD,必须使用断言条件。我希望在indicator ='A'时打印所有列的条件,而在另一些条件是indicator ='D'时打印几列。我有以下逻辑,但是我有大约100列,所以有人可以帮助我优化查询吗?

<xs:assert test="if (indicator eq 'A') 
        then test1 and test2 and test3 and test4 and test5 and test6 and test7
        else if (indicator eq 'B') then test1 and test3 
        else false()"/>

输入XML的格式如下:

`<?xml version="1.0" encoding="utf-8"?>
<p:CustomerElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <recordCount>1234</recordCount>
  <Customer>
      <indicator>A</indicator>
      <test1>hdjfs</test1>
      <test2>idsfh</test2>
	  <test3>idsfh</test3>
	  <test4>idsfh</test4>
	  <test5>idsfh</test5>
	  <test6>idsfh</test6>
	  <test7>idsfh</test7>
	</Customer>
    <Customer>
      <indicator>B</indicator>
      <test1>abcd</test1>
      <test2></test2>
	  <test3>uydshjk</test3>
	  <test4></test4>
	  <test5></test5>
	  <test6></test6>
	  <test7></test7>
    </Customer>
</p:CustomerElement>

因此,正如我提到的,当A然后所有列都填充而B仅2列时。如果万一我写错了条件,请帮助我使用哪种条件。

确定指标的值仅是A或B。

谢谢。

1 个答案:

答案 0 :(得分:0)

为简化

<xs:assert test="if (indicator eq 'A') 
              then test1 and test2 and test3 and test4 and test5 and test6 and test7
              else if (indicator eq 'B') then test1 and test3 
              else false()"/>

鉴于此声明,

  

确定指标的值仅是A或B。

允许else涵盖B情况:

<xs:assert test="if (indicator eq 'A') 
              then test1 and test2 and test3 and test4 and test5 and test6 and test7
              else test1 and test3"/>

接下来,考虑到此声明[加重],

  

当A然后 all 列填充时,而B仅2列填充

避免枚举test1test7 all (尤其是您提到的情况,

  

我大约有100列

仅限制断言中test1test7的总数,

<xs:assert test="if (indicator eq 'A') 
                 then (count(*) = 8)
                 else test1 and test3"/>

如果您已经在parent的内容模型中明确声明了它们的存在,这将比枚举它们严格得多。

最后一步,您可以将if else重写为逻辑上等效的

<xs:assert test="   (indicator eq 'A' and count(*) = 8)
                 or (test1 and test3)"/>

但相对于其前身而言,这种形式的偏好主要取决于品味。

相关问题