我已经使用SO多年了,总能找到答案,但这一次我已经很好地完全迷失了。
我有一个xml文件我想将兼容性拆分为格式良好的xml
`<product>
<item>
<partno>abc123</partno>
<Compatbility>model1: 110C, 115C, 117C. model2: 1835C, 1840C. model3: 210C, 215C, 3240C.</Compatbility>
</item>
</product>`
在Compatbility中,单词模型随每个项目条目而变化,尽管:after模型总是在那里。在每个模特组之后。
我应该使用SimpleXml DomXml还是xpath来获得以下结果
`<product>
<item>
<partno>abc123</partno>
<Compatbility>
<model>model1: 110C, 115C, 117C.</model>
<model>model2: 1835C, 1840C.</model>
<model>model3: 210C, 215C, 3240C.</model>
</Compatbility>
</item>
</product>`
由于
答案 0 :(得分:2)
对于simplexml,您可以在元素的文本值上运行匹配的正则表达式。
然后,您可以删除所有内部文本,并将解析后的结果添加为新的子元素。
这可以用你所说的全部完成:DOMDocument,SimpleXMLElement - 有或没有xpath。
以下是SimpleXML(online demo)中的注释示例:
<?php
/**
* @link http://stackoverflow.com/q/24304095/367456
* @link https://eval.in/164934
*/
$buffer = <<<XML
<product>
<item>
<partno>abc123</partno>
<Compatbility>model1: 110C, 115C, 117C. model2: 1835C, 1840C. model3: 210C, 215C, 3240C.</Compatbility>
</item>
</product>
XML;
# load the xml string
$xml = simplexml_load_string($buffer);
# obtain the element in question
$compatbility = $xml->item->Compatbility;
# parse it's inner text-value for the models by a regex
$pattern = '~(model\\d?: [^.]+\\.) ?~u';
$result = preg_match_all($pattern, $compatbility, $matches);
# remove the text (so called simplexml self-reference)
$compatbility->{0} = '';
# add the parsed models as new model elements
foreach ($matches[1] as $model) {
$compatbility->model[] = $model;
}
# output the xml
$xml->asXML('php://output');
它给出的输出是:
<?xml version="1.0"?>
<product>
<item>
<partno>abc123</partno>
<Compatbility><model>model1: 110C, 115C, 117C.</model><model>model2: 1835C, 1840C.</model><model>model3: 210C, 215C, 3240C.</model></Compatbility>
</item>
</product>
答案 1 :(得分:0)
首先,你需要先将它转换成你可以操作的东西(数组)。然后通常解析(使用爆炸)。最后,您需要再次创建一个新的xml。考虑这个例子:
$xml_string = '<product><item><partno>abc123</partno><Compatbility>model1: 110C, 115C, 117C. model2: 1835C, 1840C. model3: 210C, 215C, 3240C.</Compatbility></item></product>';
$original_xml = simplexml_load_string($xml_string);
$data = json_decode(json_encode($original_xml), true);
$compatbility = $data['item']['Compatbility']; // get all compatibility values
// explode values
$compatbility = array_filter(array_map('trim', explode('.', $compatbility)));
$new_xml = new SimpleXMLElement('<product/>'); // initialize new xml
// add necessary values
$new_xml->addChild('item')->addChild('partno', $data['item']['partno']);
$new_xml->item->addChild('Compatbility');
// loop the values and add them as children
foreach($compatbility as $value) {
$value = trim(preg_replace('/(\w+):/', '', $value));
$new_xml->item->Compatbility->addChild('model', $value);
}
echo $new_xml->asXML(); // output as xml