2012-06-28 49 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