簡體   English   中英

XQuery 1.0無法打印出我期望的結果-錯誤地使用了FLWR

[英]XQuery 1.0 is not printing what I expect - Wrong use of FLWR

我想要一份沒有病患者的醫生名單。 (生病與至少一種指ValueOutcomeMinValMaxVal

我知道該值會打印兩次,因為醫生Stack Overflow進行了兩次考試。

此外,在$doc-ill中,只有DoctorExam Outcome超出范圍。 相反,我想做的是將$doc-ill放入所有Doctor中,且至少有一個Outcome超出范圍。

有人可以強調我做錯了嗎?

declare function local:outRange($x as element()) as xs:boolean{
  boolean( $x/Value < $x/MinVal or $x/Value > $x/MaxVal )
};

(: List of all doctors :)
let $alldoc := distinct-values(//Exam/Doctor)

(: List of doctors with at least 1 ill patient :)
    for $exam in //Exam
    let $doc-ill := $exam/Doctor
    where some $outc in $exam/Outcome satisfies local:outRange($outc)
    return (

(: List of doctors without hill patients :)
        let $ok := (
            for $doc in $alldoc
            return if( some $doc2 in $doc-ill satisfies $doc2 = $doc)
            then ()
            else( $doc )
        )
        return <HealthyDoctors> {
        $ok
        }</HealthyDoctors>
    )

XML的一個小例子是:

<Exam>
    <Outcome>
      <Value>90</Value>
      <MinVal>100</MinVal>
      <MaxVal>150</MaxVal>
    </Outcome>
    <Outcome>
      <Value>15</Value>
      <MinVal>1</MinVal>
      <MaxVal>20</MaxVal>
    </Outcome>
    <Outcome>
      <Value>1</Value>
      <MinVal>1</MinVal>
      <MaxVal>5</MaxVal>
    </Outcome>
    <Doctor>Stack Overflow</Doctor>
   </Exam>
  <Exam>
    <Outcome>
      <Value>190</Value>
      <MinVal>100</MinVal>
      <MaxVal>150</MaxVal>
    </Outcome>
    <Outcome>
      <Value>10</Value>
      <MinVal>1</MinVal>
      <MaxVal>5</MaxVal>
    </Outcome>
    <Doctor>Stack Overflow</Doctor>
   </Exam>
  <Exam>
    <Outcome>
      <Value>120</Value>
      <MinVal>100</MinVal>
      <MaxVal>150</MaxVal>
    </Outcome>
    <Outcome>
      <Value>4</Value>
      <MinVal>1</MinVal>
      <MaxVal>5</MaxVal>
    </Outcome>
    <Doctor>Health</Doctor>
   </Exam>
  <Exam>
    <Outcome>
      <Value>120</Value>
      <MinVal>100</MinVal>
      <MaxVal>150</MaxVal>
    </Outcome>
    <Outcome>
      <Value>10</Value>
      <MinVal>1</MinVal>
      <MaxVal>5</MaxVal>
    </Outcome>
    <Doctor>OneIll</Doctor>
   </Exam>

因此,我期望的是:

<HealthyDoctors>
<Doctor>Health</Doctor>
</HealthyDoctors>

我不確定什么是“病患者”,但是從您的代碼中猜測,我會說這是一個Outcome元素,其中值超出了最小值或最大值。

我不確定您的整個代碼,因為要實現的結果非常復雜。 首先,您嘗試讓一名山坡病人的所有醫生得到治療,然后您似乎想讓一組山坡病人的所有醫生得到差異,以得到實際結果。 但是實際上,簡單地讓所有醫生都沒有小山病患者,而無需所有重定向,要簡單得多:

declare function local:in-range($x as element(Outcome)) as xs:boolean{
  xs:int($x/Value) >= xs:int($x/MinVal) and xs:int($x/Value) <= xs:int($x/MaxVal)
};

<HealthyDoctors> {
  for $doc in distinct-values(//Exam/Doctor)
  where every $outcome in //Exam[Doctor = $doc]/Outcome satisfies local:in-range($outcome)
  return <Doctor>{ $doc }</Doctor>
}</HealthyDoctors>

因此,這段代碼基本上說給我每個醫生,他/她分配給每個醫生的Outcome要素都滿足所有范圍要求。 您的outRange函數不能正常工作是因為將值作為字符串進行了比較(即"9" > "10" ),因此您必須將它們顯式轉換為int。

另外,我將您的函數重命名為in-range ,因為XQuery約定不使用駝峰式大小寫,而是使用連字符。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM