使用 xpath 选择第一个结果的孩子 [英] Selecting the children of first result using xpath
问题描述
我有一段 XML,其中相同的信息可以作为不同节点的子节点出现.如:
I have a piece of XML where the same information can appear as a child of different nodes. Such as :
<root>
<category id=1>
<product id="ABC123" >
<sizes>
<size name="S"/>
<size name="M"/>
<size name="L"/>
<size name="XL"/>
<size name="2XL"/>
<size name="3XL"/>
</sizes>
</product>
</products>
</category>
<category id=2>
<products>
<product id="ABC123" >
<sizes>
<size name="S"/>
<size name="M"/>
<size name="L"/>
<size name="XL"/>
<size name="2XL"/>
<size name="3XL"/>
</sizes>
</product>
<product id="PPP543" >
<sizes>
<size name="S"/>
<size name="M"/>
<size name="L"/>
<size name="XL"/>
</sizes>
</product>
</products>
</category>
我的目标是选择产品 ID ABC123 的大小并将它们存储为数组.我目前的代码是:
My goal is to select the sizes of product id ABC123 and store them as an array. The current code that I have is :
$arrTest=array();
foreach($xml->xpath('//root/category/products/product[@id= "'.$productCall.'" ]/sizes/size') as $size){
array_push($arrTest, $size["name"]);
}
$productCall 是我要查找的 ID.在这种情况下,它是 ABC123.
$productCall is the id I am looking for. In this case it's ABC123.
输出为 S,M,L,XL,2XL,3XL,S,M,L,XL,2XL,3XL.这意味着它正在读取找到的两个条目.鉴于 foreach 循环,我预计会出现这种情况,但我似乎无法找到一种方法来获取第一个结果的输出.我试过添加 [0] 和 [1] :
The output is S,M,L,XL,2XL,3XL,S,M,L,XL,2XL,3XL. Meaning that it is reading the two entries that were found. I expected this given the foreach loop, but I can't seem to find a way to just get the output of the first result. I have tried adding [0] and [1] :
$y=$xml->xpath('//root/category/products/product[@id= "'.$productCall.'" ][1]/sizes/size');
[0] 什么都不返回,而 [1] 返回的结果与我已经得到的结果相同.
[0] returns nothing and [1] returns the same results I'm already getting.
我希望这是一个简单的问题,因为我之前确实没有使用过 xpath.
I'm hoping this is a simple matter of me missing something something basic or just over-thinking, as I really haven't worked with xpath before.
推荐答案
使用:
(/*/*//product[@id='ABC123']/sizes)[1]/size
或使用:
((/*/*/product | /*/*/products/product)/sizes)[1]/size
基于 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:copy-of select=
"(/*/*//product[@id='ABC123']/sizes)[1]/size"/>
========
<xsl:copy-of select=
"((/*/*/product | /*/*/products/product)/sizes)[1]/size"/>
</xsl:template>
</xsl:stylesheet>
当此转换应用于提供的 XML 文档时(在将其更正为格式良好的文档后):
When this transformation is applied to the provided XML document (after correcting it into a well-formed one):
<root>
<category id="1">
<product id="ABC123" >
<sizes>
<size name="S"/>
<size name="M"/>
<size name="L"/>
<size name="XL"/>
<size name="2XL"/>
<size name="3XL"/>
</sizes>
</product>
</category>
<category id="2">
<products>
<product id="ABC123" >
<sizes>
<size name="S"/>
<size name="M"/>
<size name="L"/>
<size name="XL"/>
<size name="2XL"/>
<size name="3XL"/>
</sizes>
</product>
<product id="PPP543" >
<sizes>
<size name="S"/>
<size name="M"/>
<size name="L"/>
<size name="XL"/>
</sizes>
</product>
</products>
</category>
</root>
计算两个 XPath 表达式,并且从每个 XPath 表达式中选出的节点,在视觉上很好地分隔,被复制到输出——两者都是正确的:
<size name="S"/>
<size name="M"/>
<size name="L"/>
<size name="XL"/>
<size name="2XL"/>
<size name="3XL"/>
========
<size name="S"/>
<size name="M"/>
<size name="L"/>
<size name="XL"/>
<size name="2XL"/>
<size name="3XL"/>
请注意:我希望第二个 XPath 表达式更高效(在足够大的文档上),因为它不使用 //
伪运算符.
Do note: I expect the second XPath expression to be more efficient (on sufficiently large documents), because it doesn't use the //
pseudo-operator.
这篇关于使用 xpath 选择第一个结果的孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!