xpath表达式中的属性和count()

编程入门 行业动态 更新时间:2024-10-25 00:28:42
本文介绍了xpath表达式中的属性和count()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

给定以下XML文件:

<a m="1"> <b n="1" o="2"> <c p="3">3</c> <d/> </b> <b n="1" o="2"> <c p="3">3</c> <d q="3"> <e r="2">2</e> </d> <f s="1"/> </b> </a>

如何找到以下表达式:

1. count(/*/*/*) = 5 2. count (/*//*) = 6 3. count (/*/*//@*) = 4

我在Java中使用这些xpath表达式运行了xml文件,但我不明白为什么答案是5,6 ,4。

I ran the xml file with those xpath expressions in Java , but I don't understand why the answers are 5,6,4 .

有人可以解释如何计算上面的表达式(不使用java代码),但通过理解命令的实际概念 / * / * / * 和 / * // * 和 / * / * // @ *

Can someone please explain how can I calculate the above expressions (not using a java code) but by understanding the actual concept of the commands /*/*/* and /*//* and /*/*//@* ?

非常感谢。

推荐答案

/*/*/*

这将选择所有顶级元素 - 这些是: c , d , c d

This selects all "grand-children of the top element -- these are: c, d, c, d

/*//*

这将选择顶层元素的所有后代元素: b , c , d , b , c , d , e , f

This selects all descendant elements of the top element: b, c, d, b, c, d, e, f

/*/*//@*

这将选择顶级元素或其后代的所有子元素: n , o , p , n , c>, p , q , r ,<$

This selects all attributes either of children of the top element or of their descendants: n, o, p, n, o, p, q, r, s.

因此,生成的计数必须分别为:

Therefore, the counts produced must be, respectively:

4, 8, 9

XSLT ::

XSLT - based verification:

<xsl:stylesheet version="1.0" xmlns:xsl="www.w3/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:template match="/"> <xsl:value-of select="count(/*/*/*)"/> ========= <xsl:value-of select="count(/*//*)"/> ========= <xsl:value-of select="count(/*/*//@*)"/> </xsl:template> </xsl:stylesheet>

对提供的XML文档执行此转换

<a m="1"> <b n="1" o="2"> <c p="3">3</c> <d/> </b> <b n="1" o="2"> <c p="3">3</c> <d q="3"> <e r="2">2</e> </d> </b> <f s="1"/> </a>

对Xpath表达式求值并将结果复制到输出:

4 ========= 8 ========= 9

更多推荐

xpath表达式中的属性和count()

本文发布于:2023-10-30 12:00:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1542788.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表达式   属性   xpath   count

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!