2

要素の属性を取得する次のコードがありますRichText。属性には接頭辞のようなものがあるか、接頭辞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]");

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

$result1 = array();
$result2 = array();
foreach($textNode as $node){
    $result1[] = $node->attributes('http://ns.adobe.com/S7FXG/2008');
    $result2[] = $node->attributes();

}

$text = array_merge($result1,$result2);

pr($text);

?>

出力

Array
(
    [0] => 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
                )

        )

    [1] => 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
                )

        )

    [2] => 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
                )

        )

    [3] => 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
                )

        )

)

で収集されたすべての属性は、プレフィックス$result1[]を持つ必要があるものです。s7:ただし、xml からデータを格納するときはs7:、これらの属性から を取り除きます。これは、キー 0 と 1 の配列値からプレフィックスが削除されていることがわかります。そこに留まるにはプレフィックスが必要なので、次のようになります。

[s7:caps] => none
[s7:colorName] => 
[s7:colorValue] => #518269
[s7:colorspace] => rgb
[s7:elementID] => smalltext

etc...

プレフィックスが削除されないようにするにはどうすればよいですか、または配列が構築されているときにプレフィックスをそこに戻すにはどうすればよいですか?

4

1 に答える 1

1

PHP が名前空間を抽出から除外する理由は不明です。おそらく、libxml に詳しい人がそれを手伝ってくれるでしょうが、抽出後にそれらの名前を変更するのは簡単です。

//some sample XML - get the attributes
$xml = "<root name='root'><node id='1'>hello</node></root>";
$doc = new SimpleXMLElement($xml);
$attrs = $doc->xpath('//@*');

//iterate over array and add in namespace prefixes      
foreach($attrs as $key => $val) {
    $attrs['s7:'.$key] = $val;
    unset($attrs[$key]);
}
于 2012-07-03T08:59:21.160 に答える