gpt4 book ai didi

php - 在元素的属性上保留前缀

转载 作者:行者123 更新时间:2023-12-03 16:43:28 24 4
gpt4 key购买 nike

我有以下代码来获取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...


如何防止删除前缀,或者在构建数组时如何在其中添加前缀?

最佳答案

不确定为什么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]);
}

关于php - 在元素的属性上保留前缀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11302976/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com