1

您好,我正在使用 MarkLogic 9 并尝试使用 XQuery 和 FLWOR 语句构建应用程序。我设置了http服务器。我在端口 8031 上测试了一个带有静态文本的简单页面,效果很好。我还在查询控制台中测试了一个 FLWOR 语句,它也可以正常工作。但是当我结合它时,它不起作用。

我希望你能帮助我。

曼尼谢谢

埃里克

花卉声明 ML 9.0

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

测试.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 应用程序页面 在此处输入图像描述

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

1 回答 1

4

您忘记提及有关您确实获得的结果的一些确切细节,但是查看您的代码,我猜,您的 HTML 页面确实显示了,但没有预期的行。这可能是由于命名空间。

您已将 FLWOR 语句嵌入到文字 XHTML 中。这通常很好,但是由于您的 XHTML 带有默认名称空间声明,因此包含在同一 XML 中的 XPath 表达式将使用同一名称空间声明进行解释。这意味着 XPath 表达式 like/scope/item实际上被解释为/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>

于 2017-09-24T18:10:57.857 回答