0

ソース xml は次のとおりです: xml

これは、Adobe が作成した fxg ファイルの xml です。FXG ドキュメントは有効な xml であり、基本的に編集可能なドキュメントのすべての情報が含まれています。この特定の質問は、内容を変更できるように FXG 内で変更できるテキストに関するものです。

s7:elementIDxpath相対位置を使用する属性を持つその要素内のすべてのRichText要素と属性を取得しようとしています。

ソース XML には合計 3 つRichTextの要素しかなく、そのうちの 2 つだけが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.
                                )

                        )

                )

        )

)

お気付きのように、最初の 2 つの配列項目には の情報さえありませんが、必要な 2 つの項目ですs7:elementID。3 つ目は仕様によるものではありませんs7:elementID

一部の属性が表示され、他の属性が表示されない、これらの予期しない配列結果が得られる理由を誰か説明できますか?

アップデート

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:elementIDおよびfontSize属性の変数を設定するにはどうすればよいですか?

4

1 に答える 1

2

タグではなく、属性//default:RichText/@s7:elementIDを選択しています。elementIDRichText

これを使って:

$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 に答える