[development] trouble destructing xml

Matt matt at aleph-null.tv
Fri Mar 25 15:30:37 UTC 2011


If you wanted, you can avoid the children() calls by writing a
slightly more complex selector in qp().

You can use CSS3-style selectors to get the right relationships. Example:

$xml = '<doc>
  <a>
    <b>
      <c>foo</c>
    </b>
    <c>another c</c>
 </a>
<c>a third c</c>
</doc>'

// This will be 3, since there are 3 <c> elements
qp($xml, 'c')->size()

// This will match both of the <c> tags underneath the <a>, so it will be 2.
qp($xml 'a c')->size()

// This will match just the <c>'s that are direct descendants of <b>'s
that are direct descendants of <a>'s.
qp($xml, 'a>b>c')->size();

The selector syntax is exactly the same as what you would use if you
were writing a stylesheet:

a>b>c {
  color: red;
}

Matt

On Fri, Mar 25, 2011 at 8:29 AM,  <jeff at ayendesigns.com> wrote:
> I hadn't been concluding I had trouble with simplexml only because dsm()
> didn't show anything, more that dsm reinforced the situation. I was
> referring to what should have been the element in code and coming up empty,
> or with in error, in the case of foreach.
>
> Xpath doesn't work for some reason. I presume there could be something
> unusual with the file layout, since it's full of cpath stuff and I'm
> excerpting it here. It might, in part, have something to do with some of the
> path names being hyphenated. I know php has a problem with that when
> creating an object reference unless it is encased in {}.
>
> Not only does querypath work with the tag name, but it worked too well at
> first. There are, apparently, other such tags elsewhere in the data, and I
> was receiving output for all of them. It turns out you can do some wonderful
> things with querypath. First, I provided further context by using:
>
>        foreach (qp('text.xml', parent-tag-name)->children as $item)
>
> but the problem with that was I received all the children, which included
> the tags I needed, but also others. So, you can also do this:
>
>        foreach (qp('text.xml', parent-tag-name)->children(child-tag-name) as
> $item)
>
> and then only receive the correct tags in the correct concept, such as xpath
> would have done with parent-tag/child-tag had it worked in this case.
>
> Jeff
>



-- 
http://technosophos.com
http://querypath.org


More information about the development mailing list