2012-07-03 134 views
2

我有以下代碼來獲取RichText元素的屬性。這些屬性的前綴類似於s7:,或根本沒有前綴。保留元素屬性的前綴

<?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]"); 

function pr($var) { print '<pre>'; print_r($var); print '</pre>'; } 

$result1 = array(); 
$result2 = array(); 
foreach($textNode as $node){ 
    $result1[] = $node->attributes('http://ns.adobe.com/S7FXG/2008'); 
    $result2[] = $node->attributes(); 

} 

$text = array_merge($result1,$result2); 

pr($text); 

?> 

輸出

Array 
(
    [0] => 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 
       ) 

     ) 

    [1] => 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 
       ) 

     ) 

    [2] => 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 
       ) 

     ) 

    [3] => 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 
       ) 

     ) 

) 

所有$result1[]收集的屬性是應該有s7:前綴的人。但是當它存儲來自xml的數據時,它將從這些屬性剝離s7:。你可以看到這一點,因爲鍵0和1的數組值已經去掉了前綴。我需要的前綴留在那裏,所以它看起來是這樣的:

[s7:caps] => none 
[s7:colorName] => 
[s7:colorValue] => #518269 
[s7:colorspace] => rgb 
[s7:elementID] => smalltext 

etc... 

如何防止被剝離的前綴,或者我怎麼能添加它那裏當陣列得到建?

回答

1

不知道爲什麼PHP省略了提取中的命名空間 - 也許有人更熟悉libxml可以幫助解決這個問題 - 但是在提取後重命名它很簡單。

//some sample XML - get the attributes 
$xml = "<root name='root'><node id='1'>hello</node></root>"; 
$doc = new SimpleXMLElement($xml); 
$attrs = $doc->xpath('//@*'); 

//iterate over array and add in namespace prefixes  
foreach($attrs as $key => $val) { 
    $attrs['s7:'.$key] = $val; 
    unset($attrs[$key]); 
} 
相關問題