如何找出“数字”是什么?我指定的 xml 元素

发布于 2024-12-04 16:54:31 字数 773 浏览 1 评论 0原文

假设我有这样的 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 技术交流群。

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

发布评论

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

评论(3

救赎№ 2024-12-11 16:54:31

您可以计算从第一行到当前行发生 替换的所有次数:

:0,.s/<element>//gn

请注意,n 标志会阻止替换实际发生。

这显然不适用于所有可以想象的 XML 类型,但可能适合您的数据。

You could count all the times a substitute of <element> would occur from the first up to the current line:

:0,.s/<element>//gn

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.

偏爱自由 2024-12-11 16:54:31

要从当前行中提取数字,可以在 vim 中使用以下 ex 命令:

:echo substitute(getline('.'), '.*\(\d\+\).*', '\1', '')

To pull out the number from the current line you can use the following ex command in vim:

:echo substitute(getline('.'), '.*\(\d\+\).*', '\1', '')
故乡的云 2024-12-11 16:54:31

纯 XPath 解决方案

count(/*/element[. = 'some content 8']/preceding-sibling::*) +1

要轻松验证这一点,您可以使用 XPath Visualizer,或以下基于 XSLT 的验证:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">
     <xsl:value-of select=
     "count(/*/element[. = 'some content 8']/preceding-sibling::*) +1"/>
 </xsl:template>
</xsl:stylesheet>

在提供的 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>

结果评估 XPath 表达式是输出,并且它是正确的

8

A pure XPath solution:

count(/*/element[. = 'some content 8']/preceding-sibling::*) +1

To verify this easily you can use a tool like the XPath Visualizer, or the following XSLT-based verification:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">
     <xsl:value-of select=
     "count(/*/element[. = 'some content 8']/preceding-sibling::*) +1"/>
 </xsl:template>
</xsl:stylesheet>

When this transformation is performed on the provided XML document:

<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>

the result from evaluating the XPath expression is output, and it is correct:

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