Assert condition to optimize the query in XSD

2019-08-16 05:59发布

I have an XSD where i have to use assert condition. I would want to print all columns for condition when indicator='A' and few columns another condition is indicator='D'. I have the below logic but i have around 100 columns so can anyone help me with optimizing the query?

<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()"/>

The input XML is in this format:

`<?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>

So as i mentioned when A then all columns populate and when B only 2 columns. If in case i have written the condition wrong, please help me on using which condition to use.

The values for indicator is A or B only for sure.

Thanks.

1条回答
地球回转人心会变
2楼-- · 2019-08-16 06:28

To simplify

<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()"/>

given this statement,

The values for indicator is A or B only for sure.

allow the else to cover the B case:

<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"/>

Next, given this statement [emphasis added],

when A then all columns populate and when B only 2 columns

to avoid having to enumerate all of test1 through test7 (especially given that you mentioned,

i have around 100 columns

just constrain the total count of test1 through test7 in the assertion,

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

This will be no less strict than enumerating them if you've already declared their existence explicitly in the content model of parent.

As one last step, you might re-write if else to the logically equivalent,

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

but preference for this form over its predecessor is mostly a matter of taste.

查看更多
登录 后发表回答