Asked  2 Years ago    Answers:  5   Viewed   72 times

This is starting to piss me off real bad. I have this XML code:

Updated with correct namespaces

<?xml version="1.0" encoding="utf-8"?>

<Infringement xsi:schemaLocation="" xmlns="" xmlns:xsi="">

And this PHP code:

    $data = urldecode($_POST["xml"]);
    $newXML = simplexml_load_string($data);


I've dumped only $newXML and gotten tons of data, but the only xPath I've run that returned anything but an empty array was "*"

Isn't "//ID" supposed to find all ID nodes in the document? Why isn't it working?




I've dumped only $newXML and gotten tons of data, but the only xPath I've run that returned anything but an empty array was "*"

So what was returned from var_dump($newXML->xpath("*"));? <Infringement>?

If the problem is namespaces, try this:

var_dump($newXML->xpath("//*[local-name() = 'ID']"));

This will match any element in the document whose name is 'ID', regardless of namespace.

My stuff works if i replace all "xmlns" with "ns"

Wait, what? Are you sure you showed us all the xmlns-related attributes in the document?

Update: The question was edited to show that the XML really does have a default namespace declaration. That explains the original problem: your XPath expression selects ID elements that are in no namespace, but the elements in your document are in the movielabs ACNS namespace, thanks to the default namespace declaration.

The declaration xmlns="" on an element means "this element and all descendants that don't have a namespace prefix (like ID) are in the namespace represented by the namespace URI ''." (Unless an intervening descendant has a different default namespace declaration, which would shadow this one.)

So use my local-name() answer above to ignore namespaces, or use jasso's technique to specify the movielabs ACNS and use it as intended.

Wednesday, August 31, 2022

this might help

$obj = new SimpleXMLElement($xml);
$rtn = array();
$cnt = 0;
foreach($obj->xpath('///OSes/*/*') as $rec)
  foreach ($rec as $rec_obj)
    if (!isset($rtn[$cnt]))
      $rtn[$cnt] = array();

    foreach ($rec_obj as $name=>$val)
      $rtn[$cnt][(string)$name] = (string)$val;
Thursday, December 1, 2022

You were probably not "cycling" the countries and cities:

    $xml_file = '/path';
    $xml = simplexml_load_file($xml_file);

    foreach($xml->Continent as $continent) {
        echo "<div class='continent'>".$continent['Name']."<span class='status'>".$continent['Status']."</span>";
        foreach($continent->Country as $country) {
            echo "<div class='country'>".$country['Name']."<span class='status'>".$country['Status']."</span>";
            foreach($country->City as $city) {
                echo "<div class='city'>".$city['Name']."<span class='status'>".$city['Status']."</span></div>";
            echo "</div>"; // close Country div
        echo "</div>"; // close Continent div
Sunday, August 14, 2022



This selects all nodes that are not page and that are children of any page in the document, the string value of whose id attribute is equal to the string contained in $yourId (most probably you would substitute $yourId above with a specific, desired string, such as '1').

Here is a simple XSLT-based verification:

<xsl:stylesheet version="1.0"
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:param name="pId" select="3"/>

 <xsl:template match="/">
     <xsl:copy-of select="//page[@id=$pId]/node()[not(self::page)]"/>

when this transformation is applied on the provided XML document (wrapped in a single top node to make it well-formed):

    <page id='1'>
        <title>Page 1</title>
        <page id='2'>
            <title>Sub Page 1</title>
        <page id='3'>
            <title>Sub Page 2</title>
    <page id='4'>
        <title>Page 2</title>

the wanted, correct result is produced:

<title>Sub Page 2</title>

Do note: One assumption made is that an id value uniquely identifies a page. If this is not so, the proposed XPath expression will select all page elements whose id attribute has a string valu of $yourId.

If this is the case and only one page element must be selected, the OP must specify which one of the many page elements with this id should be selected.

For example, it may be the first:


or the last:


or ...

Friday, October 28, 2022

I think the following meets what you are trying to do - it excludes the strong element containing title as well as the text node that is after it. You could expand it to include the other strong elements you want to exclude:

//div/node()[not(self::strong and contains(text(), "Title") or preceding-sibling::strong[1][contains(text(), "Title")])]

The strong node is skipped by the:

not(self::strong and contains(text(), "Title")

The following text is skipped by the:

preceding-sibling::strong[1][contains(text(), "Title")]

Note that the text node needs to check its closest preceding sibling (rather than its following sibling).

Wednesday, September 14, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :

Browse Other Code Languages