Laravel中使用Orchesta/Parser解析XML多同名节点值的问题
Hey there! I’ve run into this exact issue before with Orchestra/Parser—by default, it only grabs the first matching node when you specify a simple path. Let’s get you all 10 of those <4> nodes loaded up properly.
The Problem
Your current configuration:
'images' => ['uses' => '1.2.3.4']
Tells the parser to fetch the first node matching the path 1.2.3.4, which is why you’re only seeing one value instead of all 10.
The Solution
You just need to add the multiple flag to your configuration array to tell the parser to return all matching nodes as an array:
'images' => [ 'uses' => '1.2.3.4', 'multiple' => true ]
How It Works
Orchestra/Parser’s extraction logic checks for the multiple key in your rule. When set to true, it switches from returning a single node value to collecting every node that matches your specified path into an array.
Alternative: Use an Explicit XPath Expression
If you prefer, you can also use an XPath expression that targets all matching nodes directly:
'images' => ['uses' => '//1/2/3/4']
The // syntax here tells XPath to find all <4> nodes under <3>, ensuring you capture every instance of the node regardless of context.
Testing It Out
After updating your configuration, when you run the parser, $extractedData['images'] should now be an array containing all 10 values from your <4> nodes instead of just the first one.
内容的提问来源于stack exchange,提问作者Erik180486

