xquery 按字母顺序按组名创建联系人列表

发布于 2025-01-08 19:31:58 字数 1971 浏览 1 评论 0原文

我有一个带有条目元素的 xml。每个条目都有一个作者。我试图以这样的方式对条目进行分组,使得每个组都包含作者姓名以唯一字母字符开头的条目。所有作者姓名以字母“a”开头的条目都应该在一个组中,所有作者姓名以字母“b”开头的条目应该在另一组中,依此类推。

let $feeds := 
  <feed>
     <entry>
       <author>a</author>
       <title>Title a</title>
     </entry>
     <entry>
       <author>b</author>
       <title>Title ba</title>
     </entry>
     <entry>
       <author>ab</author>
       <title>Title ab</title>
     </entry>
     <entry>
       <author>bb</author>
       <title>Title bb</title>
     </entry>
   </feed>
for $entry in $feeds/entry
  let $author := $entry/author
  order by $author ascending
    return
      $entry

返回的结果:

<entry>
  <author>a</author>
  <title>Title a</title>
</entry>, <entry>
  <author>ab</author>
  <title>Title ab</title>
</entry>, <entry>
  <author>b</author>
  <title>Title ba</title>
</entry>, <entry>
  <author>bb</author>
  <title>Title bb</title>
</entry>

我正在尝试调整代码,返回的结果看起来像这样:

<group>
<author-group>
<entry>
  <author>a</author>
  <title>Title a</title>
</entry>, <entry>
  <author>ab</author>
  <title>Title ab</title>
</entry>
</author-group>
<author-group>
<entry>
  <author>b</author>
  <title>Title ba</title>
</entry>, <entry>
  <author>bb</author>
  <title>Title bb</title>
</entry>
</author-group>
<group>

在我的示例中,预计有两个作者组 - 第一个作者组由姓名以 a 开头的所有作者组成,第二个作者组由姓名以 b 开头的所有作者组成

我想忽略作者姓名中的大小写,即 aa 和 Aa 是视为相同。

然后我会为每个作者组创建一个联系人列表。

谢谢, 索尼

I have an xml with entry elements. Each entry has an author. I am trying to group the entries in such a manner such that each group consists of entries whose author name starts with a unique alphabetic character. All entries whose author names start with letter 'a' should be in one group, all entries whose authors whose names start with letter 'b' should be in another group, and so on..

let $feeds := 
  <feed>
     <entry>
       <author>a</author>
       <title>Title a</title>
     </entry>
     <entry>
       <author>b</author>
       <title>Title ba</title>
     </entry>
     <entry>
       <author>ab</author>
       <title>Title ab</title>
     </entry>
     <entry>
       <author>bb</author>
       <title>Title bb</title>
     </entry>
   </feed>
for $entry in $feeds/entry
  let $author := $entry/author
  order by $author ascending
    return
      $entry

Returned Result:

<entry>
  <author>a</author>
  <title>Title a</title>
</entry>, <entry>
  <author>ab</author>
  <title>Title ab</title>
</entry>, <entry>
  <author>b</author>
  <title>Title ba</title>
</entry>, <entry>
  <author>bb</author>
  <title>Title bb</title>
</entry>

I am trying to tweak around the code such that the returned result looks something like so:

<group>
<author-group>
<entry>
  <author>a</author>
  <title>Title a</title>
</entry>, <entry>
  <author>ab</author>
  <title>Title ab</title>
</entry>
</author-group>
<author-group>
<entry>
  <author>b</author>
  <title>Title ba</title>
</entry>, <entry>
  <author>bb</author>
  <title>Title bb</title>
</entry>
</author-group>
<group>

In my example, two author groups are expected - First author group consisting of all authors whose names start with a, second author group consisting of all authors whose names start with b

I would like to ignore cases in author names i.e. aa and Aa are treated as same.

I would then take each author group and create a contact list.

Thanks,
Sony

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷月断魂刀 2025-01-15 19:31:58

