This question already has answers here:
Reference - How do I handle Namespaces (Tags and Attributes with a Colon in their Name) in SimpleXML?
(2 answers)
Closed 4 years ago.
I have an XML, schema below
$xml =
'<NodeSet
xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://opcfoundation.org/UA/2008/02/Types.xsd">
<Node category="category" i:type="ObjectNode">
<NodeId>
<Identifier>i=86</Identifier>
</NodeId>
</Node>
</NodeSet>';
I need to extract the i:type attribute value from the Node element. I have tried accessing it as i would do it when accessing normal attributes but it seems it doesn't work that way
Here is how i can access the category attribute,
$xml=simplexml_load_string($xml);
echo $xml->Node[0]['category']; //this prints 'category' as expected
echo $xml->Node[0]['i:type']; //prints nothing, how do i get the i:type attribute value ?
You need to access the attributes with the namespace, this can be done using the attributes() method...
echo $xml->Node[0]->attributes("i",true)['type'];
Using ("i",true) says use the i prefix rather than having to put the URI.
Related
This question already has answers here:
Reference - How do I handle Namespaces (Tags and Attributes with a Colon in their Name) in SimpleXML?
(2 answers)
Closed 4 years ago.
Here is my XML:
<WebContent diffgr:id="WebContent1" msdata:rowOrder="0">
<orig_inv_no>73</orig_inv_no>
<inv_no>141</inv_no>
<inv_type>S</inv_type>
<content_type>3</content_type>
<content_type_desc>Test</content_type_desc>
<content_value>Sample content</content_value>
</WebContent>
<WebContent diffgr:id="WebContent2" msdata:rowOrder="0">
<orig_inv_no>73</orig_inv_no>
<inv_no>141</inv_no>
<inv_type>S</inv_type>
<content_type>3</content_type>
<content_type_desc>Test</content_type_desc>
<content_value>Sample content</content_value>
</WebContent>
I am having a lot of trouble getting the attribute "differ:id" for the node "WebContent"
It seems like it doesn't like the colon in the attribute name. Any Ideas?
the attribute is "diffgr:id" not "differ:id" maybe this is your issue
Try this:
$xml->WebContent->attributes("diffgr",TRUE)->id;
// TRUE means that `diffgr` is a prefix of the attribute
COuld be found here -> https://stackoverflow.com/a/15546669/2040840
This question already has answers here:
Accessing #attribute from SimpleXML
(10 answers)
Closed 6 years ago.
I use SimpleXMLElement class for working with xml files in my project.
My question is: how to get an attribute value of some tag with some attribute? You may assume I know the name of the tag, the name of the attribute and it's location inside the xml file. For example, for such a string <someTag cp="c2"> knowing values 'someTag' and 'cp' I want to obtain the string "c2".
Thanks is advance.
You can use the attributes() function on the node to get it's attributes:
$xml_str = '<xml>
<node>
<someTag cp="c2">content</someTag>
</node>
</xml>';
$res = simplexml_load_string($xml_str);
$items = $res->xpath("//someTag");
var_dump((string) $items[0]->attributes()->cp);
The returned element is an SimpleXMLElement, so in order to use it I converted it to string (using the (string) cast).
This question already has answers here:
A simple program to CRUD node and node values of xml file [closed]
(2 answers)
PHP XML remove element and all children by name
(2 answers)
Closed 9 years ago.
How to remove all xml children with a given element name in PHP with SimpleXML?
<xml>
<computer></computer>
<book></book>
<book></book>
</xml>
I need a quick way to remove all "book" elements. Thanks!
select all nodes with xpath and unset them:
$xml = simplexml_load_string($x); // assume XML in $x
$books = $xml->xpath("//book");
foreach ($books as $book) unset($book[0]);
see it in action: https://eval.in/105257
This question already has answers here:
Remove a child with a specific attribute, in SimpleXML for PHP
(18 answers)
Closed 8 years ago.
<?xml version="1.0"?>
<rights>
<visitor>
<user>updateUsers</user>
<user>deleteUsers</user>
<user>createUsers</user>
<user>readUsers</user>
</visitor>
</rights>
This is the xml file i am using. I want to delete the nodes depending on their values. Eg if the user want me to remove the entry for updateUsers then i could remove the <user> that has value updateUsers.
I want to do it using php.
here is my script
$xmlobj=simplexml_load_file($xmlpath);
foreach($xmlobj->{$owner}[0]->children() as $mychild)
{
$childvalues[]=$mychild;
}
foreach($childvalues as $values)
{
if($userentry==$values)
{
//code to remove that node
}
}
I am struck with it. Help will be very much appreciated
Remove the child element from the parent node.
PHP Simple XML remove all Children
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Xpath fails if an element has a a xmlns attribute
I have been trying for a long time to extract a string from the following xml with no luck
http://chris.photobooks.com/xml/default.htm?state=8T
I am trying to get the ASIN number of a book and I have tried
$xpath->query('//MarketplaceASIN/ASIN')->item(0)->nodeValue;
and
$xpath->query('/GetMatchingProductResponse/GetMatchingProductResult[1]/Product/Identifiers/MarketplaceASIN/ASIN')->item(0)->nodeValue;
but neither seem to work, what am I doing wrong here?
The elements in that document are bound to the namespace http://mws.amazonservices.com/schema/Products/2011-10-01.
You may have missed it because it does not use a namespace-prefix and the xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01" just looks like an attribute, but namespace attributes are special.
All of the descendant elements inherit that namespace. You will want to register the namespace with a namespace-prefix and adjust your XPath:
$rootNamespace = $xml->lookupNamespaceUri($xml->namespaceURI);
$xpath->registerNamespace('a', $rootNamespace);
$elementList = $xpath->query('//a:MarketplaceASIN/a:ASIN');
Or you could use a more generic XPath that matches on elements and uses a predicate filter to match the local-name() and namespace-uri():
//*[local-name()='MarketplaceASIN' and namespace-uri()='http://mws.amazonservices.com/schema/Products/2011-10-01']/*[local-name()='ASIN' and namespace-uri()='http://mws.amazonservices.com/schema/Products/2011-10-01']