大多数时候我们需要将 XML 转换为数组或 JSON,但现在我必须完成将 XML 转换为 XPath 的要求,这使得我们的模板可以通过 XPath 映射轻松地从 XML 数据源中获取数据。以下函数
<?php
function sxiToXpath($sxi, $key = null, &$tmp = null)
{
$keys_arr = array();
for ($sxi->rewind(); $sxi->valid(); $sxi->next())
{
$sk = $sxi->key();
if (array_key_exists($sk, $keys_arr))
{
$keys_arr[$sk]+=1;
$keys_arr[$sk] = $keys_arr[$sk];
}
else
{
$keys_arr[$sk] = 1;
}
}
for ($sxi->rewind(); $sxi->valid(); $sxi->next())
{
$sk = $sxi->key();
if (!isset($$sk))
{
$$sk = 1;
}
if ($keys_arr[$sk] >= 1)
{
$spk = $sk . '[' . $$sk . ']';
$keys_arr[$sk] = $keys_arr[$sk] - 1;
$$sk++;
}
else
{
$spk = $sk;
}
$kp = $key ? $key . '/' . $spk : '/' . $sxi->getName() . '/' . $spk;
if ($sxi->hasChildren())
{
sxiToXpath($sxi->getChildren(), $kp, $tmp);
}
else
{
$tmp[$kp] = strval($sxi->current());
}
$at = $sxi->current()->attributes();
if ($at)
{
$tmp_kp = $kp;
foreach ($at as $k => $v)
{
$kp .= '/@' . $k;
$tmp[$kp] = $v;
$kp = $tmp_kp;
}
}
}
return $tmp;
}
function xmlToXpath($xml)
{
$sxi = new SimpleXmlIterator($xml);
return sxiToXpath($sxi);
}
$xml = <<<EOT
<?xml version="1.0" encoding="utf8" ?>
<data>
<item ID="30001">
<Company>Navarro Corp.</Company>
</item>
<item ID="30002" IDd="30002">
<Company>Performant Systems</Company>
</item>
<item ID="30003">
<Company id='id_test'><g id='id_g'>glove</g></Company>
</item>
<item>
</item>
</data>
EOT;
$rs = xmlToXpath($xml);
print_r($rs);
echo "总数:" . count($rs);
echo "<hr>";
$xml = new SimpleXMLElement($xml);
foreach ($rs as $k => $v)
{
echo "XPath:" . $k . " |值:" . $v . " ";
var_dump($xml->xpath($k));
echo "<br>";
}