How to create a new list item with FLOWR and XQuery?

我的梦境 提交于 2020-02-25 06:30:49

问题


I'm looking to select non-numerical data from an XML file towards shredding it into database columns, or at least an xmltable-like structure. This FLWOR gives a somewhat useful result:

xquery version "3.0";
declare namespace office="urn:oasis:names:tc:opendocument:xmlns:text:1.0";

<ul>
{
for $foo  in db:open("foo")
return <li>{$foo//text()[not(matches(., '[0-9]'))]}</li>

}
</ul>

However, it outputs all results into a single li tag, like:

  • a b c d

Preferred output would be of the form:

  • a
  • b

Most likely the data is problematic in some way, because a slightly different FLOWR:

xquery version "3.0";
declare namespace office="urn:oasis:names:tc:opendocument:xmlns:text:1.0";

for $foo in db:open("foo")
return $foo//text()[not(matches(., '[0-9]'))]

certainly outputs each non-numerical string on a new line. How can I output this into a list?

An excerpt of the data:

 <table:table-column table:style-name="co1" table:default-cell-style-name="ce17"/>
  <table:table-row table:style-name="ro1">
    <table:table-cell table:style-name="ce15" office:value-type="string" calcext:value-type="string">
      <text:p>John Smith</text:p>
    </table:table-cell>
  </table:table-row>
  <table:table-row table:style-name="ro2">
    <table:table-cell table:style-name="ce16" office:value-type="string" calcext:value-type="string">
      <text:p>(123) 456-7890</text:p>
    </table:table-cell>
  </table:table-row>
  <table:table-row table:style-name="ro2">
    <table:table-cell office:value-type="string" calcext:value-type="string">
      <text:p>123 Main Street</text:p>
    </table:table-cell>
  </table:table-row>
  <table:table-row table:style-name="ro2">
    <table:table-cell office:value-type="string" calcext:value-type="string">
      <text:p>Anywhere, ZZ 12345-6789</text:p>
    </table:table-cell>
  </table:table-row>
  <table:table-row table:style-name="ro1">
    <table:table-cell table:style-name="ce15" office:value-type="string" calcext:value-type="string">
      <text:p>Jane Doe</text:p>
    </table:table-cell>
  </table:table-row>
  <table:table-row table:style-name="ro2">
    <table:table-cell table:style-name="ce16" office:value-type="string" calcext:value-type="string">
      <text:p>(234) 567-8901</text:p>

回答1:


If you want an li for each of the text(), then alter what you are iterating over. Instead of selecting the text() inside of the for loop, iterate over each of the text():

xquery version "3.0";
declare namespace office="urn:oasis:names:tc:opendocument:xmlns:text:1.0";

<ul>
{
for $foo  in db:open("foo")//text()[not(matches(., '[0-9]'))]
return <li>{$foo}</li>
}
</ul>



回答2:


Only for completeness:

output:

  • people
  • joe
    • phone1
      phone2
      phone3
  • sue
    • cell4
      home5
  • alice
    • atrib6
      x7
      y9
      z10

query:

xquery version "3.0";
declare namespace office="urn:oasis:names:tc:opendocument:xmlns:text:1.0";

<ul>
{
for $line in db:open("people")//text()
return
      if (matches($line, "[0-9]"))
      then <ul>{$line}</ul>
      else <li>{$line}</li>
}
</ul>

Should this benefit anyone else.



来源:https://stackoverflow.com/questions/60229052/how-to-create-a-new-list-item-with-flowr-and-xquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!