XPath - selecting an element by index

Seemingly this should be a valid XPath select for a <cue> or <par> -- why does it's inclusion cause iHDSim to report an "Unknown Exception"

//div[@id=$chapter]/button[1]

This should identify a specific <div> (using an XPathVariable set in script), and get the first button it contains. In fact it seems that the bracketed subscript notation doesn't work at all...


Answer this question

XPath - selecting an element by index

  • liorhg

    Your question as I understand it is "how to select and element by index ", so why not just go straight to the button with "id('button_id')[state]"

    Is this a case where buttons under different divs share ids Otherwise I don't see why you need to handle div selection in the script. Right




  • rachbabe

    Thanks again, that was even more helpful. Using

    el.state.focused='true'; el.state.unsetProperty('focused');

    ...sounds like a winner, since using the markup engine here was a workaround. Alternately, using the XPath contains() function to simulate an intersection might absolve the need for script poking the focus around altogether.

  • Kunk

    Thanks Josh, that's very helpful to know. I suppose the best workaround then would be to fire an event to script which looks up the <div>, then loops through the children using element.childNodes. When the first element with .nodeName="button" is found, set an XPathVariable containing the id of the button so the markup engine triggers on the element.

    The reason it's important for markup to handle this (in this case) and not script is that I'm trying to set the focus in a way that doesn't break the explicit/implicit navigation engine. As I had been observing (and Peter confirmed that this is a feature -- not something I was doing wrong -- in another message), the moment that .focused is set from script, the assumption is that script is taking over focus management entirely and the markup navigation scheme is blocked.

  • Saibal Goswami

    Your solution sounds feasible.

    One note regarding script focus management that Peter has probably already hit on but I'll reiterate: if you set focus from script using the Animated Property API (e.g. document.someElementId.state.focused = "true";), script will take control of focus management and lock it up. However, you can immediately release control back to markup by doing document.someElementId.state.unsetProperty("focused"); The unsetProperty() will give control back to the markup engine, but the focused element will remain as the script set it (it will not revert as a style property would).



  • AmineYA

    I knew chances were none of you were missing the obvious, but just in case . . .

    Good info Peter.


  • XNA Rockstar

    The subset of XPath specified by HD DVD doesn't allow querying for child or parent nodes, so the bolded red part below is likely what's breaking your statement:

    //div[@id=$chapter]/button[1]

    I believe bracketed subscript works (so //div[1] would return the first div in the returned nodeset, for what it's worth), but the usefulness of that is limited since you cannot delve into child nodes as you are trying to.



  • Eric Wellnitz

    Thanks Peter, that's the ultimate answer.

  • Jayakumar A

    And just to clear that up :-) if all you want is the 42nd item in a nodeset, you could use:

    //div[position()=42]

    where 'position' is a built-in XPath function.

    But that won't solve the original problem which was using trying to do queries on child nodesets.



  • Dietz

    wmerydith, the subject line only applies to the first 2 messages. :) I meant "index" as in "the Nth node in a node-set" (like an array index). The "index" would "select" the node out of the node-set, for example in order to apply a style using a <set> inside a <cue> or <par>... I was trying to figure out why the XPath 1.0 in my first message wouldn't work in iHDSim. Josh cleared that up in the second message; then the discussion sidetracked to something else entirely.

    One example where one might "resort" to setting focus through script If choosing which element to set the focus on is too complicated or inefficient to express using iHD XPaths...

  • XPath - selecting an element by index