0

这是源xml:xml

这是 Adob​​e 制作的 fxg 文件的 xml。FXG 文档是有效的 xml,它基本上包含了可以编辑的文档的所有信息。此特定问题与可以在 FXG 中更改的文本有关,以便内容可以更改。

我正在尝试获取该元素中具有s7:elementID使用 xpath 相对位置的属性的所有 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属性设置变量?

4

1 回答 1

2

随着//default:RichText/@s7:elementID您选择elementID属性,而不是RichText标签。

用这个:

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

更新: SimpleXMLElement ::attributes 文档说:

SimpleXML 制定了一个规则,为大多数方法添加迭代属性。不能使用 var_dump() 或其他任何可以检查对象的东西来查看它们。

因此print_r,没有向您显示所有信息。尝试使用它们的命名空间获取属性:

foreach($textNode as $node){
    var_dump($node->attributes('http://ns.adobe.com/S7FXG/2008'));
}
于 2012-06-28T20:17:22.273 回答