這裏是源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:elementID
和fontSize
屬性設置變量?
你試過了:'//默認:RichText [@ s7:elementID]/@ *'? –