2012-06-28 51 views
0

這裏是源XML:xml出現了意外的XPath導致

這是由Adobe生產的FXG文件的XML。 FXG文檔是有效的xml,它基本上包含可編輯文檔的所有信息。這個具體問題涉及FXG中可以更改的文本,以便內容可以更改。

我試圖抓取使用xpath相對位置具有屬性s7:elementID的元素中的所有RichText元素和屬性。

源XML只有三個總RichText元素,用只有他們兩個人有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>"); 

?> 

我得到這個遠從另一個問題的幫助。但是返回的數組並不是我所期望的。像我一樣設置xpath,我期待它選擇所有RichText元素,其中包含s7:elementID以及該元素的其他屬性。但是,它並沒有抓住這些元素的其他屬性。這裏就是它輸出:

Array 
(
    [0] => SimpleXMLElement Object 
     (
      [@attributes] => Array 
       (
        [elementID] => smalltext 
       ) 

     ) 

    [1] => SimpleXMLElement Object 
     (
      [@attributes] => Array 
       (
        [elementID] => largetext 
       ) 

     ) 

) 

如果我參加了完全相同的PHP,但改變像這樣的XPath:

$textNode = $xml->xpath("//default:RichText"); 

我得到這個數組結果:

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, and Conner 
           ) 

          [1] => SimpleXMLElement Object 
           (
            [span] => and our little one on the 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] => THE 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 you the best this season. 
           ) 

         ) 

       ) 

     ) 

) 

如果您發現,前兩個數組項目甚至沒有s7:elementID的信息,但它們應該是兩個。第三個沒有設計s7:elementID

任何人都可以解釋爲什麼我得到這些意外的數組結果,一些屬性顯示,而其他人不是?

UPDATE

每杜尚,我的PHP更新爲:

$textNode = $xml->xpath("//default:RichText[@s7:elementID]");

現在陣列只返回沒有前綴的元素的屬性。我需要所有的屬性,前綴和不。

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, and Conner 
           ) 

          [1] => SimpleXMLElement Object 
           (
            [span] => and our little one on the 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] => THE JOHNSONS 
         ) 

       ) 

     ) 

) 

更新2

改變PHP來,這似乎讓所有的屬性,包括默認和與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]"); // //default:RichText[@s7:elementID]/@* 

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


foreach($textNode as $node){ 
    pr($node->attributes('http://ns.adobe.com/S7FXG/2008')); 
    pr($node->attributes()); 
} 
?> 

和XML結果:

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 
     ) 

) 

現在,我t能夠檢索RichText元素的所有屬性。如何將特定屬性存儲爲特定變量?例如,如何爲s7:elementIDfontSize屬性設置變量?

+1

你試過了:'//默認:RichText [@ s7:elementID]/@ *'? –

回答

2

With //default:RichText/@s7:elementID您選擇的是elementID屬性,而不是RichText標籤。

使用此:

$textNode = $xml->xpath("//default:RichText[@s7:elementID]"); 

更新:SimpleXMLElement::attributes docs說:

的SimpleXML取得了添加迭代屬性,大多數方法的規則。它們不能使用var_dump()或其他可以檢查對象的東西來查看。

因此print_r沒有向您顯示所有信息。嘗試使用它們的命名空間獲取屬性:

foreach($textNode as $node){ 
    var_dump($node->attributes('http://ns.adobe.com/S7FXG/2008')); 
} 
+0

現在,這更有意義了。但是我的最後一個問題是它沒有獲取具有's7:'前綴的'RichText'元素中的任何屬性。它只獲取沒有前綴的屬性。我更新了主帖以顯示數組。任何想法爲什麼會這樣做?我需要所有具有前綴的屬性。 – thindery

+0

我覺得很奇怪,xpath引用了所有具有's7:elementID'的RichText'元素,但是它甚至沒有在數組中輸出該屬性..? – thindery

+0

我已更新我的回答 – dusan