获得意外的 xpath 结果 [英] Getting Unexpected xpath results
问题描述
这里是源 xml:xml
这是 Adobe 生成的 fxg 文件的 xml.FXG 文档是有效的 xml,它基本上包含了可以编辑的文档的所有信息.这个特定问题与可以在 FXG 中更改的文本有关,以便内容可以更改.
This is the xml of an fxg file produced by Adobe. The FXG document is valid xml, and it basically contains all the information for a document that can be edited. This specific question pertains to text that can be changed within the FXG so that the content can change.
我正在尝试使用 xpath 相对位置获取该元素中具有 s7:elementID
属性的所有 RichText 元素和属性.
I'm tryng to grab all the RichText elements and attributes within that element that have an attribute of s7:elementID
using xpath relative location.
源 XML 总共只有三个 RichText
元素,其中只有两个具有 s7:elementID
The source XML only has three total RichText
elements, with only two of them having a 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,我希望它选择所有具有 s7:elementID
的 RichText
元素,以及该元素的其他属性.但是,它不会获取这些元素的任何其他属性.这是它的输出:
I got this far with help from another question. But the returned array is not what I was expecting. setting the xpath as I did, I am expecting it to select all the RichText
elements that have an s7:elementID
, as well as the other attributes of that element. However, it isn't grabbing any of the other attributes of those elements. here is what it outputs:
Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[elementID] => smalltext
)
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[elementID] => largetext
)
)
)
如果我使用完全相同的 php 但像这样更改 xpath:
If I take the exact same php but change the xpath like so:
$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
.
If you notice, the first two array items don't even have the info for the s7:elementID
, but they are the two that should. The third does not have a s7:elementID
by design.
谁能解释为什么我得到这些意外的数组结果,显示一些属性,而其他属性没有?
Can anyone explain why I am getting these unexpected array results, with some attributes showing up, and others not?
更新
根据 dusan,我将 php 更新为:
Per dusan, i updated the php to:
$textNode = $xml->xpath("//default:RichText[@s7:elementID]");
现在数组只返回没有前缀的元素的属性.我需要所有属性,前缀而不是.
Now the array only returns attributes of the element that do not have a prefix. I need all attributes, prefix and not.
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
前缀的:
changing the php to this seems to get all the attributes, both default and with the s7
prefix:
<?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
属性设置变量?
now that it is able to retrieve all the attributes of the RichText
element. How can I store certain attributes as a specific variable? For example, how can I set a variable for the s7:elementID
and the fontSize
attributes?
推荐答案
使用 //default:RichText/@s7:elementID
您正在选择 elementID
属性,而不是RichText
标签.
With //default:RichText/@s7:elementID
you are selecting elementID
attributes, not the RichText
tag.
使用这个:
$textNode = $xml->xpath("//default:RichText[@s7:elementID]");
更新:SimpleXMLElement::attributes 文档 说:
SimpleXML 制定了向大多数方法添加迭代属性的规则.不能使用 var_dump() 或其他任何可以检查对象的方法来查看它们.
SimpleXML has made a rule of adding iterative properties to most methods. They cannot be viewed using var_dump() or anything else which can examine objects.
所以 print_r
没有向您显示所有信息.尝试使用其命名空间获取属性:
So print_r
is not showing to you all the information. Try getting the attributes using their namespace:
foreach($textNode as $node){
var_dump($node->attributes('http://ns.adobe.com/S7FXG/2008'));
}
这篇关于获得意外的 xpath 结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!