Viewed   78 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

Not sure why you used namespace notaion in the first place(the sample xml is not namespaced)

In your xpath, you need to select all condition/normal tags, not the condition tag as you were doing... Also, xpath() returns a list, so foreach over it. You don't need to access it as children, unless you want to parse the children of $child. There it would make sense, and it would work as expected.

foreach ($xml->xpath("/Condition/Normal") as $child) {
    echo $child["type"] . "="  . $child->getName()."<br/>";


Monday, September 12, 2022

You can probably do it with XPath or something, but SimpleXMLElement::xpath() returns an array that is easy to sort:

usort($xQuery, function ($a, $b) { return strcmp($a->Make, $b->Make); });
foreach ($xQuery as $results) {
    // …
Saturday, September 24, 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 :