2017-09-24 66 views
1

您好,我正在使用MarkLogic 9,並嘗試使用XQuery和FLWOR語句一起構建一個應用程序。我設置了http服務器。我用8031端口上的靜態文本測試了一個簡單的頁面,它工作正常。我還在查詢控制檯中測試了一個 FLWOR語句,該語句也正常工作。但是當我結合它時,它不起作用。MarkLogic 9中的FLWOR在chrome中不顯示結果

我希望你能幫助我。

曼尼感謝

埃裏克

FLOWER STATEMENT ML 9.0

for $i in /scope/item 
let $sscc := $i/transaction/sscc/text() 
return <tr><td>{$sscc}</td></tr> 

TEST.XQY

xquery version "1.0-ml"; 
xdmp:set-response-content-type("text/html; charset=utf-8"), 
'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">', 
<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <title>Find my orders</title> 
    </head> 
    <body> 
    <table> 
     <tr><th>SSCC</th></tr> 
     { 
     for $i in /scope/item 
     let $sscc := $i/transaction/sscc/text() 
     return <tr><td>{$sscc}</td></tr> 
     } 
    </table> 
    </body> 
</html> 

HTTP應用程序頁面 enter image description here

XML源文件

<scope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <item> 
     <transaction> 
      <type>CI</type> 
      <sscc>00000379471900000025</sscc> 
      <location>4260210630688</location> 
      <device>VISTALINK.004</device> 
      <date>2017-04-25</date> 
      <time>02:15:33</time> 
      <gmtOffset>+02:00</gmtOffset> 
      <actorId>155081</actorId> 
     </transaction> 
     <order> 
      <orderNumber>3794719</orderNumber> 
     </order> 
     <load> 
      <rti> 
       <ean>8714548186004</ean> 
       <grai>8003087145481860040019877322</grai> 
       <column>2</column> 
       <size> 
        <width>1900</width> 
        <height>95</height> 
        <depth>0</depth> 
       </size> 
       <position> 
        <x>2062,48707520218</x> 
        <y>2015,24337520512</y> 
        <z>0</z> 
       </position> 
      </rti> 
      <rti> 
       <ean>8714548106002</ean> 
       <grai>8003087145481060020016434653</grai> 
       <column>0</column> 
       <size> 
        <width>1900</width> 
        <height>95</height> 
        <depth>0</depth> 
       </size> 
       <position/> 
      </rti> 
      <rti> 
       <ean>8714548186004</ean> 
       <grai>8003087145481860040012803719</grai> 
       <column>2</column> 
       <size> 
        <width>1900</width> 
        <height>95</height> 
        <depth>0</depth> 
       </size> 
       <position> 
        <x>2064,20629390666</x> 
        <y>2124,57539157396</y> 
        <z>0</z> 
       </position> 
      </rti> 
      <rti>...</rti> 
      <rti>...</rti> 
      <rti>...</rti> 
      <rti>...</rti> 
      <rti>...</rti> 
     </load> 
    </item> 
</scope> 

回答

4

你忘了提及一下,結果你沒得到一些具體的細節,但是看着你的代碼,我猜,你的HTML頁顯示,但沒有預期的行。這可能是由於命名空間。

您已經在文字XHTML中嵌入了FLWOR語句。這通常很好,但是由於你的XHTML攜帶了默認的命名空間聲明,所以包含在同一個XML中的XPath表達式將被解釋爲具有相同的命名空間聲明。這意味着在您的情況下,像/scope/item這樣的XPath表達式實際上被解釋爲/xhtml:scope/xhtml:item

最簡單的方法是預先抓取項目,和/或使用通配符前綴。也許是這樣的:

let $items := fn:collection()/scope/item 
return 
    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
     <title>Find my orders</title> 
    </head> 
    <body> 
     <table> 
     <tr><th>SSCC</th></tr> 
     { 
      for $i in $items 
      let $sscc := $i/*:transaction/*:sscc/text() 
      return <tr><td>{$sscc}</td></tr> 
     } 
     </table> 
    </body> 
    </html> 

HTH!

+0

謝謝,這可以幫助我很多 –

+0

grtjn,你是如何處理嵌套與花的聲明? –

+0

SO上有例子:https://stackoverflow.com/q/11403946/918496 – grtjn