Xpath:选择节点但不选择特定的子元素 [英] Xpath: Select node but not specific child elements
问题描述
我有一个类似于以下的结构:
I have a structure similar to the following:
<page id='1'>
<title>Page 1</title>
<page id='2'>
<title>Sub Page 1</title>
</page>
<page id='3'>
<title>Sub Page 2</title>
</page>
</page>
<page id='4'>
<title>Page 2</title>
</page>
我需要按 Id 选择一个页面,但如果该页面有后代页面,我不想返回这些元素,但我确实想要该页面的其他元素.如果我选择第 1 页,我想返回标题而不是子页面...
I need to select a page by Id but if that page has descendant pages I don't want to return those elements, but I do want the other elements of that page. If I select Page 1 I want to return title but not the child pages...
//page[@id=1]
以上让我获得第 1 页,但如何排除子页面?此外,页面中可以有任意数量的元素.
The above gets me page 1, but how do I exclude the sub pages? Also, There could be any arbitrary number of elements in a page.
//page[@id=1]/*[not(self::page)]
我发现这可以为我提供我想要的数据.然而,该数据作为一个对象数组返回,每个元素一个对象,显然不包括元素名称???.我正在使用 PHP SimpleXML 来实现它的价值.
I have found that this gets me the data I want. However, that data comes back as an array of objects with one object per element and apparently excludes the element names???. I am using PHP SimpleXML for what it is worth.
推荐答案
使用:
//page[@id=$yourId]/node()[not(self::page)]
这将选择所有不是 page
并且是文档中任何 page
的子节点的节点,其 id
属性的字符串值等于 $yourId
中包含的字符串(很可能你会用一个特定的、所需的字符串替换上面的 $yourId
,例如 '1'
>).
This selects all nodes that are not page
and that are children of any page
in the document, the string value of whose id
attribute is equal to the string contained in $yourId
(most probably you would substitute $yourId
above with a specific, desired string, such as '1'
).
这是一个简单的基于 XSLT 的验证:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="pId" select="3"/>
<xsl:template match="/">
<xsl:copy-of select="//page[@id=$pId]/node()[not(self::page)]"/>
</xsl:template>
</xsl:stylesheet>
当此转换应用于提供的 XML 文档时(包装在单个顶部节点中以使其格式良好):
when this transformation is applied on the provided XML document (wrapped in a single top node to make it well-formed):
<pages>
<page id='1'>
<title>Page 1</title>
<page id='2'>
<title>Sub Page 1</title>
</page>
<page id='3'>
<title>Sub Page 2</title>
</page>
</page>
<page id='4'>
<title>Page 2</title>
</page>
</pages>
产生想要的、正确的结果:
<title>Sub Page 2</title>
请注意:一个假设是id
值唯一标识一个页面
.如果不是这样,建议的 XPath 表达式将选择 all page
元素,其 id
属性的字符串值为 $yourId代码>.
Do note: One assumption made is that an id
value uniquely identifies a page
. If this is not so, the proposed XPath expression will select all page
elements whose id
attribute has a string valu of $yourId
.
如果是这种情况并且必须只选择一个 page
元素,则 OP 必须指定具有此 id的多个
page
元素中的哪一个代码>应该被选中.
If this is the case and only one page
element must be selected, the OP must specify which one of the many page
elements with this id
should be selected.
例如可能是第一个:
(//page[@id=$yourId]/node()[not(self::page)])[1]
或最后一个:
(//page[@id=$yourId]/node()[not(self::page)])[last()]
或...
这篇关于Xpath:选择节点但不选择特定的子元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!