如何在 Clojure 中的 xml 树上组合 zip 过滤器查询的结果?

发布于 2024-08-17 08:41:41 字数 1220 浏览 5 评论 0原文

我想在 xml 树上组合三个 zip 过滤器查询的结果。我正在解析的 XML 如下所示:

<someroot>
  <publication>
    <contributors>
      <person_name>
        <surname>Surname A</surname>
      </person_name>
      <person_name>
        <given_name>Given B</given_name>
        <surname>Surname B</surname>
        <suffix>Suffix B</suffix>
      </person_name>
    </contributors>
  </publication>
</someroot>

从这个示例中您可以看到 是可选的 - 只有 是可选的。 是必需的。这就是我的问题 - 如果我运行三个单独的查询,我得到的响应将彼此不平衡:

(xml-> xml :publication :contributors :person_name :given_name text)
(xml-> xml :publication :contributors :person_name :surname text)
(xml-> xml :publication :contributors :person_name :suffix text)

运行这三个查询后,我将留下三个基数不匹配的序列; given_namesuffix 的长度为 1,而 surname 的长度为 2。这使得我无法组合每个名称的组成部分。我需要编写一个查询来在序列构造期间执行此名称串联。

我正在查看 clojure.contrib.zip-filter.xml 的非常稀疏的文档,并且无法弄清楚如何做到这一点(或者是否可能)。不幸的是我是 Clojure (和 Lisp)新手!谁能指出我如何编写一个连接其他三个嵌入式查询的查询?

I want to combine the results of three zip-filter queries on an xml tree. The XML I am parsing looks like this:

<someroot>
  <publication>
    <contributors>
      <person_name>
        <surname>Surname A</surname>
      </person_name>
      <person_name>
        <given_name>Given B</given_name>
        <surname>Surname B</surname>
        <suffix>Suffix B</suffix>
      </person_name>
    </contributors>
  </publication>
</someroot>

From this example you can see that <given_name> and <suffix> are optional - only <surname> is required. Here in lies my problem - if I run three separate queries the responses I get will be out of kilter with each other:

(xml-> xml :publication :contributors :person_name :given_name text)
(xml-> xml :publication :contributors :person_name :surname text)
(xml-> xml :publication :contributors :person_name :suffix text)

After running these three queries I will be left with three sequences whose cardinalities do not match; given_name and suffix will be length 1 while surname will be length 2. This makes it impossible for me to combine the components of each name. I need to write a single query that will perform this name concatenation during sequence construction.

I'm looking at the very sparse documentation for clojure.contrib.zip-filter.xml and can't figure out how I could do this (or if it is even possible). Unfortunately I am a Clojure (and Lisp) newbie! Can anyone point out how I can write a query that will concatenate three other embedded queries?

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

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

发布评论

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

评论(2

孤独难免 2024-08-24 08:41:42

您可以一步获取所有人员子树 (xml-> xmlzip :publication :contributors :person_name),然后获取名称部分(如果存在)(xml1-> personzip :surname text) 并将它们组合成您想要的结果so:

(use 'clojure.contrib.zip-filter.xml)
(defn format-name
  [surname given suffix]
  (str surname (if given (str ", " given)) (if suffix (str ", " suffix))))
(defn get-names
  [xz] 
  (map (juxt 
         #(xml1-> % :surname text) 
         #(xml1-> % :given_name text) 
         #(xml1-> % :suffix text))
     (xml-> xz :publication :contributors :person_name)))
(let [x (clojure.zip/xml-zip (clojure.xml/parse "foo.xml"))] 
  (map (partial apply format-name) (get-names x)))

结果为(“姓氏A”“姓氏B,给定B,后缀B”)

You can get all the person subtrees in one step (xml-> xmlzip :publication :contributors :person_name) then get the name parts if they exist (xml1-> personzip :surname text) and combine them to the result you want like so:

(use 'clojure.contrib.zip-filter.xml)
(defn format-name
  [surname given suffix]
  (str surname (if given (str ", " given)) (if suffix (str ", " suffix))))
(defn get-names
  [xz] 
  (map (juxt 
         #(xml1-> % :surname text) 
         #(xml1-> % :given_name text) 
         #(xml1-> % :suffix text))
     (xml-> xz :publication :contributors :person_name)))
(let [x (clojure.zip/xml-zip (clojure.xml/parse "foo.xml"))] 
  (map (partial apply format-name) (get-names x)))

Results in ("Surname A" "Surname B, Given B, Suffix B")

说好的呢 2024-08-24 08:41:42

我想另一种解决方案是

(xml-> xml :publication :contributors :person_name)

稍后处理每个

I suppose an alternative solution is to

(xml-> xml :publication :contributors :person_name)

and then process each <person_name> later on.

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