此 XQuery 转换

 <groups> 
 {
 let $entries := /*/entry,

  $vals := $entries/author/lower-case(substring(.,1,1))
      return
         for $fst in  $vals[index-of($vals, .)[1]]
           order by $fst 
             return
              <author-group>
                  {$entries[$fst eq lower-case(substring(author,1,1))]}
              </author-group>
   }
 </groups>

应用于提供的 XML 文档时

<feed>
  <entry>
    <author>a</author>
    <title>Title a</title>
  </entry>
  <entry>
    <author>b</author>
    <title>Title ba</title>
  </entry>
  <entry>
    <author>ab</author>
    <title>Title ab</title>
  </entry>
  <entry>
    <author>bb</author>
    <title>Title bb</title>
  </entry>
</feed>

产生所需的正确结果

<groups>
   <author-group>
      <entry>
         <author>a</author>
         <title>Title a</title>
      </entry>
      <entry>
         <author>ab</author>
         <title>Title ab</title>
      </entry>
   </author-group>
   <author-group>
      <entry>
         <author>b</author>
         <title>Title ba</title>
      </entry>
      <entry>
         <author>bb</author>
         <title>Title bb</title>
      </entry>
   </author-group>
</groups>

This XQuery transformation:

 <groups> 
 {
 let $entries := /*/entry,

  $vals := $entries/author/lower-case(substring(.,1,1))
      return
         for $fst in  $vals[index-of($vals, .)[1]]
           order by $fst 
             return
              <author-group>
                  {$entries[$fst eq lower-case(substring(author,1,1))]}
              </author-group>
   }
 </groups>

when applied on the provided XML document:

<feed>
  <entry>
    <author>a</author>
    <title>Title a</title>
  </entry>
  <entry>
    <author>b</author>
    <title>Title ba</title>
  </entry>
  <entry>
    <author>ab</author>
    <title>Title ab</title>
  </entry>
  <entry>
    <author>bb</author>
    <title>Title bb</title>
  </entry>
</feed>

produces the wanted, correct result:

<groups>
   <author-group>
      <entry>
         <author>a</author>
         <title>Title a</title>
      </entry>
      <entry>
         <author>ab</author>
         <title>Title ab</title>
      </entry>
   </author-group>
   <author-group>
      <entry>
         <author>b</author>
         <title>Title ba</title>
      </entry>
      <entry>
         <author>bb</author>
         <title>Title bb</title>
      </entry>
   </author-group>
</groups>
雨夜星沙 2025-01-15 19:31:58

这是 XQuery 1.0 解决方案:

let $feeds :=  
  <feed> 
     <entry> 
       <author>a</author> 
       <title>Title a</title> 
     </entry> 
     <entry> 
       <author>b</author> 
       <title>Title ba</title> 
     </entry> 
     <entry> 
       <author>Ab</author> 
       <title>Title ab</title> 
     </entry> 
     <entry> 
       <author>Bb</author> 
       <title>Title bb</title> 
     </entry> 
   </feed>
return
<group>{
  for $initial in distinct-values($feeds/entry/author/lower-case(substring(.,1,1)))
        order by $initial
  return
    <author-group>{
      for $entry in $feeds/entry[author/lower-case(substring(.,1,1)) eq $initial]
      let $author := $entry/author 
      order by $author ascending
      return
        $entry
    }</author-group>
}</group>

HTH

Here an XQuery 1.0 solution:

let $feeds :=  
  <feed> 
     <entry> 
       <author>a</author> 
       <title>Title a</title> 
     </entry> 
     <entry> 
       <author>b</author> 
       <title>Title ba</title> 
     </entry> 
     <entry> 
       <author>Ab</author> 
       <title>Title ab</title> 
     </entry> 
     <entry> 
       <author>Bb</author> 
       <title>Title bb</title> 
     </entry> 
   </feed>
return
<group>{
  for $initial in distinct-values($feeds/entry/author/lower-case(substring(.,1,1)))
        order by $initial
  return
    <author-group>{
      for $entry in $feeds/entry[author/lower-case(substring(.,1,1)) eq $initial]
      let $author := $entry/author 
      order by $author ascending
      return
        $entry
    }</author-group>
}</group>

HTH

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文