xpath表达式中的属性和count() [英] Attributes and count() in xpath expression
问题描述
给定以下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代码),但通过理解命令的实际概念 / * / * / *
和 / * // *
和 / * / * // @ * $ c $
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="http://www.w3.org/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()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!