如何在 Clojure 中的 xml 树上组合 zip 过滤器查询的结果?
我想在 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_name
和 suffix
的长度为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以一步获取所有人员子树 (xml-> xmlzip :publication :contributors :person_name),然后获取名称部分(如果存在)(xml1-> personzip :surname text) 并将它们组合成您想要的结果so:
结果为(“姓氏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:
Results in ("Surname A" "Surname B, Given B, Suffix B")
我想另一种解决方案是
稍后处理每个
。I suppose an alternative solution is to
and then process each
<person_name>
later on.