Skip to content

Commit 6caf689

Browse files
committed
Add toXhtml()
1 parent c9a2548 commit 6caf689

File tree

2 files changed

+39
-19
lines changed

2 files changed

+39
-19
lines changed

src/class-xml-file.php

+29-19
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,8 @@ function merge_update_required($scan, $persist)
164164
if (!$sysTime) return true;
165165
//print "\n<br/>xml_file::merge_update_required - sysTime=$sysTime, aPath=$this->aPath";
166166
//print_r($this->merge_list());
167-
foreach ($this->merge_list($scan) as $m)
168-
if (@filemtime($m) > $sysTime) return true;
167+
foreach ($this->merge_list($scan) as $accessor)
168+
if (@filemtime($accessor) > $sysTime) return true;
169169
return false;
170170
}
171171

@@ -180,9 +180,9 @@ function merge_join_to_xml($scan, $root, $item, $target, $persist)
180180
}
181181

182182
// print "\n<br/>xml_file:: merge_join_to_xml - list="; print_r($this->merge_list($scan));
183-
foreach ($this->merge_list($scan) as $m) {
184-
// print "\n<br/>xml_file::merge_join_to_xml - m=$m";
185-
$M = new xml_file($m);
183+
foreach ($this->merge_list($scan) as $accessor) {
184+
// print "\n<br/>xml_file::merge_join_to_xml - accessor=$accessor";
185+
$M = new xml_file($accessor);
186186
$n = 0;
187187
while (++$n > 0) { // Always. See break below.
188188
// print "\n<br/>>xml_file::merge_join_to_xml - cnt = " . $this->cnt("/$root/$item");
@@ -427,7 +427,7 @@ static function toDoc($k)
427427
return null;
428428
}
429429

430-
static function toXML($k)
430+
static function toXml($k)
431431
{
432432
if (is_string($k)) {
433433
if (file_exists($k)) return file_get_contents($k);
@@ -439,6 +439,16 @@ static function toXML($k)
439439
}
440440
return null;
441441
}
442+
443+
static function toXhtml($k)
444+
{
445+
$s = self::toXML($k);
446+
if (substr($s, 0, 6) == "<?xml " && false !== ($l = strpos($s, '?>')))
447+
$s = substr($s, $l + 2);
448+
449+
$s = trim($s);
450+
return $s;
451+
}
442452

443453
static function toJson($k) { return self::toXmlFile($k)->saveJson(); }
444454

@@ -547,14 +557,14 @@ static function qsplit($separator = ",", $string, $delim = "\"", $remove = true)
547557
}
548558

549559
// Extends XPaths correctly
550-
static function extend_path($b, $l, $m)
560+
static function extend_path($base, $field, $accessor)
551561
{
552-
if ($b == "") $b = '/';
553-
if ($b[strlen($b) - 1] != '/') $b = $b . "/";
554-
if ($m == '@') $m = $b . '@' . $l;
555-
else if ($m == '') $m = $b . $l;
556-
else if ($m[0] != '/') $m = $b . $m;
557-
return $m;
562+
if ($base == "") $base = '/';
563+
if ($base[strlen($base) - 1] != '/') $base = $base . "/";
564+
if ($accessor == '@') $accessor = $base . '@' . $field;
565+
else if ($accessor == '') $accessor = $base . $field;
566+
else if ($accessor[0] != '/') $accessor = $base . $accessor;
567+
return $accessor;
558568
}
559569
////////////////////////////////////////////////////////////////////////////////////////////////////////
560570
////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -675,18 +685,18 @@ private function CreateXMLNode($srcx, $value = "")
675685

676686
$s = "";
677687
$xsx = $this->xpathsplit($srcx);
678-
foreach ($xsx as $n => $m) {
688+
foreach ($xsx as $n => $accessor) {
679689
$pre_s = $s;
680-
if (!($m == "" && $s == "")) $s = "$s/$m";
690+
if (!($accessor == "" && $s == "")) $s = "$s/$accessor";
681691
if ($s == "") continue;
682692

683693
$en = $this->query($s);
684694
if ($en->length == 0) {
685-
if ($m[0] == '@') {
686-
$this->replace_attribute($parent, substr($m, 1), $value, false);
695+
if ($accessor[0] == '@') {
696+
$this->replace_attribute($parent, substr($accessor, 1), $value, false);
687697
} else {
688-
if (!$this->XPathAttribute($m, $a, $b, $c)) {
689-
$dd = $this->Doc->createElement($m);
698+
if (!$this->XPathAttribute($accessor, $a, $b, $c)) {
699+
$dd = $this->Doc->createElement($accessor);
690700
if ($n == count($xsx) - 1)
691701
$this->replace_content($dd, $value);
692702
$parent->appendChild($dd);

tests/converter-test.php

+10
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ public function testToXML(): void
4343
$this->assertEquals("string", gettype(xml_file::toXML(self::getDocEl())));
4444
}
4545

46+
public function testToXHTML(): void
47+
{
48+
$s = "<?xml version='1.0' standalone='yes' ?>\n<elements><item /></elements>";
49+
$e = "<elements><item/></elements>";
50+
$this->assertEquals($e, str_replace(" ", "", xml_file::toXHTML($s)));
51+
$this->assertEquals($e, str_replace(" ", "", xml_file::toXHTML(xml_file::toXML($s))));
52+
$this->assertEquals($e, str_replace(" ", "", xml_file::toXHTML(xml_file::toXmlFile($s))));
53+
$this->assertEquals($e, str_replace(array(" ", "\n"), "", xml_file::toXHTML(xml_file::toDoc($s))));
54+
}
55+
4656
public function testToDoc(): void
4757
{
4858
$this->assertEquals("DOMDocument", get_class(xml_file::toDoc(self::XML_TEXT)));

0 commit comments

Comments
 (0)