原文地址
http://www.lshift.net/blog/2008/01/31/some-simple-examples-of-using-erlangs-xpath-implementation
这篇文章很好的介绍了xmerl_xpath 这个很方便的东西, 而在官方的文档里, 一笔带过, 让人不识宝,我把它挖掘出来,有兴趣的同学折腾折腾...
We’ve been investigating the possibility of an XPath-based routing extension to RabbitMQ, where XPath would be used as binding patterns, and the message structure would be exposed as XML infoset. As part of this work, we’ve been looking at Erlang’s XPath implementation that comes as part of the built-in xmerl library.
Here are a couple of examples of Erlang’s XPath in action. First, let’s parse a document to be queried:
{ParsedDocumentRootElement, _RemainingText = ""} =
xmerl_scan:string("<foo>" ++
"<myelement myattribute=\"red\">x</myelement>" ++
"<myelement myattribute=\"blue\">x</myelement>" ++
"<myelement myattribute=\"blue\">y</myelement>" ++
"</foo>").
(We could have used xmerl_scan:file to read from an external file, instead of xmerl_scan:string, if we’d wanted to.)
Next, let’s retrieve the contents of every myelement node that contains text exactly matching “x”:
69> xmerl_xpath:string("//myelement[. = 'x']/text()”,
ParsedDocumentRootElement).
[#xmlText{parents = [{myelement,1},{foo,1}],
pos = 1,
language = [],
value = “x”,
type = text},
#xmlText{parents = [{myelement,2},{foo,1}],
pos = 1,
language = [],
value = “x”,
type = text}]
Notice that it’s returned two XML text nodes, and that the “parents” elements differ, corresponding to the different paths through the source document to the matching nodes.
Next, let’s search for all myelements that have a myattribute containing the string “red”:
72> xmerl_xpath:string("//myelement[@myattribute='red']“,
ParsedDocumentRootElement).
[#xmlElement{
name = myelement,
expanded_name = myelement,
nsinfo = [],
namespace = #xmlNamespace{default = [],nodes = []},
parents = [{foo,1}],
pos = 1,
attributes =
[#xmlAttribute{
name = myattribute,
expanded_name = [],
nsinfo = [],
namespace = [],
parents = [],
pos = 1,
language = [],
value = “red”,
normalized = false}],
content =
[#xmlText{
parents = [{myelement,1},{foo,1}],
pos = 1,
language = [],
value = “x”,
type = text}],
language = [],
xmlbase = “/localhome/tonyg”,
elementdef = undeclared}]
This time, there’s only the one match. Finally, a query that no nodes satisfy:
75> xmerl_xpath:string("//myelement[@myattribute='red' and . = 'y']“,
ParsedDocumentRootElement).
[]
If we had replaced the 'y' with 'x', we’d have retrieved a non-empty nodeset.
分享到:
相关推荐
"some Matlab examples of microphone array beamforming.zip"这个压缩包文件提供了若干个Matlab示例,旨在帮助用户理解并掌握麦克风阵列声束形成的原理与实现方法。 首先,我们来探讨一下麦克风阵列的基本概念。...
SOME EXAMPLES OF THE USE OF IEC 60870-5-101 PROTOCOL STANDARD
JSP Simple Examples Index 1. Creating a String In jsp we create a string as we does in a java. In jsp we can declare it inside the declaration directive or a scriptlet directive. String Length In ...
本资源“erlang_examples”提供了一个丰富的源代码集合,旨在全面展示Erlang的各种功能和特性。下面我们将深入探讨Erlang的关键概念及其在实际应用中的体现。 1. **函数式编程**: Erlang是纯函数式编程语言,这意味...
Gossip of Algorithm Examples
_H_RNN_HLS_An_LSTM_template_and_a_few_examples_using_Vivado_H_RNN_HLS
标题《erlang-examples-2.0.pdf》表明这是一份包含了Erlang语言示例程序的电子文档。Erlang是一种为构建并发和分布式系统设计的编程语言,它特别适用于需要高可靠性和高可用性的场景,例如电信、银行和即时通讯系统...
ZVON.org - XPath Tutorial Please, start with: General/examples.html (English) General_cze/examples.html (česky) General_dut/examples.html (Nederlands) General_fre/examples.html (Français) General_...
python,,QQ(Some interesting examples of python crawlers that are friendly to beginners. )
Model Predictive Control System Design and Implementation Using MATLAB(R) proposes methods for design and implementation of MPC systems using basis functions that confer the following advantages: - ...
Section 4 describes how to run each of the application examples. The software library that the code examples are built upon is described in section 5. The latter section also gives an API reference...
MatLabOctave examples of popular machine learning algorithms with code examples and mathematics being explained.zip
The book is organized to describe both simple and complex RTL design scenarios using VHDL. It gives practical information on the issues in ASIC prototyping using FPGAs, design challenges and how to ...
Experimental Matlab code for methods and some examples in Higher-order organization of complex networks.zip
Minimal examples of data structures and algorithms in Python
- **Sample Code**: The cookbook provides examples of how to implement basic and advanced audio and video functionality using these APIs. #### 9. **Using EventKitUI to Manage Calendars, Dates, and ...
PTB数据集simple-examples.zip 妾发初覆额,折花门前剧。 郎骑竹马来,绕床弄青梅。 同居长干里,两小无嫌猜, 十四为君妇,羞颜未尝开。 低头向暗壁,千唤不一回。 十五始展眉,愿同尘与灰。 常存抱柱信,岂上...