这里是源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]/@ *'? –