Skip to content

Commit 9c45567

Browse files
author
Owen Leibman
committed
Eliminate XML Source Manipulation
Original solution required XML manipulation to overcome what appears to be an xpath problem. This version replaces xpath with iteration, eliminating the need to manipulate the XML.
1 parent 8a2357f commit 9c45567

File tree

1 file changed

+23
-41
lines changed

1 file changed

+23
-41
lines changed

src/PhpSpreadsheet/Reader/Xlsx.php

+23-41
Original file line numberDiff line numberDiff line change
@@ -132,40 +132,6 @@ private function loadZip(string $filename, string $ns = ''): SimpleXMLElement
132132
return self::testSimpleXml($rels);
133133
}
134134

135-
private function loadStyleZip(string $filename, string $ns = ''): SimpleXMLElement
136-
{
137-
// With the following:
138-
// <x:styleSheet xmlns:x="whatever"...
139-
// simplexml_load_file specifying namespace works fine,
140-
// but xpath on the result does not. I can't figure out
141-
// how to make xpath work in this circumstance, but I can
142-
// manipulate the xml to the far more usual:
143-
// <stylesheet xmlns="whatever"...
144-
// Ugly, but arguably serviceable.
145-
$xml = $this->getFromZipArchive($this->zip, $filename);
146-
$xmlns = " xmlns=\"$ns\"";
147-
if (strpos($xml, $xmlns) === false) {
148-
$pattern = "~ xmlns:([A-Za-z0-9_]+)=\"$ns\"~";
149-
if (preg_match($pattern, $xml, $matches) === 1) {
150-
$pattern = "~ xmlns:${matches[1]}=~";
151-
$repl = Worksheet::pregReplace($pattern, ' xmlns=', $xml);
152-
$pattern = "~<(/?)${matches[1]}:~";
153-
$repl = Worksheet::pregReplace($pattern, '<$1', $repl);
154-
if ($repl !== '') {
155-
$xml = $repl;
156-
}
157-
}
158-
}
159-
$rels = simplexml_load_string(
160-
$this->securityScanner->scan($xml),
161-
'SimpleXMLElement',
162-
0,
163-
$ns
164-
);
165-
166-
return self::testSimpleXml($rels);
167-
}
168-
169135
// This function is just to identify cases where I'm not sure
170136
// why empty namespace is required.
171137
private function loadZipNonamespace(string $filename, string $ns): SimpleXMLElement
@@ -572,15 +538,31 @@ public function load(string $filename, int $flags = 0): Spreadsheet
572538
if ($xpath === null) {
573539
$xmlStyles = self::testSimpleXml(null);
574540
} else {
575-
$xmlStyles = $this->loadStyleZip("$dir/$xpath[Target]", $mainNS);
541+
$xmlStyles = $this->loadZip("$dir/$xpath[Target]", $mainNS);
576542
}
577543

578-
$xmlStyles->registerXPathNamespace('smm', $mainNS);
579-
$fills = self::xpathNoFalse($xmlStyles, 'smm:fills/smm:fill');
580-
$fonts = self::xpathNoFalse($xmlStyles, 'smm:fonts/smm:font');
581-
$borders = self::xpathNoFalse($xmlStyles, 'smm:borders/smm:border');
582-
$xfTags = self::xpathNoFalse($xmlStyles, 'smm:cellXfs/smm:xf');
583-
$cellXfTags = self::xpathNoFalse($xmlStyles, 'smm:cellStyleXfs/smm:xf');
544+
$fills = [];
545+
$fonts = [];
546+
$borders = [];
547+
$xfTags = [];
548+
$cellXfTags = [];
549+
if (count($xmlStyles) > 0) {
550+
foreach ($xmlStyles->fills->fill as $fill) {
551+
$fills[] = $fill;
552+
}
553+
foreach ($xmlStyles->fonts->font as $font) {
554+
$fonts[] = $font;
555+
}
556+
foreach ($xmlStyles->borders->border as $border) {
557+
$borders[] = $border;
558+
}
559+
foreach ($xmlStyles->cellXfs->xf as $xf) {
560+
$xfTags[] = $xf;
561+
}
562+
foreach ($xmlStyles->cellStyleXfs->xf as $xf) {
563+
$cellXfTags[] = $xf;
564+
}
565+
}
584566

585567
$styles = [];
586568
$cellStyles = [];

0 commit comments

Comments
 (0)