php - Getting Unexpected xpath results -
here source xml: xml
this xml of fxg file produced adobe. fxg document valid xml, , contains information document can edited. specific question pertains text can changed within fxg content can change.
i'm tryng grab richtext elements , attributes within element have attribute of s7:elementid
using xpath relative location.
the source xml has 3 total richtext
elements, 2 of them having s7:elementid
<?php $url = "http://testvipd7.scene7.com/is/agm/papermusepress/hol_12_f_green?&fmt=fxgraw"; $xml = simplexml_load_file($url); $xml->registerxpathnamespace('default', 'http://ns.adobe.com/fxg/2008'); $xml->registerxpathnamespace('s7', 'http://ns.adobe.com/s7fxg/2008'); $textnode = $xml->xpath("//default:richtext/@s7:elementid"); print("<pre>".print_r($textnode,true)."</pre>"); ?>
i got far question. returned array not expecting. setting xpath did, expecting select richtext
elements have s7:elementid
, other attributes of element. however, isn't grabbing of other attributes of elements. here outputs:
array ( [0] => simplexmlelement object ( [@attributes] => array ( [elementid] => smalltext ) ) [1] => simplexmlelement object ( [@attributes] => array ( [elementid] => largetext ) ) )
if take exact same php change xpath so:
$textnode = $xml->xpath("//default:richtext");
i array result:
array ( [0] => simplexmlelement object ( [@attributes] => array ( [x] => 278.418 [y] => 115.542 [columngap] => 18 [columncount] => 1 [textalign] => left [fontfamily] => trade gothic lt pro bold cn [fontsize] => 11 [color] => #518269 [whitespacecollapse] => preserve [width] => 212.582 [height] => 33 ) [content] => simplexmlelement object ( [p] => array ( [0] => simplexmlelement object ( [span] => scott, anna, , conner ) [1] => simplexmlelement object ( [span] => , our little 1 on way ) ) ) ) [1] => simplexmlelement object ( [@attributes] => array ( [x] => 278.998 [y] => 86.7168 [columngap] => 18 [columncount] => 1 [textalign] => left [fontfamily] => bootstrap [fontsize] => 19 [color] => #518269 [whitespacecollapse] => preserve [trackingright] => 4% [width] => 240 [height] => 29 ) [content] => simplexmlelement object ( [p] => simplexmlelement object ( [span] => johnsons ) ) ) [2] => simplexmlelement object ( [@attributes] => array ( [x] => 278.418 [y] => 77.2373 [columngap] => 0 [columncount] => 1 [textalign] => left [fontfamily] => trade gothic lt pro bold cn [fontsize] => 11 [color] => #518269 [whitespacecollapse] => preserve ) [content] => simplexmlelement object ( [p] => simplexmlelement object ( [span] => array ( [0] => w [1] => ishing best season. ) ) ) ) )
if notice, first 2 array items don't have info s7:elementid
, 2 should. third not have s7:elementid
design.
can explain why getting these unexpected array results, attributes showing up, , others not?
update
per dusan, updated php to:
$textnode = $xml->xpath("//default:richtext[@s7:elementid]");
now array returns attributes of element not have prefix. need attributes, prefix , not.
array ( [0] => simplexmlelement object ( [@attributes] => array ( [x] => 278.418 [y] => 115.542 [columngap] => 18 [columncount] => 1 [textalign] => left [fontfamily] => trade gothic lt pro bold cn [fontsize] => 11 [color] => #518269 [whitespacecollapse] => preserve [width] => 212.582 [height] => 33 ) [content] => simplexmlelement object ( [p] => array ( [0] => simplexmlelement object ( [span] => scott, anna, , conner ) [1] => simplexmlelement object ( [span] => , our little 1 on way ) ) ) ) [1] => simplexmlelement object ( [@attributes] => array ( [x] => 278.998 [y] => 86.7168 [columngap] => 18 [columncount] => 1 [textalign] => left [fontfamily] => bootstrap [fontsize] => 19 [color] => #518269 [whitespacecollapse] => preserve [trackingright] => 4% [width] => 240 [height] => 29 ) [content] => simplexmlelement object ( [p] => simplexmlelement object ( [span] => johnsons ) ) ) )
update 2
changing php seems attributes, both default , s7
prefix:
<?php $url = "http://testvipd7.scene7.com/is/agm/papermusepress/hol_12_f_green?&fmt=fxgraw"; $xml = simplexml_load_file($url); $xml->registerxpathnamespace('default', 'http://ns.adobe.com/fxg/2008'); $xml->registerxpathnamespace('s7', 'http://ns.adobe.com/s7fxg/2008'); $textnode = $xml->xpath("//default:richtext[@s7:elementid]"); // //default:richtext[@s7:elementid]/@* function pr($var) { print '<pre>'; print_r($var); print '</pre>'; } foreach($textnode $node){ pr($node->attributes('http://ns.adobe.com/s7fxg/2008')); pr($node->attributes()); } ?>
and xml result:
simplexmlelement object ( [@attributes] => array ( [caps] => none [colorname] => [colorvalue] => #518269 [colorspace] => rgb [elementid] => smalltext [fill] => true [filloverprint] => false [firstbaselineoffset] => ascent [joints] => miter [maxfontsize] => 11 [miterlimit] => 4 [referencepoint] => inherit [rowcount] => 1 [rowgap] => 18 [rowmajororder] => true [stroke] => false [strokeoverprint] => false [warpbend] => 0.5 [warpdirection] => horizontal [warphorizontaldistortion] => 0 [warpstyle] => none [warpverticaldistortion] => 0 [weight] => 1 ) ) simplexmlelement object ( [@attributes] => array ( [x] => 278.418 [y] => 115.542 [columngap] => 18 [columncount] => 1 [textalign] => left [fontfamily] => trade gothic lt pro bold cn [fontsize] => 11 [color] => #518269 [whitespacecollapse] => preserve [width] => 212.582 [height] => 33 ) ) simplexmlelement object ( [@attributes] => array ( [caps] => none [colorname] => [colorvalue] => #518269 [colorspace] => rgb [elementid] => largetext [fill] => true [filloverprint] => false [firstbaselineoffset] => ascent [joints] => miter [maxfontsize] => 19 [miterlimit] => 4 [referencepoint] => inherit [rowcount] => 1 [rowgap] => 18 [rowmajororder] => true [stroke] => false [strokeoverprint] => false [warpbend] => 0.5 [warpdirection] => horizontal [warphorizontaldistortion] => 0 [warpstyle] => none [warpverticaldistortion] => 0 [weight] => 1 ) ) simplexmlelement object ( [@attributes] => array ( [x] => 278.998 [y] => 86.7168 [columngap] => 18 [columncount] => 1 [textalign] => left [fontfamily] => bootstrap [fontsize] => 19 [color] => #518269 [whitespacecollapse] => preserve [trackingright] => 4% [width] => 240 [height] => 29 ) )
now able retrieve attributes of richtext
element. how can store attributes specific variable? example, how can set variable s7:elementid
, fontsize
attributes?
with //default:richtext/@s7:elementid
selecting elementid
attributes, not richtext
tag.
use this:
$textnode = $xml->xpath("//default:richtext[@s7:elementid]");
update: simplexmlelement::attributes docs says that:
simplexml has made rule of adding iterative properties methods. cannot viewed using var_dump() or else can examine objects.
so print_r
not showing information. try getting attributes using namespace:
foreach($textnode $node){ var_dump($node->attributes('http://ns.adobe.com/s7fxg/2008')); }
Comments
Post a Comment