Jump to content

Name() Xpath Function & Php ... How Is This Done?


Demerit

Recommended Posts

This should be simple, right?<Root><link>foo</link></Root>How do I return the element name (i.e., "link") while targeting the element's value (i.e., "foo").$result=$myxml->xpath(name('/Root/link[text()=foo]')) doesn't work$result=$myxml->xpath('/Root/link[text()=foo]/name()') doesn't work$result=$myxml->xpath('/Root/link[text()=foo]::name()') doesn't work$result=$myxml->xpath('/Root/link[text()=foo]/self::name()') doesn't work$result[0]->name() doesn't workname($result[0]); doesn't work and you can see how desperate I've become...What gives? How is this function used because I can't find a relevant, working example anywhere (including the W3C).

Link to comment
Share on other sites

use nodeName when working with the DOMhttp://www.w3schools.com/dom/prop_attr_nodename.asp

		<data>			<date>11/01/09</date>			<date>01/01/09</date>			<date>02/01/09</date>			<date>03/01/09</date>			<date>04/01/09</date>			<date>05/01/09</date>			<date>06/01/09</date>			<date>07/01/09</date>			<date>08/01/09</date>			<date>09/01/09</date>			<date>10/01/09</date>			<date>11/01/09</date>			<date>01/01/10</date>			<date>02/02/10</date>			<date>03/01/10</date>			<date>04/01/10</date>		</data>

document.write(xmlDoc.selectSingleNode("//*[text()='01/01/09']").nodeName)

returns 'date'

Link to comment
Share on other sites

In XPath, the local-name() or name() function, depending on whether you want the fully qualified name or the full one. PHP has the same DOM as JavaScript and ASP.NET, so what aalbetski said applies there too.The reason none of your solutions work is because there is no xpath() method in PHP's DOM... oh wait... you're using SimpleXML, aren't you?!? It's even easier there. The getName() method is what you need.So, the solutions are (or at least should be):

echo $myxml->xpath('/Root/link[text()="foo"]')->getName();

echo $myxml->xpath('/Root/link[text()="foo"]/local-name()');

$myxml = new DOMDocument;$myxml->load('file.xml');$xpath = new DOMXPath($myxml);echo $xpath->query('/Root/link[text()="foo"]')->item(0)->tagName;

Link to comment
Share on other sites

...So, the solutions are (or at least should be):
echo $myxml->xpath('/Root/link[text()="foo"]')->getName();echo $myxml->xpath('/Root/link[text()="foo"]/local-name()');

...

Yes, I'm using SimpleXML.So, I tried what you said and it's not working.Here's my XML code (test.xml):
<?xml version='1.0' standalone='yes'?><Root><link>foo</link></Root>

Here's my PHP code (test.php):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Untitled Document</title></head><body><?php$myxml = simplexml_load_file('test.xml');echo $myxml->xpath('/Root/link[text()="foo"]')->getName();/*echo $myxml->xpath('/Root/link[text()="foo"]/local-name()');*/?></body></html>

With

echo $myxml->xpath('/Root/link[text()="foo"]')->getName();

I receive the error message:"Fatal error: Call to a member function getName() on a non-object in /Applications/xampp/xamppfiles/htdocs/test.php on line 10"Then with

echo $myxml->xpath('/Root/link[text()="foo"]/local-name()');

I receive the error message:"Warning: SimpleXMLElement::xpath() [simplexmlelement.xpath]: Invalid expression in /Applications/xampp/xamppfiles/htdocs/test.php on line 11Warning: SimpleXMLElement::xpath() [simplexmlelement.xpath]: xmlXPathEval: evaluation failed in /Applications/xampp/xamppfiles/htdocs/test.php on line 11"Am I doing something wrong? Or is there something I need to be aware of with my XAMPP client?Running:Macbook ProOS 10.5.74GB RAMIntel Core 2 Duo, 2.4 GHzXAMPP Version 0.3 (19)PHP 5.2.6I really appreciate your help.-D

Link to comment
Share on other sites

Try:

echo $myxml->xpath('/Root/link[text()="foo"]')[0]->getName();

or (if that doesn't work)

$xpathMatches = $myxml->xpath('/Root/link[text()="foo"]');echo $xpathMatches[0]->getName();

It seems the XPath function returns an array of matched elements as objects, not the first matched element as an object. It is why the call to getName() produced that error.

Link to comment
Share on other sites

Try:
echo $myxml->xpath('/Root/link[text()="foo"]')[0]->getName();...

Didn't work since I don't believe the actual xpath expression can be accessed directly as an array, although its result can.As such, the second option did work. Thanks, boen_robot!Googling for this was difficult since so many people refer to the element's value as "the element", which it's not, really. Element = nodename, element value = nodevalue, right?Here's my code, if anyone wants it:text.xml

<?xml version='1.0' standalone='yes'?><Root><article>The</article><noun>Emperor</noun><verb>has</verb><determiner>no</determiner><noun>clothes</noun><punctuation>.</punctuation></Root>

test.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Untitled Document</title></head><body><?php$myxml = simplexml_load_file('test.xml');$xpathMatches = $myxml->xpath('/Root/*');echo "<table style='border: 1px solid #000000'><tr><td style='border: 1px solid #000000; text-align: right; font-weight: bold'>Element: </td>";while(list(, $node) = each($xpathMatches)){echo "<td style='border: 1px solid #000000; text-align: center'>".$node->getName()."</td>";}echo "</tr><tr><td style='border: 1px solid #000000; text-align: right; font-weight: bold'>Value: </td>";reset ($xpathMatches);while(list(, $chode) = each($xpathMatches)){echo "<td style='border: 1px solid #000000; text-align: center'>".$chode."</td>";}echo "</tr></table>";?></body></html>

And a cloud for those searching:Finding the name of an element finding an element's name XML xpath XSLT Using PHP to find the name of an element Using PHP to find an element's name Using PHP with an xpath expression to find the name of an element Using PHP with an xpath expression to find an element's name Using PHP How do I find the name of the container element using an xpath expression nodename from nodevalue finding a nodename by targeting the nodevalue getName() local-name() name()

Link to comment
Share on other sites

Or you could have googled "simplexml" which would return as the first findhttp://us2.php.net/simplexmlwith a list of SimpleXMLFunctions, including getName
This is true; however, initially I was looking for an xpath solution to be used within the xpath expression.Barring that, the SimpleXML solution will do quite nicely.Thanks for providing that link!
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...