如何找出“数字”是什么?我指定的 xml 元素
假设我有这样的 xml 结构
<root>
<element>some content</element>
<element>some content 2</element>
<element>some content 3</element>
<element>some content 4</element>
<element>some content 5</element>
<element>some content 6</element>
<element>some content 7</element>
<element>some content 8</element>
<element>some content 9</element>
<element>some content 10</element>
</root>
,我在根中有很多标签,假设有 1000 个,每个标签当然包含不同的数据(不是唯一的),我的问题是......最快的方法是什么?元素的数量,例如,如果我使用元素“some content 8”,我想得到数字 8,我知道 xpath 可能是正确的工具,但在我看来它非常慢(我想这样做几秒钟内,因为我使用经常)
也许一些“vim”方式如何做到这一点?
谢谢
Let's say I have xml structure like this
<root>
<element>some content</element>
<element>some content 2</element>
<element>some content 3</element>
<element>some content 4</element>
<element>some content 5</element>
<element>some content 6</element>
<element>some content 7</element>
<element>some content 8</element>
<element>some content 9</element>
<element>some content 10</element>
</root>
I have plenty of tags in the root, let's say 1000, and each of them contains different data of course (not unique), my problem is ... what is the fastest way how to find out the number of the element, for instance if I work with element "some content 8" I want to get number 8, I know that xpath is probably the right tool for that but it seems to me very slow (I want to do that like in few seconds, because I use it very often)
Maybe some "vim" way how to do this?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以计算从第一行到当前行发生
替换的所有次数:请注意,
n
标志会阻止替换实际发生。这显然不适用于所有可以想象的 XML 类型,但可能适合您的数据。
You could count all the times a substitute of
<element>
would occur from the first up to the current line:Note that the
n
flag prevents the substitute from actually occuring.This will obviously not work for all conceivable types of XML but might fit your data.
要从当前行中提取数字,可以在 vim 中使用以下 ex 命令:
To pull out the number from the current line you can use the following ex command in vim:
纯 XPath 解决方案:
要轻松验证这一点,您可以使用 XPath Visualizer,或以下基于 XSLT 的验证:
在提供的 XML 文档上执行此转换时:
结果评估 XPath 表达式是输出,并且它是正确的:
A pure XPath solution:
To verify this easily you can use a tool like the XPath Visualizer, or the following XSLT-based verification:
When this transformation is performed on the provided XML document:
the result from evaluating the XPath expression is output, and it is correct: