Search

When pulling a Dynamic XML feed from another site, is there any way to write the XPath to pull only a select few nodes within a nodeset?

Example:

Grab a blog feed and pull the latest 3 entries, but only include the date, title and URL.

Right now all I can figure out is this:

 atom:feed/atom:entry[position() <= 3]

It pulls the latest 3 entries with all the information, like title, entry, comments, links, categories, and so on ...

That's a lot of information that I don't need. I'd like to trim it down to just what I need.

Thanks!

There are a 2 sneaky ways to do this, and 1 fantasy feature I just dreamed up:

  1. Reformat the feed with an XSLT page and use that as the new source for another Dynamic XML DS.
  2. Create a new Dynamic XML DS for each subtree from the source feed, matching only the elements you need.
  3. Build multiple matching modes into the DS editor so that you could additionally use an XPath expression to select nodes (and their subtrees) to omit from the result.

Unfortunately, all of these would be inefficient and mostly pointless in all but the rarest edge cases. Normally less XML implies faster page rendering, but unless your source feed contains hundreds of unneeded elements, or your XSLT is doing some intensive recursion on all elements, removing the unused elements would actually cause page rendering to be slower.

The only other good reason I can think of to do this is to make debugging more pleasant, since I'd find it pretty annoying having to scroll past tons of unused code, but my solution for that would be to implement something Nils suggested a while ago (which we're planning to add to Symphony 3) - a table of contents for each individual DS with links to quickly scroll to their position.

Um.. correction: #2 above doesn't actually make sense. If you had several distinct subtrees with data you're interested in, you could just use XPath's | union operator to get them all without the surrounding elements.

This method works for a very limited number of use-cases. You'll run into problems with repeated elements (say, if you just wanted the <category> element from each entry, but different entries had different numbers of these elements), and also if you need information from parent elements (like an id attribute on the entry-wrapping elements - you could match these with @id but it might be hard or impossible to read these again from the page XSLT).

Thanks for the reply.

I messed with the union operator, but without success. I think my syntax isn't quite right. No biggie. I just wanted to streamline my sources a bit more on page that pulls a lot of data.

Can't you grab from the source, and use php.net/DOMXPath ? Basically, load the document. Created DOM. XPath Query for what you want. DOMDocument::saveXML() ?

Create an account or sign in to comment.

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.3
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details