获得意外的xpath结果

时间:2012-06-28 19:32:35

标签: php xml xpath

以下是源xml:xml

这是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

任何人都可以解释为什么我会得到这些意外的数组结果,某些属性显示出来,而其他属性没有?

更新

Per dusan,我将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
        )

)

现在它能够检索RichText元素的所有属性。如何将某些属性存储为特定变量?例如,如何为s7:elementIDfontSize属性设置变量?

1 个答案:

答案 0 :(得分:2)

使用//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'));
}