Search

Hi there,

I would like to get the value of a node name. name() and local-name() don't seem to work - else I am using them wrong.

My XML

<entry>
<other-randomly-named-nodes />
<page-1-name handle="nice-page">Nice Page</page-1-name>
    <page-2-name handle="another-page">Another Page</page-2-name>
    <page-3-name handle="real-sexy-page">Real Sexy Page</page-3-name>
    <etc>
</entry>

All I have is the value of one of the handles from URL params $page (so one of nice-page, another-page, sexy-page or any other page).

I need to get which page number I have so I can use that to find other nodes from that page (all of which are at the same level in the XML tree because some muppet (me!) made this for a client and they now want to change how it works but keep existing data).

I can use

//*[@handle=$page]

To get the value of the node but I really want to get the name of the node. Something like:

//*[@handle=$page]/local-name()

So if $page = nice-page I could get display page-1-name

Then I can use a string function to just get the page number eg "page-1" which I can use to match other nodes.

Any ideas - hopefully I'm just using local-name() wrong but I've tried a lot of combinations!

Cheers.

Try passing an XPath expression to the name function directly:

local-name(//*[@handle=$page])

Remember it is a function, so takes an argument inside the parenthesis. If this omitted (as per your example) then the current context node is assumed.

You could also try caching the first query to a variable, then calling name() or local-name() on that variable.

Unless you're dealing with XML namespaces, you can just use name(). local-name() is there to access the current namespace only, so you probably don't need it with normal Symphony XML.

Thanks Nick - that did the trick!

@timchesney: you should try to stay away from // selectors. It is best to write out the entire path of the nodes you wish to select for performance reasons and to prevent unwanted results!

Hey Remie, Yeah - I generally do steer clear of them. In this case I only have the handle attribute of an unknown element so I'm basically searching the whole tree. I could narrow it down to data/datasource/entry/ I guess but there is only one datasource for the page and one entry so I doubt there would be much of a performance improvement.

I'm only doing it in this silly way because I set the whole thing up in a dumb way ages ago and I'm trying to hack in some new functionality. Not ideal!

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