1

我有这个xml:

<Item id="3" idLevel="3">
    <Label qualifier="Usual">
        <LabelText language="ALL">BE01</LabelText>
    </Label>
    <Label qualifier="Usual">
        <LabelText language="EN">R&#xc9;GION DE BRUXELLES-CAPITALE / BRUSSELS HOOFDSTEDELIJK GEWEST</LabelText>
    </Label>
</Item>
<Item id="4" idLevel="3">
    <Label qualifier="Usual">
        <LabelText language="ALL">BE001</LabelText>
    </Label>
    <Label qualifier="Usual">
        <LabelText language="EN">VLAAMS GEWEST</LabelText>
    </Label>
</Item>
<Item id="123" idLevel="3">
    <Label qualifier="Usual">
        <LabelText language="ALL">RO001</LabelText>
    </Label>
    <Label qualifier="Usual">
        <LabelText language="EN">MACROREGIUNEA DOI</LabelText>
    </Label>
</Item>

我想获取<LabelText language="EN">邻居<LabelText language="ALL">以“BE”开头并且后面有 3 个数字的值。

在这种情况下,我将在示例中获得第二个 xml 元素的值:VLAAMS GEWEST

我有一个想法如何以丑陋的方式处理它,但我相信应该有更灵活和优雅的方式来做到这一点:

$crawler = new Crawler();
$crawler->addXmlContent($xml);
$crawler = $crawler->filterXPath('//Item[@idLevel="3"]');

foreach ($crawler as $domElement) {
    // here I check if inside element's neighbour has value of "BE" and three numbers after with regex
}

有没有办法处理它DomCrawler而不是迭代所有元素并检查每个元素?

4

1 回答 1

2

您可以使用一个 XPath 表达式来获取您所需的文本:

//Item[@idLevel="3"]/Label[string-length(preceding-sibling::Label/LabelText/text()) = 5 and starts-with(preceding-sibling::Label/LabelText/text(), "BE") and number(substring(preceding-sibling::Label/LabelText/text(), 3)) = number(substring(preceding-sibling::Label/LabelText/text(), 3))]/LabelText[@language="EN"]/text()

分解它:

  • //Item[@idLevel="3"]- 获取具有属性值的Item节点idLevel3
  • /Label- 它的Label孩子有...
  • [string-length(preceding-sibling::Label/LabelText/text()) = 5Label/LabelText-文本长度等于 5的兄弟节点...
  • and starts-with(preceding-sibling::Label/LabelText/text(), "BE")- 并以文本开头BE
  • and number(substring(preceding-sibling::Label/LabelText/text(), 3)) = number(substring(preceding-sibling::Label/LabelText/text(), 3))]- 最后 3 个字符是数字
  • /LabelText[@language="EN"]/text()- 获取具有文本属性的LabelText节点的文本languageEN
于 2017-05-02T08:30:22.950 回答