XPathを使い始めるだけで、PHPのSimpleXML
オブジェクトで実装されています。今私はを使用してSimpleXML
オブジェクトの配列を作成しています。zuq
という接頭辞が与えられたドキュメントにあります。しかし、私はSimpleXML
オブジェクトが名前空間に関係なくすべての子孫を参照するようにしたいと思います。私は//child::zuq:*
を使ってみましたが、作成するSimpleXML
ツリーは完全ではないようです。名前空間要素のすべての子のXPath文字列
キャプチャされるオブジェクトは、zuq
など、名前空間に関係なくすべての子孫要素を含む、ドキュメント全体のzuq
名前空間のすべての最上位オブジェクトである必要があります。
TL; DR:私は各SimpleXML
ルートオブジェクトは、指定された名前空間の最上位レベルの文書要素が与えられた文書からSimpleXML
オブジェクトツリーを作成するにはどうすればよい(例えばzuq
)かかわらず、前記要素のすべての子孫を含みます子孫の名前空間? XPathは必須条件ではありませんが、私の読書に基づいて最良の選択肢と思われます。
test.htmlという
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:zuq="http://localhost/zuq">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<h1>Heading</h1>
<p>Paragraph</p>
<zuq:region name="myRegion">
<div class="myClass">
<h1><zuq:data name="myDataHeading" /></h1>
<p>
<zuq:data name="myDataParagraph">
<zuq:format type="trim">
<zuq:param name="length" value="200" />
<zuq:param name="append">
<span class="paragraphTrimOverflow">...</span>
</zuq:param>
</zuq:format>
</zuq:data>
</p>
</div>
</zuq:region>
</body>
</html>
$sxml = simplexml_load_file('test.html');
$sxml_zuq = $sxml->xpath('//zuq:*/descendant-or-self::node()');
print_r($sxml_zuq);
が生成されます
Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => myRegion
)
[div] => SimpleXMLElement Object
(
[@attributes] => Array
(
[class] => myClass
)
[h1] => SimpleXMLElement Object //I don't know why these don't contain their zuq descendants
(
)
[p] => SimpleXMLElement Object
(
)
)
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => myRegion
)
[div] => SimpleXMLElement Object
(
[@attributes] => Array
(
[class] => myClass
)
[h1] => SimpleXMLElement Object
(
)
[p] => SimpleXMLElement Object
(
)
)
)
[2] => SimpleXMLElement Object
(
[@attributes] => Array
(
[class] => myClass
)
[h1] => SimpleXMLElement Object
(
)
[p] => SimpleXMLElement Object
(
)
)
[3] => SimpleXMLElement Object
(
[@attributes] => Array
(
[class] => myClass
)
[h1] => SimpleXMLElement Object
(
)
[p] => SimpleXMLElement Object
(
)
)
[4] => SimpleXMLElement Object
(
)
[5] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => myDataHeading
)
)
[6] => SimpleXMLElement Object
(
[@attributes] => Array
(
[class] => myClass
)
[h1] => SimpleXMLElement Object
(
)
[p] => SimpleXMLElement Object
(
)
)
[7] => SimpleXMLElement Object
(
)
[8] => SimpleXMLElement Object
(
)
[9] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => myDataParagraph
)
)
[10] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => myDataParagraph
)
)
[11] => SimpleXMLElement Object
(
[@attributes] => Array
(
[type] => trim
)
)
[12] => SimpleXMLElement Object
(
[@attributes] => Array
(
[type] => trim
)
)
[13] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => length
[value] => 200
)
)
[14] => SimpleXMLElement Object
(
[@attributes] => Array
(
[type] => trim
)
)
[15] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => append
)
[span] => ...
)
[16] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => append
)
[span] => ...
)
[17] => SimpleXMLElement Object
(
[@attributes] => Array
(
[class] => paragraphTrimOverflow
)
[0] => ...
)
[18] => SimpleXMLElement Object
(
[@attributes] => Array
(
[class] => paragraphTrimOverflow
)
[0] => ...
)
[19] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => append
)
[span] => ...
)
[20] => SimpleXMLElement Object
(
[@attributes] => Array
(
[type] => trim
)
)
[21] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => myDataParagraph
)
)
[22] => SimpleXMLElement Object
(
)
[23] => SimpleXMLElement Object
(
[@attributes] => Array
(
[class] => myClass
)
[h1] => SimpleXMLElement Object
(
)
[p] => SimpleXMLElement Object
(
)
)
[24] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => myRegion
)
[div] => SimpleXMLElement Object
(
[@attributes] => Array
(
[class] => myClass
)
[h1] => SimpleXMLElement Object
(
)
[p] => SimpleXMLElement Object
(
)
)
)
)
xmlを含めないようにしてください。そうしないと、すぐにこの質問に回答します。 – ajreal
@ajreal;ルルツ。良いことも、答えは過大評価されています:) – Dan